Question

In: Computer Science

Create a C++ file with given instructions below- Analyze random numbers Create two arrays, one int...

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)

Solutions

Expert Solution

#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;
   }
}


Related Solutions

C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them...
Create a program using Java. Create two different 3D arrays with random numbers. (lets name them array1 and array 2) Add the two 3Darrays together and then get the average. Save the average on a separate 3D array.(lets name it array3) Then add array1 and array3 then get the average Save the average on a separate 3Darray(lets name it array 4) Then add array2 and array3 then get the average Save the average on a separate 3Darray (lets name it...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84,...
C++ Given 2 int arrays that are related, sort them correspondingly. EXAMPLE: int arr1[] = {84, 4, 30, 66, 15}; int arr2[] = {7, 5, 2, 9, 10}; SORTED ANSWER: 4 - 5 15 - 10 30 - 2 66 - 9 84 - 7 IN C++
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
Create a program that generates a file of random numbers, and then prints them in neat...
Create a program that generates a file of random numbers, and then prints them in neat fashion to another file, and also saves to that file the average and standard deviation of those numbers. I) First, you would need to generate a file of random numbers that consists of N random numbers (100 < N < 1000). Each random digit should be a real number (type double) between 0 and 50. This file and its digits would now serve as...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are...
how to write in java; Write a method int[] coPrime[int num, int[]numbers] { // instructions are that it returns an array of all the elements of the int[] array numbers which are coprime with x } Note that the array that is returned may be an empty array--you will have to count how many times gcf(x, numbers[i]) == 1. ASSUME numbers is not null and not empty.
Answer the following questions based on the given c file: #include #include #include    int c...
Answer the following questions based on the given c file: #include #include #include    int c = 0; void *fnC() {     int i;     for(i=0;i<10;i++)     {   c++;         printf(" %d", c);     }       } int main() { int rt1, rt2;   pthread_t t1, t2; int trial_count = 0; // For every trial, lets zero out the counter and run the count routine “twice”     // as threads that can be scheduled onto independent cores instead of running     // in sequence. for...
First create the text file given below. Then complete the main that is given. There are...
First create the text file given below. Then complete the main that is given. There are comments to help you. An output is also given Create this text file: data1.txt -59 -33 34 0 69 24 -22 58 62 -36 5 45 -19 -73 62 -5 95 42 ` Create a project and a Main class and copy the Main class and method given below. First declare the array below the comments that tell you to declare it. Then there...
create two random numbers between 1 and 6. if when the sum of two numbers are...
create two random numbers between 1 and 6. if when the sum of two numbers are added togethere their sum is less than 5 or greater than 12, output to the console: "you win". if is not, output "you lose" C++
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers...
HW_6a - Read a text file Create a new C++ project and name it as:   Numbers Create a text file and     save it as:   data.txt Create and save the file      in a C++ project      in the Resource folder. Enter the following numbers:        3                                                              4                                                              5       Note:   After you enter the 5, don’t press the enter key. Save and close the file. Add another file and name it:   Source.cpp Write one statement that declares a file...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT