Write a complete c++ program that does the following. Program must be done in th
ID: 3870444 • Letter: W
Question
Write a complete c++ program that does the following.
Program must be done in the main.
// note: please explain how you obtain the answer and the logic behind it.
1) create an array that holds 100 integers;
2) Fill the array with consecutive numbers that are divisible by 5 (in other words , the first number in the array is 5, and the next is 10, and the next is 15 and so on)
3) Shuffle the array
4) Use the random number generator to randomly select one of the numbers in an array
5) Print that number to the screen.
Explanation / Answer
#include<iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void swap (int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
void shuffleArray ( int arr[], int n )
{
srand ( time(NULL) );
for (int i = n-1; i > 0; i--) {
int j = rand() % (i+1);
swap(&arr[i], &arr[j]);
}
}
int main() {
int a[100];
for(int i=0,j=5;i<100;i++,j+=5) {
a[i]=j;
}
shuffleArray(a, 100);
srand (time(NULL));
int index = rand() % 100;
cout<<"Random Number is "<<a[index]<<endl;
return 0;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.