943,982 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 4731
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 24th, 2006
0

adding numbers of binary output

Expand Post »
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!
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006
Aug 24th, 2006
1

Re: adding numbers of binary output

Quote 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])
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Aug 24th, 2006
0

Re: adding numbers of binary output

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
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006
Aug 24th, 2006
0

Re: adding numbers of binary output

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);
Quote ...
and then join them together
Figure this out
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Aug 24th, 2006
0

Re: adding numbers of binary output

I think some major clarificarion is needed.
Click to Expand / Collapse  Quote originally posted by insamd ...
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.
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is online now Online
7,747 posts
since May 2006
Aug 24th, 2006
0

Re: adding numbers of binary output

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
Reputation Points: 10
Solved Threads: 2
Junior Poster
grunge man is offline Offline
169 posts
since Mar 2006
Aug 24th, 2006
0

Re: adding numbers of binary output

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.
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006
Aug 24th, 2006
0

Re: adding numbers of binary output

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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 27th, 2006
0

Re: adding numbers of binary output

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.
Reputation Points: 16
Solved Threads: 0
Newbie Poster
insamd is offline Offline
13 posts
since Aug 2006
Aug 27th, 2006
0

Re: adding numbers of binary output

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.
Reputation Points: 307
Solved Threads: 62
Posting Pro
Bench is offline Offline
565 posts
since Feb 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: how to know if a directory exists in windows?
Next Thread in C Forum Timeline: File descriptors





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC