Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions

Question

1. What causes musculoskeletal pain?

Answered: 1 week ago

Question

13. You always should try to make a good first impression.

Answered: 1 week ago