In: Computer Science
Create a C++ file with given instructions below-
Analyze random numbers
Create two arrays, one int one double. They should have 10 elements each
Ask the user how many random numbers to generate. Then generate that many random numbers between 1 and 10. Use a for loop. Keep track of how many of each number in the range was generated (1 to 10) in the int array. Be sure to initialize the array to all 0's to start.
Then, send both arrays off to a function. This function will calculate the percentage of each number. For example: if you generated 200 numbers, and 3 was generated 24 times, it would be 12%. Store the percentages in the double array. You will also need to send the amount of numbers generated to this function, also.
Output the count and percentages in another function.
Note: Run this with 10, 100, 1000, and 10,000 numbers. Do this in separate runs, no need to code it to do it. Do you see a trend in the percentages? (No need to turn in your answer)
#include <stdlib.h> /* srand, rand
*/
#include <time.h> /* time
*/
#include<iostream>
#include<iomanip>
using namespace std;
void percentageCalculator(int num[],double perc[],int
total);
void printPercentage(int num[],double perc[]);
int main(){
srand (time(NULL));
int total,i;
int num[10],randNum;
double perc[10];
cout<<"How many random numbers to generate:
";
cin>>total;
for(i=0;i<10;i++){
num[i]=0;
}
for(i=0;i<total;i++){
randNum = rand() % 10 + 1;
num[randNum-1]+=1;
}
percentageCalculator(num,perc,total);
printPercentage(num,perc);
return 0;
}
void percentageCalculator(int num[],double perc[],int total){
for(int i=0;i<10;i++){
perc[i]=num[i]*1.0/total;
}
}
void printPercentage(int num[],double perc[]){
cout<<"Number
Count Percentage\n";
for(int i=0;i<10;i++){
cout<<setw(6)<<i+1<<"
"<<setw(5)<<num[i]<<"
"<<setw(5)<<fixed<<setprecision(4)<<perc[i]<<endl;
}
}