help with codes

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2004
Posts: 13
Reputation: missy is an unknown quantity at this point 
Solved Threads: 0
missy missy is offline Offline
Newbie Poster

help with codes

 
0
  #1
Nov 14th, 2004
how do you convert to binary.

i have to write a program that will prompt the user to enter an unlimited(user defined) number positive intergers, the program will convert each positive interger(base 10 value) into its equivalent binary representation and displat each conversation. to exit the program, the user is to ente a negative interger.

the largest enterger value to be used is 15, i also have to create an array of SIZE 4(all array position are to begin with the value '0' in each position).
prompt the user to enter an interger between 0 and 15;
convert the interger to its equivalent binary value;
display the new binary value.

can someone please help me with this and you show me an example.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: help with codes

 
0
  #2
Nov 15th, 2004
>how do you convert to binary.
Try using printing 1 if val % 2 is not 0 and 1 otherwise, then divide val by 2 and repeat until val is 0. This should give you a good start toward the solution.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 141
Reputation: meabed is on a distinguished road 
Solved Threads: 3
Team Colleague
meabed's Avatar
meabed meabed is offline Offline
Junior Poster

Re: help with codes

 
0
  #3
Nov 19th, 2004
the program displays the binary code for integers up to 32,768 in

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main( int argc, char *argv[] )
  4. {
  5. int arg1, x;
  6.  
  7. if( argc == 1 )
  8. {
  9. puts( "BINARY num = displays num in binary form." );
  10. exit( 1 );
  11. }
  12.  
  13. arg1 = atoi( argv[1] );
  14. printf( "INTEGER: %i\n", arg1 );
  15. printf( " HEX: %x\n", arg1 );
  16. printf( " BINARY:" );
  17. for( x = 15; x > 7; x-- )
  18. printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
  19.  
  20. printf( " " );
  21.  
  22. for( x = 7; x > -1; x-- )
  23. printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
  24.  
  25. puts( "" );
  26. puts( " 16 bit:15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0" );
  27. puts( " 8 bit: 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0" );
  28. puts( " Hex: |----4---| |----3---| |----2---| |----1---|" );
  29. puts( " Sum: 3 1 " );
  30. puts( " 2 6 8 4 2 1 " );
  31. puts( " 7 3 1 0 0 0 5 2 1 " );
  32. puts( " 6 8 9 9 4 2 1 5 2 6 3 1 " );
  33. puts( " 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 1" );
  34. exit( 0 );
  35. }
Last edited by alc6379; Nov 19th, 2004 at 11:43 am.
Real Eyes Realize Real Lies
My Resume
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,829
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 750
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Senior Bitch

Re: help with codes

 
0
  #4
Nov 19th, 2004
Originally Posted by meabed
the program displays the binary code for integers up to 32,768 in

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main( int argc, char *argv[] )
  4. {
  5. int arg1, x;
  6.  
  7. if( argc == 1 )
  8. {
  9. puts( "BINARY num = displays num in binary form." );
  10. exit( 1 );
  11. }
  12.  
  13. arg1 = atoi( argv[1] );
  14. printf( "INTEGER: %i\n", arg1 );
  15. printf( " HEX: %x\n", arg1 );
  16. printf( " BINARY:" );
  17. for( x = 15; x > 7; x-- )
  18. printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
  19.  
  20. printf( " " );
  21.  
  22. for( x = 7; x > -1; x-- )
  23. printf( " %i ", (arg1 & 1 << x ) > 0 ? 1 : 0 );
  24.  
  25. puts( "" );
  26. puts( " 16 bit:15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0" );
  27. puts( " 8 bit: 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0" );
  28. puts( " Hex: |----4---| |----3---| |----2---| |----1---|" );
  29. puts( " Sum: 3 1 " );
  30. puts( " 2 6 8 4 2 1 " );
  31. puts( " 7 3 1 0 0 0 5 2 1 " );
  32. puts( " 6 8 9 9 4 2 1 5 2 6 3 1 " );
  33. puts( " 8 4 2 6 8 4 2 6 8 4 2 6 8 4 2 1" );
  34. exit( 0 );
  35. }
Yes, yes, do all of the work for the OP so that she doesn't need to think. Maybe the steady decline of the general public's IQ is your fault because of spoonfeeding. Do you think that writing bad code that does something similar to the homework problem makes you look any smarter? If so then you're wrong, it just makes you look gullible and ignorant.
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC