| | |
adding numbers of binary output
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
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:
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!
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:
C Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <string> using namespace std; void decToBin(int number, int base); int main() { int answer; int count=0; cout<<"Enter a string: "; string name; getline(cin,name); /* for (unsigned int i=0; i<name.size(); i++) { cout << name[i] << " binary ("; decToBin(name[i], 2); cout << ")" << endl; } */ for (unsigned int i=0; i<name.size(); i++) { decToBin(name[i],2)=answer; answer=answer/10; cout<<answer<<endl; } system("PAUSE"); return 0; } void decToBin (int number, int base) { if (number>0) { decToBin(number/base, base); cout << number%base; } }
As you can tell i am not sure of the direction i should go, any help would be appreciated, thanks!
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])
A litle of help. In your for loop you can make a temp array wich will convert the name[i] to decimal with atoi.
Figure this out
C Syntax (Toggle Plain Text)
char tmp[2] = { 0, 0 }; tmp[0] = name[i]; answer = atoi(tmp);
•
•
•
•
and then join them together
I think some major clarificarion is needed.
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.
•
•
•
•
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.
•
•
Join Date: Mar 2006
Posts: 163
Reputation:
Solved Threads: 2
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
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)
(My interpretation of abstract, is, to change something little by little until it morphs into something new)
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
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
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
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.
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.
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.
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? ![]() |
Other Threads in the C Forum
- Previous Thread: how to know if a directory exists in windows?
- Next Thread: File descriptors
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






