Valerie
Valerie

Reputation: 21

Write a program that generates 10000 random integers in the range 0-99 and produces a Histogram from the random data

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!

Upvotes: 0

Views: 490

Answers (1)

rrauenza
rrauenza

Reputation: 6993

You're going to need to scale your output. Each of your rows might have as many as 1000 X's.

  1. Find the max value across all "buckets" (i.e., max of nums[0] through nums[9])
  2. Multiply each bucket's value by the width of your screen, say 80.
  3. Divide each bucket's count by the max value you calculated
  4. Print that many X's.

This will give you a histogram where your longest result reaches the 80th column.

Your program otherwise seems to work fine, except you need to include stdlib.h for rand() and the way you do your array is very odd. Why is it two dimensional at all?

You can also just do n = rand() % 10 and use n to index your array. Then leave out the if statements. It's essentially the same thing.

If your question is about the shape of your histogram, then add that to the question explaining what you expected.

Upvotes: 2

Related Questions