Connecting Tech Pros Worldwide Forums | Help | Site Map

Reqd a prog in C++ for conversions

Newbie
 
Join Date: Sep 2006
Posts: 2
#1: Sep 1 '06
please can anbody help me in solving this problem in C programming!!!
I have to write a program to convert the Decimal No. Sys in to Binary coded system.
It has ben a headache these days for me to solve this problem.
I m completely unable to solve this problem

rgb rgb is offline
Member
 
Join Date: Aug 2006
Posts: 37
#2: Sep 1 '06

re: Reqd a prog in C++ for conversions


this is one way to do it (simplified):

int input=7;
int r=0, x=0;
while(input>0)
{
int y = i%2;
input /= 2;
x += y * pow(10,r++);
printf("%d\n",x);
}

where
"input" should be in some form of scanf( ) or other input command and statement "x += y * pow(10,r++);" should be change to some type of strcat( ) to prevent overflow.
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#3: Sep 1 '06

re: Reqd a prog in C++ for conversions


Quote:

Originally Posted by rgb

this is one way to do it (simplified):

int input=7;
int r=0, x=0;
while(input>0)
{
int y = i%2;
input /= 2;
x += y * pow(10,r++);
printf("%d\n",x);
}

where
"input" should be in some form of scanf( ) or other input command and statement "x += y * pow(10,r++);" should be change to some type of strcat( ) to prevent overflow.

This is a very poor solution

1. It does not compile (i should be input ?)

2. It goes wrong is input >= 0x400

3. Because it uses pow it decends into floating point arithmatic unnecessarily which could introduce rounding errors.

4. It uses an int to represent a decimal coded binary value (which is why 2 happens), in my opinion very poor style.


abbey, you sound like you have already made an attempt at doing this, ignore rgbs solution and post what you already have we will help you make it work.

You should be able to do it by using the bitwise operators to determin if any given binary digit should be 1 or 0 and then you can store the result in a string and then print it out when finished.
Newbie
 
Join Date: Sep 2006
Posts: 2
#4: Sep 2 '06

re: Reqd a prog in C++ for conversions


plese can any body help me out with this program in C lang.
question is to convert Decimal Number System to Binary System.
it is a very tough prog i guess. Plz give the prog wid correct input
Newbie
 
Join Date: Sep 2006
Posts: 1
#5: Sep 2 '06

re: Reqd a prog in C++ for conversions


Quote:

Originally Posted by abbey07

plese can any body help me out with this program in C lang.
question is to convert Decimal Number System to Binary System.
it is a very tough prog i guess. Plz give the prog wid correct input

this programe is working on every base
#include<conio.h>
#include<stdio.h>
#include<alloc.h>
void main()
{
int digit,num,i,x,base;
int *p;
clrscr();
printf("\nenter number");
scanf("%d",&num);
printf("\nenter base");
scanf("%d",&base);
digit=0;
x=num;
do
{
num=num/base;
digit++;
}while(num!=0);
p=(int *)malloc(sizeof(int));

for(i=0;i<digit;i++)
{

p[i]=x%base;
x=x/base;
}
for(i=digit-1;i>=0;i--)
{
printf("%d",p[i]);
}
getch();
}
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#6: Sep 2 '06

re: Reqd a prog in C++ for conversions


abbey07 - please don't double post about the same problem

MOHIT AGRAWAL - please only respond with code where appropriate, in this case I believe that abbey07 was posting a homework task, by giving them the answer instead of helping them write the answer themselves you prevent (or at least hinder) their learning. It is better for people to work out the solutions to their problems themselves even if they do need hints along the way.

And BTW your solution is wrong because you do not allocate enough memory and because since you do not output a new line or flush the stdout the answer will not necessarily be printed. stdout only has to display output when it receives a new line.
rgb rgb is offline
Member
 
Join Date: Aug 2006
Posts: 37
#7: Sep 5 '06

re: Reqd a prog in C++ for conversions


Quote:

Originally Posted by Banfa

This is a very poor solution

1. It does not compile (i should be input ?)

2. It goes wrong is input >= 0x400

3. Because it uses pow it decends into floating point arithmatic unnecessarily which could introduce rounding errors.

4. It uses an int to represent a decimal coded binary value (which is why 2 happens), in my opinion very poor style.


abbey, you sound like you have already made an attempt at doing this, ignore rgbs solution and post what you already have we will help you make it work.

You should be able to do it by using the bitwise operators to determin if any given binary digit should be 1 or 0 and then you can store the result in a string and then print it out when finished.

I don't want to give out the solution so i just give the idea that you need to do a modulo of 2 to get the remainder and then get the whole number part of the input divided by 2. plus I explicitly mention that the input part wasn't done as I believe as you said that this problem is some type of learning exercise in school setting. I also agree that it won't compile since I don't even have the function declaration.
Reply