Question: Data Structure C++ language 1. Write and test a function that fills an int vector of size m with unique random numbers between 1 and

Data Structure C++ language 1. Write and test a function that fills an int vector of size m with unique random numbers between 1 and n. Make sure that m is not greater than n.

The function prototype is

vector uniqueRandomFill(int m, int n)

Unique randomFill, without the unique looks like

#include "stdafx.h"

#include

#include

#include

#include

using namespace std;

vector uniqueRandomFill(int m, int n);

uniform_int_distribution *uid;

default_random_engine *dre;

int main()

{

dre = new default_random_engine(time(nullptr));

for (int i = 1; i <= 9; i++)

{

uid = new uniform_int_distribution(1, 9);

vector v = uniqueRandomFill(i, 9);

copy(v.begin(), v.end(), ostream_iterator(cout));

cout << endl;

}

return 0;

}

vector uniqueRandomFill(int m, int n)

{

vector v(m);

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

v[i] = (*uid)(*dre);

return v;

}

2. Write a function that compares 2 int vectors. The function returns true if at least k of the vectors elements equal one another.

The function prototype is

bool kEqual(const vector& v1, const vector& v2, int k)

Make sure the functions parameters make sense. For example, k cannot be greater than a vectors size. Note: equal elements do not have to appear at the same position in the vectors.

For example,

Vector v {1,2,3,4} equals vector w {4,3,2,1} when k = 4

Vector v {1,7,3,4} equals vector w {4,3,2,1} when k = 3

Vector v {1,7,3,4} equals vector w {4,3,2,6} when k = 2

Vector v {1,7,3,4} equals vector w {4,3,2,6} when k = 2

Vector v {1,7,3,4} equals vector w {8,3,2,6} when k = 1

Vector v {1,7,3,4} equals vector w {9,8,2,6} when k = 0

3. Using uniqueRandomFill, write a function that generates unique random vectors until a generated vector equals the functions vector parameter. equal means that the parameter vector contains the same elements as a generated vector. The function returns the number of vectors it generated until a match appeared. The functions prototype is

int gamble(const vector& original, int m, int n)

where m and n have the same meaning as uniqueRandomFills m and n.

4.Test the function for m = 1 to 9 and let n = 9. Note n = 9 satisfies the condition that m <= n.

Use the following loop to test your function.

for i = 1 to 9

{

vectorv = uniqueRandoFill(i, 9);

cout << i << << gamble(v, i, 9);

}

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!