Can somebody please help "tidy up" my program?

Hey guys,

As stated in the title, could somebody help me tidy up my program?

The question for this task was:
Using vectors create a program that converts randomly generated
integer numbers between 0 and 10 to binary and displays the converted numbers.
Sample Output:
Decimal Binary
1 1
2 10
3 11

Currently my code is outputting the random integers and binary numbers... but they're all jumbled up and not spaced apart...the binary numbers are also being displayed backwards (you have to read them from right to left).

Could somebody help me sort these problems out?
Here is my program.

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
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>

using namespace std;

int main()
{
cout<<"Converting decimal to binary"<<endl<<endl;
    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<<endl<< numbersToBeConverted[j] <<"   "<<endl;
    }

    while (randomNum>0)
{
    binary = (randomNum%2);
    randomNum /= 2;
    bin.push_back(binary);

}
   for(int a = 0; a < bin.size(); a++)
    {
        cout<< bin[a] <<"   ";

    }
}
    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iomanip>

int binary( int n )
{
    if( n == 0 ) return 0 ;
    else return binary(n/2) * 10 + n%2;
}

int main()
{
    std::cout<<"Converting decimal to binary\n\n" ;
    std::srand( std::time(nullptr) ) ;

    const std::size_t sz = 1 + std::rand()%5 ;
    std::vector<int> numbers(sz) ;
    for( int& v : numbers ) v = rand()%10;

    for( int n : numbers )
        std::cout << n << ' ' << std::setw(4) << std::setfill('0') << binary(n) << '\n' ;
}

http://coliru.stacked-crooked.com/a/07c14d94946d4315
Last edited on
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>
#include <algorithm>

int main()
{
    std::cout<<"Converting decimal to binary\n\n" ;
    std::srand( std::time(nullptr) ) ;

    const std::size_t n = 1 + std::rand()%5 ;

    for( std::size_t i = 0 ; i < n ; ++i )
    {
        int number = std::rand()%10 ;
        std::cout << number << ' ' ;

        std::vector<int> binary ;

        while( number > 0 )
        {
            binary.push_back( number%2 ) ;
            number /= 2 ;
        }

        binary.resize(4) ;
        std::reverse( binary.begin(), binary.end() ) ;

        for( int b : binary ) std::cout << b ;
        std::cout << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/c4c1eec9bce5877d
Last edited on
Topic archived. No new replies allowed.