Here is my hwprb.cpp : #include <iostream> #include \"randint.h\" using std::cou
ID: 3544178 • Letter: H
Question
Here is my hwprb.cpp:
#include <iostream> #include "randint.h" using std::cout; int main() { int digitCount[10] = {}; for (int i = 0; i < 1000; ++i) ++digitCount[randint() % 10]; for (int i = 0; i < 10; ++i) cout << "There were " << digitCount[i] << " " << i << "'s "; } Here is my question:
Modify hwprb.cpp to produce a "better" random digit and print a histogram (bar chart) of the frequencies. Here is the pseudocode for better_random_digit(): repeat until y < 1000: y = randint() >> 21; //only use high-order bits return last decimal digit of y Tally 500 "better" random digits and print the histogram in this format: DIGIT COUNT 0 50 ************************************************* 1 51 ************************************************** 2 49 ************************************************ 3 50 ************************************************* 4 50 ************************************************* 5 48 *********************************************** 6 53 **************************************************** 7 50 ************************************************* 8 50 ************************************************* 9 49 ************************************************
Here is my hwprb.cpp:
#include <iostream> #include "randint.h" using std::cout; int main() { int digitCount[10] = {}; for (int i = 0; i < 1000; ++i) ++digitCount[randint() % 10]; for (int i = 0; i < 10; ++i) cout << "There were " << digitCount[i] << " " << i << "'s "; } Here is my question:
Modify hwprb.cpp to produce a "better" random digit and print a histogram (bar chart) of the frequencies. Here is the pseudocode for better_random_digit(): repeat until y < 1000: y = randint() >> 21; //only use high-order bits return last decimal digit of y Tally 500 "better" random digits and print the histogram in this format: DIGIT COUNT 0 50 ************************************************* 1 51 ************************************************** 2 49 ************************************************ 3 50 ************************************************* 4 50 ************************************************* 5 48 *********************************************** 6 53 **************************************************** 7 50 ************************************************* 8 50 ************************************************* 9 49 ************************************************
Explanation / Answer
#include <iostream>
#include "randint.h"
using std::cout;
int betterRandint()
{
long long int y=0;
while(y>=1000)
{
y = randint() >> 21; //only use high-order bits
}
return y%10;
}
int main()
{
int digitCount[10] = {};
for (int i = 0; i < 1000; ++i)
++digitCount[betterRandint()];
for (int i = 0; i < 10; ++i)
cout << "There were " << digitCount[i] << " " << i << "'s ";
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.