Write a C++ program that chooses six random numbers from 1 to 74 ( including 1 a
ID: 3581340 • Letter: W
Question
Write a C++ program that chooses six random numbers from 1 to 74 ( including 1 and 74) but it must not repeat the numbers. Once a number is chosen in that set of six it cannot be chosen in that same set. For example the set of six numbers 22 3 56 12 14 3 would be in error since 3 is chosen twice. First get a working program that chooses the six random numbers for the range we want, then figure out how to prevent a double number from being selected and build that into your program. (Hint: every time you chose a number you need to check it against the previous numbers. For example n3 must be compared with n2 and n1. A loop works well for checking). Lastly the entire program should run 10 times demonstrating no repeat numbers.
Explanation / Answer
#include<iostream>
#include<conio.h>
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
using namespace std;
int main ()
{
int rand_num;
srand (time(NULL));
/* generate random number between 1 and 74: */
// generating 6 random numbers
for(int i=0;i<10;i++){
rand_num = rand() % 74 + 1;
/*
int t = array[j];
array[j] = array[i];
array[i] = t;
*/
cout<<"random Number is "<<rand_num<<endl;
}
getch();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.