Converting Integers to Binary using vectors and random number generators. Need some guidance!

Pages: 12
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.

Thanks in advance :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace 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<<"";

    }

    else if (remainder!=0 || remainder!=1)
    {
   convert.push_back(remainder);

    cout << convert[a] <<" ";
    }
    }

return 0;
}
Last edited on
Why do you even store the numbers at all? You can covert them as soon as you generate them ;)
I would've done exactly that! :)
but the end result needs to be like this

Integers====Binary
1=========1
6=========110
4=========100
etc.

because I've only been programming in C++ for a week now, I'm not even sure how to orientate the output as above.
Last edited on
Can anybody lead me in the right direction here? :)
Since the binary number is on the same line as the decimal number, You definitely don't need to store the numbers at all.

As a first step, try reworking your code to eliminate storing the numbers.
OK! :)
The task was to test that we could effectively use vectors, I was maybe over-complicating things!

Could you perhaps help me out with the conversion from decimal - Binary?
Im not sure how to put it in code!
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?
Last edited on
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!
Last edited on
> decimal/2 - 0-1 as the remainder till decimal = 0.
1
2
3
4
while( decimal ){
   remainder = decimal%2;
   decimal /= 2;
}
you need to explain it better, ¿what do you do with the remainder?
Take decimal 19 as an example.

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 
Ah I see!
Is there a way to get them to display separately?

Integers====Binary
1=========1
6=========110
4=========100
etc.

I seem to be getting a strange output of a lot of "0"s.
I've been trying to figure this out for a while now...very frustrating stuff.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace 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;
}
Last edited on
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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace 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;
}
Last edited on
> 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?

Also, you've got cout << /**/ << cout;
OK, here is a simple program to convert int to binary(Although it displays binary backwards...you have to read it from right to left).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
    vector<int>bin;

    int a = 2;
    int binary;
    
    
    
    
while (a>0)
{
    binary = (a%2);
    a/=2;
    bin.push_back(binary);
}
    for(int j = 0; j < bin.size(); j++)
    {
        cout<< bin[j] <<" ";
    }




    return 0;
}


trying to simplify my original code to do the same...no luck yet tho!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace 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;
}

Last edited on
Im getting an error at line 28 saying "1value required as left operand of assignment.

How could I go about solving this?

I think that using numbersToBeConverted.size() is not correct, however im not sure how to call up the random numbers stored in numbersToBeConverted
The error says Lvalue, not 1value. On line 28, why are you trying to divide the size of the vector by two?
Just do it the usual way and then reverse it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int number;
    vector<bool> bin;
    cin>>number;
    while(number!=0)
    {
        bin.push_back(number%2);
        number/=2;
    }
    reverse(bin.begin(),bin.end());
    for (vector<bool>::iterator it=bin.begin();it!=bin.end(); ++it)
        cout<<*it;
    return 0;
}
>> why are you trying to divide the size of the vector by two?

What I was trying to do was to divide the integers stored in NumbersToBeConverted by 2, in order to get the binary value after %2.

Could you suggest a way to do this?
Pages: 12