Date: April 24, Tuesday Name: 1) 1. Write a program that generates 200 random nu
ID: 3715513 • Letter: D
Question
Date: April 24, Tuesday Name: 1) 1. Write a program that generates 200 random numbers (0-9), count the number of each random numbers The program consists of the following procedures: main procedure calls genData to generate the random numbers, calls countRandoms to count the number of random numbers 0, 1, 2, ... 9, and calls displayResults to display the results genData procedure generates 200 random numbers (0~9) and save them in an array countRandoms procedure counts the number of os, 1s, ... 4) displayResults procedure displays the results as follows: 3) 0: 20 1: 15 2: 24 3: 19 mple run of the program is as follows: windowssystem32cmd.exe andor nunbers are: 2:091200 1?s/30 3 20000000 ???????? 20000000 ?? 80000000 20000000 8000000000000000 20000000000000000000000 ?? 20000000 100000000000000000000000 0000000 20000000 2000000 8000000000000000000000 20000000 000000000000000000000000 2.00000000000000000000000000000 8000000000000000000000 0000000000000000000000 90000000 60000000 2000000000000000 s S0000000000000000000000 €0000000 &000000000000000000000 9000000000000000000000 &0000000 ?000000000000000000000 S000000000000000000000 S0000000 10000000 60000000 80000000 9000000000000000000000 60000000 60000000 €0000000 S8000000 20000000000000000000000 s0000000 1000000000000000000000000000000 2000000000000000000000 200000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000 20000000 S0000000 90000000000000000000000000000000 20000000 20000000 20000000 200000000000000000000000 20000000000000000000000000000000 see00000 s000000000000000 90000000 2000000000000000000000 0000000 900000000000000000000000000000000000000099999 80000000 90000000000000000000000 9000000000000000000000 20000000 s000000 20000000000000000000000000000000000000 20000000 200000000000000000000000000000000000000000000 000000000000000 20000000 €0000000 6000000 ??0808000 S800000 ???????? 1000b000 10000000 20000000Explanation / Answer
The C++ code is as follows :
#include<bits/stdc++.h>
using namespace std;
#define MAX 200 // defining the constraints on the array
#define SIZE 10
int arr[MAX]; // array to contain all the random numbers
int c[SIZE]; // array store the number of times 0-9 occurs
int genData()
{
for(int i = 0; i < MAX; i++)
{
arr[i] = rand() % 10; // generating the random numbers
cout << "0000000" << arr[i] << " ";
}
cout << endl;
return 0;
}
int countRandoms()
{
for(int i = 0; i < MAX; i++) // checking the number of times each number occurs
{
if(arr[i] == 0) // incrementing if we come across the number which is being checked in the if condition
c[0]++;
if(arr[i] == 1)
c[1]++;
if(arr[i] == 2)
c[2]++;
if(arr[i] == 3)
c[3]++;
if(arr[i] == 4)
c[4]++;
if(arr[i] == 5)
c[5]++;
if(arr[i] == 6)
c[6]++;
if(arr[i] == 7)
c[7]++;
if(arr[i] == 8)
c[8]++;
if(arr[i] == 9)
c[9]++;
}
return 0;
}
int displayResults()
{
for(int i = 0; i < SIZE; i++) // displaying the results in the given format
{
cout << i <<": " << c[i] << endl; // printing the output
}
return 0;
}
int main()
{
genData(); // calling all the required functions
countRandoms();
displayResults();
return 0;
}
Output :
It will first print all the 200 random numbers
0: 15
1: 21
2: 17
3: 19
4: 19
5: 20
6: 24
7: 23
8: 19
9: 23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.