This code is in C++. The output should look something like this (pretend the dots are spaces):
0-9.......5...XXXXX
10-19...7...XXXXXXX
20-29...2...XX
etc.
90-99...11..XXXXXXXXXXX
Here is what I have so far:
.
#include <iostream>
using namespace std;
int main() {
int count, x, i = 0;
int nums[10][10000] = { 0 };
for (count = 0; count < 10000; count++) {
x = rand() % 100;
if (x >= 0 && x <= 9)
{
nums[0][1]++;
}
else if (x >= 10 && x <= 19)
{
nums[1][1]++;
}
else if (x >= 20 && x <= 29)
{
nums[2][1]++;
}
else if (x >= 30 && x <= 39)
{
nums[3][1]++;
}
else if (x >= 40 && x <= 49)
{
nums[4][1]++;
}
else if (x >= 50 && x <= 59)
{
nums[5][1]++;
}
else if (x >= 60 && x <= 69)
{
nums[6][1]++;
}
else if (x >= 70 && x <= 79)
{
nums[7][1]++;
}
else if (x >= 80 && x <= 89)
{
nums[8][1]++;
}
else
{
nums[9][1]++;
}
}
cout << "0-9: ";
for (i = 0; i < nums[0][1]; i++) { cout << "X"; }
cout << endl;
cout << "10-19: ";
for (i = 0; i < nums[1][1]; i++) { cout << "X"; }
cout << endl;
cout << "20-29: ";
for (i = 0; i < nums[2][1]; i++) { cout << "X"; }
cout << endl;
cout << "30-39: ";
for (i = 0; i < nums[3][1]; i++) { cout << "X"; }
cout << endl;
cout << "40-49: ";
for (i = 0; i < nums[4][1]; i++) { cout << "X"; }
cout << endl;
cout << "50-59: ";
for (i = 0; i < nums[5][1]; i++) { cout << "X"; }
cout << endl;
cout << "60-69: ";
for (i = 0; i < nums[6][1]; i++) { cout << "X"; }
cout << endl;
cout << "70-79: ";
for (i = 0; i < nums[7][1]; i++) { cout << "X"; }
cout << endl;
cout << "80-89: ";
for (i = 0; i < nums[8][1]; i++) { cout << "X"; }
cout << endl;
cout << "90-99: ";
for (i = 0; i < nums[9][1]; i++) { cout << "X"; }
cout << endl;
}
Since the program needs 10,000 random integers, my output doesn't look much like a histogram. Does anyone have any ideas on how to fix that? I also can't figure out how to add the number of random numbers generated for a certain range to the output. Thank you for your help!
Aucun commentaire:
Enregistrer un commentaire