Monday, December 12, 2011

Please help editing a C program for converting decimal to binary?

For every decimal value I type, I get 1. I provided my thought process behind this program. please point out where i was wrong. I am very new to programming. I'm using microsoft visual express c++ 2010.








#include %26lt;stdio.h%26gt;


#include "conio.h"





int main()


{


/* This program prompts the user for a decimal and then converts it to a binary number.*/





int bit=0;//intilizes bit, bit is a placeholder for the 8 positions in a binary number.


int decimal=0;//initializes decimal variable


int base=256;/* represents the value of the power of 2 at a point, starts with 256 so that when it enters the 'for' loop it will start with 128 which =2^7 or the first bit holder*/


printf ("\nEnter a decimal value: ");//prompts the user for a decimal value








for(bit=0; bit%26lt;=7; bit++)//for loop, ranges from 0 to 7 because there are 8 numbers in a binary number (atleast in this case)


{//start for loop


scanf("%d", %26amp;decimal);//reads the entered decimal value and puts it into the 'decimal' variable


base%=2;//takes the remainder of base/2 to get to the following power of 2





if (decimal-base%26gt;=0)//if the difference is %26gt;= zero print 1


{//start if


printf("1");





}//end if


else{//start else; if the diff is not %26gt;= zero print 0 and do not subtract


printf("0");


continue;//do not subtract and go to next expression


}//end else


decimal = decimal-base;// formula for subtraction; decimal is equivalent to the previous decimal minus the corresponding power of 2








}//end for loop


printf("\nThe value of %d is ", decimal);//gives value of previously given decimal number


for(bit=0; bit%26lt;=7; bit++)// grab values from for loop, then...


printf("%d", %26amp;decimal);// print these values on screen


printf(" in binary.\n");//print 'in binary.'





_getch();//pause screen


return 0;//end program





_getch();


return 0;


}|||int power = 1


print ("enter a number")


get decimal


// get is outside of loop.





while (decimal %26gt; 0)


//get the binary digit


rem = decimal % 2;





// store the remainder.


store = store + (rem * power);





// reset decimal to the next value. Int division results in an int.


decimal = decimal / 2;





// though we are looking for a binary number we are storing the number as a decimal


power = power * 10;


end while





print store.


end program.





Try to remember to keep the program simple. Also try to understand what you are doing.





Have fun.|||It's good that you had a thought process, and tried to document it, but your program doesn't work. As the previous responder said, you need to keep it simple. Rather than try to understand the problem in your logic, or suggest you debug it and try to fix what you have, I recommend you just start over. There is no conversion to be done, the entered number is already all ones and zeros, you just want to display it that way. Here's one way to do it:





#include %26lt;stdio.h%26gt;


#include %26lt;limits.h%26gt;





#define MAX_LINE_LEN 32





char line[MAX_LINE_LEN];


const char binChar[] = {'0','1'};


void printBinary(int);





int main(int argc, char *argv[]) {


聽 聽 int n;





聽 聽 while (1) {


聽 聽 聽 聽 printf("\nEnter a base-10 integer : ");


聽 聽 聽 聽 fgets(line,MAX_LINE_LEN,stdin);


聽 聽 聽 聽 if (sscanf(line,"%d",%26amp;n) == 1) {


聽 聽 聽 聽 聽 聽 printBinary(n);


聽 聽 聽 聽 }


聽 聽 }


聽 聽 return 0;


}





void printBinary(int x) {


聽 聽 static const unsigned N = sizeof(x) * CHAR_BIT - 1;


聽 聽 static char bin[sizeof(x) * CHAR_BIT + 1];


聽 聽 unsigned i;


聽 聽 char *p = bin;





聽 聽 bin[N] = '\0';


聽 聽 printf("bin: ");


聽 聽 for (i = 0; i %26lt;= N; i++) {


聽 聽 聽 聽 bin[i] = binChar[(x %26gt;%26gt; (N - i)) %26amp; 1];


聽 聽 }


聽 聽 while ((*p == '0') %26amp;%26amp; (*(p + 1) != '\0')) ++p;


聽 聽 puts(p);


}





#if 0





Sample run:





Enter a base-10 integer : 42


bin: 101010





Enter a base-10 integer : 1024


bin: 10000000000





#endif

No comments:

Post a Comment