0

How to convert binary representation to array [Solved]

How would one convert? For example : int main() { int num; cin >> num; int* x = &num; cout << x; gives output of what needs to be placed into an array. Maybe something like char* arr ? I'm kinda lost here :) To solve a Code Coach called Number of Ones it requires converting x into an array (I think... ). Can anyone suggest an approach, remembering this challenge is marked as easy and therefore shouldn't require using intermediate+ knowledge? Thank you, all suggestions welcome!

11th Nov 2022, 1:46 AM
Scott D
Scott D - avatar
8 Antworten
+ 3
Is the binary representation a string? If so, you just have to iterate over the string characters and append each corresponding value to the array. Iterate and append = loop thru string indexes and write the array using the same indexes. Corresponding value = a translation from ASCII '0' and '1' to the array values (maybe 0 and 1 integers).
11th Nov 2022, 4:00 AM
Emerson Prado
Emerson Prado - avatar
+ 3
Emerson Prado (and anyone who may read this) I think I found the answer to what I was asking although it uses bitset which I don't remember seeing in the "easy" level of lessons so far. #include <iostream> #include <bitset> using namespace std; int main() { int x; cin >> x; string str = bitset<8>(x).to_string(); cout << str; }
11th Nov 2022, 5:06 AM
Scott D
Scott D - avatar
+ 3
Looks like I was able to solve the task. For anyone interested I have linked the code, it passed the Code Coach. And if anyone sees a more "elegant" solution (easy level) I am always appreciative of tips that will help me improve. https://code.sololearn.com/cPXNlBRf7yCe/?ref=app
11th Nov 2022, 5:54 AM
Scott D
Scott D - avatar
+ 2
Good! I'd like to suggest one correction and one improvement: The loop "for (int i = 0; i <16; i++)" limits the input to 2^16-1. For higher numbers, the most significant bits are lost. Instead of 16, use the string length as limit. The array "num" and the loop "for (int x = 0; x < 1; x++)" are unnecessary - num[x] is always '1'. Just compare str[i] to '1'. If you prefer using a variable, make it a char and assign it '1'.
12th Nov 2022, 12:35 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Emerson Prado Fantastic! Works great, cuts out a few lines of code and looks cleaner. FWIW, in my first attempt I used a bitset value of 8. It passed 3 of 5 tests and failed two. I played with it a "bit" (excuse the bad pun) and realized using cout << str that if cin x was a larger number then it was returning all "ones". Setting bitset to 16 resolved that. 32 seems even safer. Thanks again! P.S. I realize there were 2 options for strlen. • to use the int method (which I did) • to instead use "for(unsigned int... str.length()" Is one more preferable than the other? Code attached for anyone interested. https://code.sololearn.com/c5oDAKhcD81S/?ref=app
12th Nov 2022, 1:39 AM
Scott D
Scott D - avatar
+ 2
The second one is a bit less readable, but saves some code and a variable
12th Nov 2022, 5:36 AM
Emerson Prado
Emerson Prado - avatar
+ 1
Emerson Prado Thank you for the reply. Here is the task. " Task: Count the number of ones in the binary representation of a given integer. Input Format: An integer. Output Format: An integer representing the count of ones in the binary representation of the input. Sample Input: 9 Sample Output: 2 " I now realize I was possibly mistaking the binary number for memory address. I am very new to programming but think I know how to solve this if I can understand how to take the given integer and turn it into a binary string or array. I'm getting more familiar with arrays but it takes some time. I don't yet know how to use C++ to convert an int to binary, perhaps I missed it in an earlier lesson.
11th Nov 2022, 4:24 AM
Scott D
Scott D - avatar
0
Emerson Prado Thanks! I'll look that over and see if I can actually apply your advice to my current code without breaking it😂
12th Nov 2022, 12:57 AM
Scott D
Scott D - avatar