C++ For Engineers and Scientist 5.5 #12 (Numerical) Write a program that tests t
ID: 3807782 • Letter: C
Question
C++ For Engineers and Scientist
5.5 #12
(Numerical) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters, such as zerocount, onecount, twocount, and so forth to 0. Then generate a large number of pseudorandom integers between 0 and 9. Each time a 0 occurs, increment the variable you have designated as the zero counter; when a 1 occurs, increment zerocount; when 1 occurs , increment onecount; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of the time they occurred.
Explanation / Answer
// C++ code
#include <iostream>
#include <cmath>
#include <fstream>
#include <stdlib.h>
using namespace std;
int main ()
{
int zerocount= 0,onecount= 0,twocount= 0,threecount= 0,fourcount= 0,fivecount= 0,sixcount= 0,sevencount= 0,eightcount= 0,ninecount= 0;
int number;
int total = 0;
// generating 10000 randomm numbers
for (int i = 0; i < 100000; ++i)
{
number = rand()%10;
if(number == 0) zerocount++;
else if(number == 1) onecount++;
else if(number == 2) twocount++;
else if(number == 3) threecount++;
else if(number == 4) fourcount++;
else if(number == 5) fivecount++;
else if(number == 6) sixcount++;
else if(number == 7) sevencount++;
else if(number == 8) eightcount++;
else if(number == 9) ninecount++;
}
total = zerocount+onecount+twocount+threecount+fourcount+fivecount+sixcount+sevencount+eightcount+ninecount;
cout << "0s : " << zerocount << " percentage of zero: " << 100*zerocount/(01.0*total) << "%" << endl;
cout << "1s : " << onecount << " percentage of zero: " << 100*onecount/(01.0*total) << "%" << endl;
cout << "2s : " << twocount << " percentage of zero: " << 100*twocount/(01.0*total) << "%" << endl;
cout << "3s : " << threecount << " percentage of zero: " << 100*threecount/(01.0*total) << "%" << endl;
cout << "4s : " << fourcount << " percentage of zero: " << 100*fourcount/(01.0*total) << "%" << endl;
cout << "5s : " << fivecount << " percentage of zero: " << 100*fivecount/(01.0*total) << "%" << endl;
cout << "6s : " << sixcount << " percentage of zero: " << 100*sixcount/(01.0*total) << "%" << endl;
cout << "7s : " << sevencount << " percentage of zero: " << 100*sevencount/(01.0*total) << "%" << endl;
cout << "8s : " << eightcount << " percentage of zero: " << 100*eightcount/(01.0*total) << "%" << endl;
cout << "9s : " << ninecount << " percentage of zero: " << 100*ninecount/(01.0*total) << "%" << endl;
return 0;
}
/*
output:
0s : 10130 percentage of zero: 10.13%
1s : 10072 percentage of zero: 10.072%
2s : 9990 percentage of zero: 9.99%
3s : 9842 percentage of zero: 9.842%
4s : 10174 percentage of zero: 10.174%
5s : 9930 percentage of zero: 9.93%
6s : 10059 percentage of zero: 10.059%
7s : 9954 percentage of zero: 9.954%
8s : 9891 percentage of zero: 9.891%
9s : 9958 percentage of zero: 9.958%
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.