Question: Hi, I have to write binary_vector function that makes the following program in main work. - The binary_vector program takes a integer and turn it

Hi, I have to write binary_vector function that makes the following program in main work.

- The binary_vector program takes a integer and turn it in to a binary number recursively

- The binary_vector program has to have 8 bits. In order words, 4 in decimal equals to 00000100 in binary.

int main()

{

vector integers;

vector v;

initialize(integers);

cout << endl;

cout << "vector of integers" << endl;

print_vector(integers);

cout << integers;

cout << endl;

sort(integers.begin(), integers.end());

cout << "sorted vectors of integers" << endl;

print_vector(integers);

cout << integers;

cout << endl;

for (int i = 0; i < SIZE; ++i)

{

cout << setw(3) << "integers[" << setw(2) << i;

cout << "] = " << setw(2) << integers[i] << " -- ";

cout << setw(3) << integers[i] << " ";

print_vector(binary_vector(integers[i])); // This shouldn't be changed

}

cout << endl;

return 0;

}

And here is what I have for binary_vector:

bool binary_vector(int n)//returns the integer to a 8 bits binary number

{

int decimalNumber = 0, i = 0, remainder;

while (n!=0)

{

remainder = n % 10;

n /= 10;

decimalNumber += remainder * pow(2,i);

++i;

}

return decimalNumber;

}

This is only a portion of my whole program, if you need other function, you can say so and I will provide them.

My whole program:

#include

#include

#include

#include

#include

#include

using namespace std;

unsigned int seed = (unsigned int)time(NULL);

const int SIZE = 10;

const int VARIABLES = 8;

template

void print_vector(vector &vector);

void initialize(vector &);

double random (unsigned int &);

ostream &operator << (ostream &, vector);

void output(ostream &, vector );

bool binary_vector(int);

int main()

{

vector integers;

vector v;

initialize(integers);

cout << endl;

cout << "vector of integers" << endl;

print_vector(integers);

cout << integers;

cout << endl;

sort(integers.begin(), integers.end());

cout << "sorted vectors of integers" << endl;

print_vector(integers);

cout << integers;

cout << endl;

for (int i = 0; i < SIZE; ++i)

{

cout << setw(3) << "integers[" << setw(2) << i;

cout << "] = " << setw(2) << integers[i] << " -- ";

cout << setw(3) << integers[i] << " ";

print_vector(binary_vector(integers[i]));

}

cout << endl;

return 0;

}

template

void print_vector(vector &vector)

{

for (int i = 0; i < vector.size(); ++i)

cout << setw(3) << vector[i];

cout << endl;

}

void initialize(vector &vector)

{

for (int i = 0; i < SIZE; ++i)

vector.push_back(int( 100 * random (seed)));

}

ostream &operator << (ostream &out, vector v)

{

output(out,v);

return(out);

}

void output(ostream &out, vector v)

{

cout << setprecision(5) << fixed;

for(int i = 0; i < v.size(); ++i)

cout << setw(3) << v[i];

cout << endl;

}

bool binary_vector(int n)//returns the integer to a 8 bits binary number

{

int decimalNumber = 0, i = 0, remainder;

while (n!=0)

{

remainder = n % 10;

n /= 10;

decimalNumber += remainder * pow(2,i);

++i;

}

return decimalNumber;

}

double random (unsigned int &seed)

{

const int MODULUS = 15749;

const int MULTIPLIER = 69069;

const int INCREMENT = 1;

seed = ((MULTIPLIER * seed) + INCREMENT) % MODULUS;

return double (seed) / double (MODULUS);

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!