decimal to binary in while loop..stuck

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Feb 2008
Posts: 20
Reputation: Crazycfk is an unknown quantity at this point 
Solved Threads: 0
Crazycfk Crazycfk is offline Offline
Newbie Poster

decimal to binary in while loop..stuck

 
0
  #1
Mar 13th, 2008
i am doing a decimal to binary conversion and i have figure out how to do the conversion i just don't know how to make it cout so that it is in reverse.... i was also told that it could done in a while loop

here is the code
  1.  
  2. 1. #include<iostream>
  3. 2. using namespace std;
  4. 3. int main(){
  5. 4.
  6. 5. int dec,olddec,remain;
  7. 6.
  8. 7. cout<<"please input a decimal to be converted to binary:"<<endl;
  9. 8. cin>>dec;
  10. 9. cout<<endl;
  11. 10.
  12. 11. while(dec>0){
  13. 12.
  14. 13. olddec=dec;
  15. 14.
  16. 15. remain = dec % 2;
  17. 16.
  18. 17. dec /=2;
  19. 18.
  20. 19. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl;
  21. 20. }
  22. 21.
  23. 22. return 0;
  24. 23. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: decimal to binary in while loop..stuck

 
0
  #2
Mar 14th, 2008
Originally Posted by Crazycfk View Post
i am doing a decimal to binary conversion and i have figure out how to do the conversion i just don't know how to make it cout so that it is in reverse.... i was also told that it could done in a while loop

here is the code
  1.  
  2. 1. #include<iostream>
  3. 2. using namespace std;
  4. 3. int main(){
  5. 4.
  6. 5. int dec,olddec,remain;
  7. 6.
  8. 7. cout<<"please input a decimal to be converted to binary:"<<endl;
  9. 8. cin>>dec;
  10. 9. cout<<endl;
  11. 10.
  12. 11. while(dec>0){
  13. 12.
  14. 13. olddec=dec;
  15. 14.
  16. 15. remain = dec % 2;
  17. 16.
  18. 17. dec /=2;
  19. 18.
  20. 19. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl;
  21. 20. }
  22. 21.
  23. 22. return 0;
  24. 23. }

Well it looks to me like you are calculating the digits in the reverse order that you want to display them. remainder is the digit. You can't just print remainder in the order that you calculate it because then the digits are in reverse order, and currently remainder is writing over itself. Currently you HAVE to write over remainder before displaying because you have no place to store the old remainder.

Solution: use an array that sets aside an integer for every digit. Since you are using integers and integers have 32 bits, you might as well make an array called digit that would hold 32 integers. So say the user typed in the number 18. In binary, that is:

10010

Working right to left, by the end of your calculation, you'd want your digit values to be this for the above number:
  1. digit[0] = 0
  2. digit[1] = 1
  3. digit[2] = 0
  4. digit[3] = 0
  5. digit[4] = 1
You'd display the results in this order: digit[4], then digit[3], then digit[2], then digit[1], then digit[0]. The result is this:
10010

Make sure you initialize all of the digits to 0 first or have some way of keeping track of the number of digits in the number.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 163
Reputation: atish00 is an unknown quantity at this point 
Solved Threads: 4
atish00 atish00 is offline Offline
Junior Poster

Re: decimal to binary in while loop..stuck

 
0
  #3
Mar 14th, 2008
keep on dividing number till it gives true value.

while( n%10 => 0)

{
conversion code
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 275
Reputation: dougy83 is on a distinguished road 
Solved Threads: 45
dougy83 dougy83 is offline Offline
Posting Whiz in Training

Re: decimal to binary in while loop..stuck

 
0
  #4
Mar 14th, 2008
I believe a common way to get around the reversal is to use a string to store the output:
  1. void binToString(int x){
  2. string binStr;
  3.  
  4. while(x > 0){
  5. if(x & 1) // same as if(x % 2)
  6. binStr = "1" + binStr;
  7. else
  8. binStr = "0" + binStr;
  9.  
  10. x >>= 1; // same as x = x / 2
  11. }
  12.  
  13. cout << binStr << endl;
  14. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 20
Reputation: Crazycfk is an unknown quantity at this point 
Solved Threads: 0
Crazycfk Crazycfk is offline Offline
Newbie Poster

Re: decimal to binary in while loop..stuck

 
0
  #5
Mar 15th, 2008
ok i am really bad at strings since my professor barely explained how to use them so i am trying to avoid for this project i have to do till i can try it with simpler programs;
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones

  1. #include<iostream>
  2. using namespace std;
  3. //int dec(int []); // for after i get program to run i want to turn it into function
  4.  
  5. int main(){
  6.  
  7. int dec,olddec,remain,i,p[50];
  8. cout<<"please input a decimal to be converted to binary:"<<endl;
  9. cin>>dec;
  10. cout<<endl;
  11.  
  12. while(dec > 0){
  13. i = 0;
  14.  
  15. olddec = dec;
  16.  
  17. remain = dec % 2;
  18.  
  19. p[i]=remain;
  20.  
  21. i++;
  22.  
  23. dec /=2;
  24.  
  25. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl;
  26. }
  27. for (;i>=0;i--){
  28. cout<<p[i];
  29. }
  30. }
Last edited by Crazycfk; Mar 15th, 2008 at 3:28 pm. Reason: clarifying unclear statements
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: decimal to binary in while loop..stuck

 
0
  #6
Mar 15th, 2008
Originally Posted by Crazycfk View Post
ok i am really bad at strings since my professor barely explained how to use them so i am trying to avoid for this project i have to do till i can try it with simpler programs;
i tried an array but it does not seem to want to output the numbers when it run it gives a - number that are not in 0's and ones
  1. #include<iostream>
  2. using namespace std;
  3. //int dec(int []); // for after i get program to run i want to turn it into function
  4.  
  5. int main(){
  6.  
  7. int dec,olddec,remain,i,p[50];
  8. cout<<"please input a decimal to be converted to binary:"<<endl;
  9. cin>>dec;
  10. cout<<endl;
  11.  
  12. while(dec > 0){
  13. i = 0;
  14.  
  15. olddec = dec;
  16.  
  17. remain = dec % 2;
  18.  
  19. p[i]=remain;
  20.  
  21. i++;
  22.  
  23. dec /=2;
  24.  
  25. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl;
  26. }
  27. for (;i>=0;i--){
  28. cout<<p[i];
  29. }
  30. }

Two problems, both involving i. Problem 1, you are initializing i to 0 every trip through the loop in line 13. i needs to be initialized once, and before the loop. You are thus rewriting over old calculations. Two, i is too large when you get to your for-loop in line 27. If a number has 5 digits, you are going through that loop 6 times. You want to go through it 5 times, once per digit. The nonsense digits that you see at the beginning of the number is that extra integer that you do not want to display, but that you DO display in your program. You did not initialize your array to 0, so that memory location has not been touched by your program. If the number 45 was in there before the program started, that's what will be outputted, in addition to the REAL digits.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 20
Reputation: Crazycfk is an unknown quantity at this point 
Solved Threads: 0
Crazycfk Crazycfk is offline Offline
Newbie Poster

Re: decimal to binary in while loop..stuck

 
0
  #7
Mar 16th, 2008
Eureka...!!!!!!!!Thank you all for the help and the little bits of knowledge that i have gained due to your efforts......

this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number

  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.  
  5. int dec,olddec,remain,i,p[50];
  6. cout<<"please input a decimal to be converted to binary:"<<endl;
  7. cin>>dec;
  8. cout<<endl;
  9.  
  10. i=0;
  11.  
  12. while(dec > 0){
  13.  
  14. olddec = dec;
  15.  
  16. remain = dec % 2;
  17.  
  18. p[0+i]=remain;
  19.  
  20. i++;
  21.  
  22. dec /=2;
  23.  
  24. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl;
  25. }
  26. for (;i-1>=0;i--){
  27. cout<<p[i-1];
  28. }
  29. cout<<endl;
  30. return 0;
  31. }
Last edited by Crazycfk; Mar 16th, 2008 at 2:47 am. Reason: found problem within program so had to change the claim of it being finished
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: decimal to binary in while loop..stuck

 
0
  #8
Mar 16th, 2008
Originally Posted by Crazycfk View Post
Eureka...!!!!!!!!Thank you all for the help and the little bits of knowledge that i have gained due to your efforts......

this is the finished product... seems i was wrong if the input is 0 the number becomes a large - number
  1. #include <iostream>
  2. using namespace std;
  3. int main(){
  4.  
  5. int dec,olddec,remain,i,p[50];
  6. cout<<"please input a decimal to be converted to binary:"<<endl;
  7. cin>>dec;
  8. cout<<endl;
  9.  
  10. i=0;
  11.  
  12. if (dec == 0) // trivial case. Just display 0.
  13. cout << 0;
  14. else // start of else
  15. {
  16.  
  17. while(dec > 0){ // start of while
  18.  
  19. olddec = dec;
  20.  
  21. remain = dec % 2;
  22.  
  23. p[0+i]=remain;
  24.  
  25. i++;
  26.  
  27. dec /=2;
  28.  
  29. cout<<olddec<<"/2 = "<<dec<<" "<<"remainder = "<<remain<<endl;
  30. } // end of while
  31. for (;i-1>=0;i--){
  32. cout<<p[i-1];
  33. }
  34. } // end of "else"
  35. cout<<endl;
  36. return 0;
  37. }

Not a huge problem. If the number is 0, there's nothing to calculate, so I would just short-circuit the problem with an if-else statement. If the number entered equals 0, just display 0. If it's larger than 0, go through your while loop. I added an if-else loop above. The bulk of your original code is in the "else" part.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC