please do this in C++ and follow all directions. Problem1: You have four identic
ID: 3889758 • Letter: P
Question
please do this in C++ and follow all directions.
Explanation / Answer
c++ code:
#include<bits/stdc++.h>
using namespace std;
//function check if new_number is allready generated or not?
bool validRandomNumber(int new_number, vector<int> finalists)
{
if( find(finalists.begin(), finalists.end(), new_number ) == finalists.end())
{
return true;
}
else
{
return false;
}
}
int main()
{
cout << "Enter integer for srand" << endl;
int randonNum = 0;
cin >> randonNum;
int valid_numbers = 0;
vector<int> finalists;
srand(randonNum);
while(valid_numbers != 4)
{
int new_number = rand()%25 + 1; //generate new number
if(validRandomNumber(new_number,finalists)) //check if new_number is allready generated or not?
{
valid_numbers += 1;
finalists.push_back(new_number);
}
}
//print all finalists
cout << "All the finalists are: ";
for (int i = 0; i < finalists.size(); ++i)
{
cout << finalists[i] << endl ;
}
return 0;
}
Sample Output:
1)
Enter integer for srand
1
All the finalists are:
17
18
10
1
2)
Enter integer for srand
2
All the finalists are:
21
17
24
10
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.