Originally Posted by quzah
??Code:char number[] = "11110000";
was that ment to replace int number[] = {1,1,1,1,0,0,0,0}; array ?
Originally Posted by quzah
??Code:char number[] = "11110000";
was that ment to replace int number[] = {1,1,1,1,0,0,0,0}; array ?
Don't tell me you don't know how to do some basic subtraction.
Quzah.
Hope is the first step on the road to disappointment.
No need for subtraction with such a null terminated string, but from the initial post it looks like the input is supposed to be some integer, not a string containing a representation of an integer in binary.
Which is why we use subtraction...
Hint: - '0'
Quzah.
Hope is the first step on the road to disappointment.
That is going to be rather redundant, in my opinion (as I stated in my previous post). One can simply count the '1' chars directly from the null terminated string. On the other hand, if you are provided with an integer, such subtraction will not work, since we cannot subtract a char from a string.Hint: - '0'
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Faster and more elegant.Code:remainder = number%2; // == "remainder = number & 1"
Why? Use a loop. While you're at it put counting code into your loop and you would have solved your problem.Code:void binary(int number) { //... code ... binary(number >> 1); }
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
So, you don't got it? Dissect this bit of probably non-portable code:Originally Posted by mburtCode:#include <iostream> #include <string> long binaryCount(long number) { number = (number & 0x55555555) + ((number & 0xaaaaaaaa) >> 1); number = (number & 0x33333333) + ((number & 0xcccccccc) >> 2); number = (number & 0x0f0f0f0f) + ((number & 0xf0f0f0f0) >> 4); number = (number & 0x00ff00ff) + ((number & 0xff00ff00) >> 8); return (number & 0x0000ffff) + ((number & 0xffff0000) >> 16); } int main() { std::string input = "Awesome"; long finalCount = 0; for(int i = 0; input[i]; i++) finalCount += binaryCount(input[i]); std::cout << finalCount << std::endl; return 0; }
If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein
Sure it's redundant. He didn't ask for the best solution, Dave's already posted a link to those. He asked how to initialize it without putting , between them. Asked. Answered.Originally Posted by laserlight
Quzah.
Hope is the first step on the road to disappointment.
>im trying to figure out how to count the 1's in the binary bytes outputted.
Code:#include <bitset> . . for ( int i=0; i<(int)name.size(); i++ ) { cout << "1's in " << name[i] << ": " << std::bitset<8>(name[i]).count() << endl; }