Probleml: You have four identical prizes to give away and a pool of 25 finalists
ID: 3869937 • Letter: P
Question
Probleml: You have four identical prizes to give away and a pool of 25 finalists. The finalists are assigned numbers from 1 to 25. Write a program to randomly select the numbers of 4 finalists to receive a prize. Make sure not to pick the same number twice. For example, picking finalists 3, 15, 22, and 14 would be valid but picking 3, 3, 31, and 17 would be invalid, because finalist number 3 is listed twice and 31 is not a valid finalist number. Print the numbers of the four finalists. At the beginning, ask the user to enter an integer which you pass in to the srand(...) function. Do not use srand(time(NULL) as this will generate random output which wil not pass the test cases. See the posted template on ZyBooks. coutExplanation / Answer
Code:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int validRandomNumber(int x);
int main()
{
int arr[4];
int randomNum = 0;
int i=0;
int temp = 0;
/* Taking input for srand function */
cout<<"Enter Integer for srand";
cin>>randomNum;
srand(randomNum);
int count = 1;
/* loop till we get 4 random finalists */
while(count < 5)
{
temp = rand();
/* calling a function to validate the random numbers */
int res = validRandomNumber(temp);
if(res) {
arr[i]=temp;
count++;
i++;
}
temp = 0;
}
/* displaying the array of 4 finalists */
for (int j = 0;j<4;j++) {
cout << arr[j];
cout << " ";
}
}
/* Checks if the number lies between 1 and 25 */
int validRandomNumber(int x)
{
if(x>0 && x <= 25)
return 1;
return 0;
}
output:
Enter Integer for srand 5
18 19 22 15
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.