Hey guys!
Im busy with a project that uses vectors, and random number generators to convert integers into binary.
Im very new to coding in C++. Currently my program is outputting the numbers i need to convert into binary, but it is not outputting my calculation and ultimately, the binary equivalent. Please could somebody guide me in the right direction here!
Im currently coding using CodeBlocks.
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
int randomNum;
srand(time(NULL)); // This is used for random seeding of random numbers.
int randomVectorSize = 1+ rand()%5; //This generates a random number between 1 and 6
vector<int> numbersToBeConverted;// This creates the vector where our randomly generated decimal numbers will be stored
for(int i= 0; i<randomVectorSize; i++) //This code generates a vector with a random size between 1 and 6
{
int randomNum = rand()%10; // This generates a random number between 0 and 10
numbersToBeConverted.push_back(randomNum); // This stores the random number generated in the vector
cout<<numbersToBeConverted[i] <<" ";
}
vector<int>convert;
for(int a= 0; a<randomVectorSize; a++)
{
int remainder = randomNum %2;
if (randomNum <= 1){
cout<<randomNum<<"";
}
elseif (remainder!=0 || remainder!=1)
{
convert.push_back(remainder);
cout << convert[a] <<" ";
}
}
return 0;
}
Oh, if you're required to use std::vector then you should. I didn't know you had such a requirement. In that case, ignore everything I have said so far.
Do you know how to convert decimal to binary by hand? That is, can you do it in your head or on paper? If not, there is also a trick you can use with the bitwise shift operator and the bitwise and operator. Do you know if you're required to do it one way or the other?
I know how to convert it by hand, just not sure how to get is to display using code. decimal/2 save 0 or 1 as the remainder till decimal = 0.
I think we would be required to use the easiest way, not sure if the lecturer would be happy without the calculation. But since he hasn't given us an example of what the calculation should look like...I'm not really sure how to write it in code!
2 \ 19
= 9 r. 1
= 4 r. 1
= 2 r. 0
= 1 r. 0
= 0 r. 1
Read the remainders from the bottom up :
decimal 19 = 10011 binary
It is difficult to show nicely here, but the process is just a "nest " of divisions - divide 19 by 2 to get 9, then divide the 9 by 2 to get 4, and so on.
Just not sure how to display all "remainders"
> Just not sure how to display all "remainders"
store them.
1 2 3 4 5 6 7 8 9
std::vector<int> result;
while( decimal ){
remainder = decimal%2;
result.push_back( remainder );
decimal /= 2;
}
//taking decimal=19 as an example
//result = {1,1,0,0,1}
//you can "read" the binary number from right to left
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
int randomNum;
srand(time(NULL)); // This is used for random seeding of random numbers.
int randomVectorSize = 1+ rand()%5; //This generates a random number between 1 and 6
vector<int> numbersToBeConverted;// This creates the vector where our randomly generated decimal numbers will be stored
for(int i= 0; i<randomVectorSize; i++) //This code generates a vector with a random size between 1 and 6
{
int randomNum = rand()%10; // This generates a random number between 0 and 10
numbersToBeConverted.push_back(randomNum); // This stores the random number generated in the vector
cout<<numbersToBeConverted[i] <<" ";
}
vector<int>convert;
for(int a= 0; a<randomVectorSize; a++)
{
int remainder = randomNum %2;
if (randomNum <= 1){
cout<<randomNum<<"";
}
while( randomNum ){
remainder = randomNum%2;
convert.push_back( remainder );
randomNum /= 2;
cout << convert[a]<<" ";
}
}
return 0;
}
indent your code properly.
note that `randomNum' in line 10 is a different variable that `randomNum' at line 16.
explain the purpose of the loop at line 24.
explain the purpose of the condition in line 27.
I would recommend you to make a function std::vector<int> decimal_to_binary(int);
Ah! I didnt notice the first randomNum!
I'm not too sure what my thinking was in line 24.
In line 27 I wanted the output of any random number of 1 or 0 to be displayed as such when the application ran (0 as a binary = 0; 1 as binary = 1).
This assignment has been an absolute nightmare for me, partly because vectors were not explained very well to me by the programmer. I have, however, spent the last few hours getting more familiar with them.
What is the purpose of the second (int) in your suggested function?
Apologies for my ignorance.
Here is the first stage of my cleaning up of the chaos.
Any idea why my codes output is Integer.....random numbers? Why is it not converting to binary properly?
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
srand(time(NULL)); // This is used for random seeding of random numbers.
int randomVectorSize = 1+ rand()%5; //This generates a random number between 1 and 6
vector<int> numbersToBeConverted;// This creates the vector where our randomly generated decimal numbers will be stored
for(int i= 0; i<randomVectorSize; i++) //This code generates a vector with a random size between 1 and 6
{
int randomNum = rand()%10; // This generates a random number between 0 and 10
numbersToBeConverted.push_back(randomNum); // This stores the random number generated in the vector
vector<int>convert;
int remainder = randomNum %2;
while(randomNum)
{
remainder = randomNum%2;
convert.push_back(remainder);
randomNum /= 2;
cout<<numbersToBeConverted[i] <<" ..... "<< cout << convert[i]<<" ";
}
}
return 0;
}
> What is the purpose of the second (int) in your suggested function?
that's the parameter, a function that takes an integer and returns a vector
so you can call it like std::vector<int> binary = decimal_to_binary(42);
Stop trying to do all at once.
I would suggest you to make your program work on only one integer. Later you could wrap it in the `generate a lot of numbers' code.
Your output is inside the `convert to binary' code. ¿what are you outputting then?
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
usingnamespace std;
int main()
{
srand(time(NULL)); // This is used for random seeding of random numbers.
int randomVectorSize = 1+ rand()%5; //This generates a random number between 1 and 6
vector<int> numbersToBeConverted;// This creates the vector where our randomly generated decimal numbers will be stored
for(int i= 0; i<randomVectorSize; i++) //This code generates a vector with a random size between 1 and 6
{
int randomNum = rand()%10; // This generates a random number between 0 and 10
numbersToBeConverted.push_back(randomNum); // This stores the random number generated in the vector
}
int binary;
vector<int>bin;
for(int j = 0; j < numbersToBeConverted.size(); j++)
{
cout<< numbersToBeConverted[j] <<" ";
}
while (numbersToBeConverted.size()>0)
{
binary = (numbersToBeConverted.size()%2);
numbersToBeConverted.size() /=2;
bin.push_back(binary);
}
for(int a = 0; a < bin.size(); a++)
{
cout<< bin[a] <<" ";
}
return 0;
}