adding numbers of binary output

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

Join Date: Aug 2006
Posts: 13
Reputation: insamd is an unknown quantity at this point 
Solved Threads: 0
insamd insamd is offline Offline
Newbie Poster

adding numbers of binary output

 
0
  #1
Aug 24th, 2006
Hey guys,

I was wondering if anyone can help me in terms of using atoi to get my my results to add the 1's of my binary output and then join them together... eg. 110101 and 100101 is 4 and 3 = 43.
I was thinking of integer dividing the number by 10, but i am unsure of how to do that.
Here is my code:
  1.  
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <string>
  5. using namespace std;
  6. void decToBin(int number, int base);
  7. int main()
  8. {
  9. int answer;
  10. int count=0;
  11. cout<<"Enter a string: ";
  12. string name;
  13. getline(cin,name);
  14.  
  15. /* for (unsigned int i=0; i<name.size(); i++)
  16.   {
  17.   cout << name[i] << " binary (";
  18.   decToBin(name[i], 2);
  19.   cout << ")" << endl;
  20.   }
  21.   */
  22. for (unsigned int i=0; i<name.size(); i++)
  23. {
  24. decToBin(name[i],2)=answer;
  25. answer=answer/10;
  26. cout<<answer<<endl;
  27. }
  28. system("PAUSE");
  29. return 0;
  30. }
  31. void decToBin (int number, int base)
  32. {
  33. if (number>0)
  34. {
  35. decToBin(number/base, base);
  36. cout << number%base;
  37. }
  38. }
The commented part gets the numbers into binary, the other loop under it i was trying to do so decToBin became an int and could then div 10.
As you can tell i am not sure of the direction i should go, any help would be appreciated, thanks!
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: adding numbers of binary output

 
1
  #2
Aug 24th, 2006
Originally Posted by isamd
  1. decToBin(name[i],2)=answer;
This is illegal. What did you want with this line?
Last edited by andor; Aug 24th, 2006 at 5:34 am. Reason: Yust for fun (kidding added [/quote])
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: insamd is an unknown quantity at this point 
Solved Threads: 0
insamd insamd is offline Offline
Newbie Poster

Re: adding numbers of binary output

 
0
  #3
Aug 24th, 2006
i was trying to make it into an integer, because when i try to just div 10 the decToBin(name[i],2) it won't let me... thats my understanding of what i had to do. But it didn't work. So yeah thats why i am asking here, sorry limited knowledge
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: adding numbers of binary output

 
0
  #4
Aug 24th, 2006
A litle of help. In your for loop you can make a temp array wich will convert the name[i] to decimal with atoi.
  1. char tmp[2] = { 0, 0 };
  2. tmp[0] = name[i];
  3. answer = atoi(tmp);
and then join them together
Figure this out
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: adding numbers of binary output

 
0
  #5
Aug 24th, 2006
I think some major clarificarion is needed.
Originally Posted by insamd View Post
I was wondering if anyone can help me in terms of using atoi to get my my results to add the 1's of my binary output and then join them together... eg. 110101 and 100101 is 4 and 3 = 43.
Do you mean you want to convert series of 1's and 0's typed in by the user into it's octal (base 8) representation? That's what this example shows. There is nothing decimal (base 10) about it. In decimal, this value is 37, not 43.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 163
Reputation: grunge man is an unknown quantity at this point 
Solved Threads: 2
grunge man grunge man is offline Offline
Junior Poster

Re: adding numbers of binary output

 
0
  #6
Aug 24th, 2006
if im corect um u said u wanted to put together 4 and 3 to make 43 well u can do that by this math equation 4*10+3 then suplement thouthes for there bionary equivlents or whatever conversion u want. also if u wand the user to input the number you could do a*10+b. also if u wana do division lots of times it wont come out even so you have to use float on the variables?
dont know if that was any help but there u go
THE ONLY WAY TO MOVE IN THE FUTURE IS TO ABSTRACT IN THE PAST
(My interpretation of abstract, is, to change something little by little until it morphs into something new)
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: insamd is an unknown quantity at this point 
Solved Threads: 0
insamd insamd is offline Offline
Newbie Poster

Re: adding numbers of binary output

 
0
  #7
Aug 24th, 2006
sorry about my explanation... i don't wish to change into decimal, i was wanting to get the output of the binary's and add each binary's 1's...
1101110 number of 1's = 5
1001101 number of 1's = 4
1011011 number of 1's = 5

Then join those numbers together, so the number would be 545.
Sorry for the misunderstanding, my bad.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,406
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 247
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: adding numbers of binary output

 
0
  #8
Aug 24th, 2006
Some basic parts:
#include <stdio.h>

int count_ones(const char *text)
{
   int ones = 0;
   for ( ; *text; ++text )
   {
      if ( *text == '1' )
      {
         ++ones;
      }
   }
   return ones;
}

int main(void)
{
   const char *text[] = 
   {
      "1101110", "1001101", "1011011",
   };
   int total = 0;
   size_t i;
   for ( i = 0; i < sizeof text / sizeof *text; ++i )
   {
      total *= 10;
      total += count_ones(text[i]);
   }
   printf("total = %d\n", total);
   return 0;
}

/* my output
total = 545
*/
Last edited by Dave Sinkula; Aug 24th, 2006 at 11:21 pm.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: insamd is an unknown quantity at this point 
Solved Threads: 0
insamd insamd is offline Offline
Newbie Poster

Re: adding numbers of binary output

 
0
  #9
Aug 27th, 2006
thanks for your help guys appreciate it...

i am struggling to grasp the concept! but not to worry, as i was going to ask if anyone knew of some good tutorials or books that i can get to gain a better understanding.. i just can't comprehend the logic to what i need to do. So i figure i need to go back to step 1.

Thanks again guys.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 490
Reputation: Bench has a spectacular aura about Bench has a spectacular aura about Bench has a spectacular aura about 
Solved Threads: 49
Bench's Avatar
Bench Bench is offline Offline
Posting Pro in Training

Re: adding numbers of binary output

 
0
  #10
Aug 27th, 2006
That depends exactly what part you don't understand. Essentially, your problem seems to be about looping through arrays, and performing some operation on each element. If you're not comfortable with arrays, then do a google search for array tutorials, or check:
http://www.daniweb.com/tutorials/tutorial1732.html
http://www.cprogramming.com/tutorial/lesson8.html
Here's one for 'C' Style strings, which are null terminated arrays of char
http://www.cprogramming.com/tutorial/c/lesson9.html
and one for looping
http://www.cprogramming.com/tutorial/lesson3.html

You might find it handy to bite a smaller chunk off your problem, and create a simple program to solve that. for example, write a program which takes a series of 1's and 0's, and outputs the number of 1's in that series.
Last edited by Bench; Aug 27th, 2006 at 9:39 am.
¿umop apisdn upside down?
Reply With Quote Quick reply to this message  
Reply

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



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