1

I have been assigned the following question. Write a program that generates 10000 random integers in the range 0-99 and produces a Histogram from the random data. Assume that we wish to count the number of numbers that lie in each of the intervals 0-9, 10-19, 20-29, ........., 90- 99. This requires that we hold 10 counts, use an array to hold the 10 counts. While it would be possible to check which range a value x lies in by using if-else statements this would be pretty tedious. A much better way is to note that the value of x/10 returns the index of the count array element to increment. I have the following code but I keep getting subscript requires array or pointer type error.

    #include<iostream>
using namespace std;

int main() {
    int count[10], i, nums[10000];

    // Create the Samples
    for (i = 0; i < 10000; i++){
        nums[i] = rand() % 100;


            //Actually displaying the histogram.
            cout << "0-9: ";
            for (i = 0; i < nums[0][1] / 20; i++) { cout << "X"; }
        cout << endl;

            cout << "10-19: ";
        for (i = 0; i < nums[1][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "20-29: ";
        for (i = 0; i < nums[2][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "30-39: ";
        for (i = 0; i < nums[3][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "40-49: ";
        for (i = 0; i < nums[4][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "50-59: ";
        for (i = 0; i < nums[5][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "60-69: ";
        for (i = 0; i < nums[6][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "70-79: ";
        for (i = 0; i < nums[7][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "80-89: ";
        for (i = 0; i < nums[8][1] / 20; i++) { cout << "X"; }
        cout << endl;

        cout << "90-99: ";
        for (i = 0; i < nums[9][1] / 20; i++) { cout << "X"; }
        cout << endl;
    }
    }


  • 2
    `nums[0][1]` is nonsense when `nums` is a ONE dimension array. And Fyi, a single 10-element counter array, `int count[10] = {0};` and a loop of 10000 iterations that effectively does this each iteration: `++count[ (rand() % 100) / 10 ];` would make almost all of this code go away. At least that seems to be the data you're trying to gather. – WhozCraig Jan 14 '21 at 16:28
  • Ok so how would I be able to identify which numbers fall in each range (ex. 50-59) in a one dimension array? Sorry I am new to arrays. @WhozCraig – FutureBallerCoder Jan 14 '21 at 16:29
  • I have made those changes but now how do I use cout in order to display the results in the form of a histogram? – FutureBallerCoder Jan 14 '21 at 16:41
  • The assignment prompt answers the question "how would I be able to identify which numbers fall in each range" very explicitly. – sweenish Jan 14 '21 at 16:42

1 Answers1

3

Your use of nums is incorrect, and ultimately you don't really care about retaining those values anyway. All you care about is that they happened; not what they still-are after the fact.

Gathering data for a specific range in 0..99 , broken into segments of 10 each (0..9, 10..19, 20..29, etc) can easily be done by doing this:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>

int main()
{
    int count[10] = {0};

    std::srand((unsigned)std::time(nullptr));

    for (int i=0; i<10000; ++i)
        ++count[(rand() % 100) / 10];

    for (int i=0; i<10; ++i)
    {
        std::cout 
            << std::setw(2)
            << std::setfill('0')
            << std::setprecision(2)
            << i * 10
            << '-'
            << std::setw(2)
            << (i+1) * 10 - 1
            << " : ";

        for (int j=0; j<count[i]/20; ++j)
            std::cout << '*';
        std::cout.put('\n');
    }     
}

Output (will vary, obviously)

00-09 : ***************************************************
10-19 : *************************************************
20-29 : **************************************************
30-39 : **************************************************
40-49 : *****************************************************
50-59 : ************************************************
60-69 : ************************************************
70-79 : ************************************************
80-89 : ***************************************************
90-99 : ************************************************

C Version (oops)

I misread the question, and thought you were targeting C (mainly because no one in their right mind used rand() in modern C++ programs with the advent of <random>). Not letting a mundane thing go to waste, see below.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main()
{
    int count[10] = {0};

    srand((unsigned)time(NULL));
    for (int i=0; i<10000; ++i)
        ++count[(rand() % 100) / 10];

    for (int i=0; i<10; ++i)
    {
        printf("%02d-%02d : ", i*10, (i+1)*10-1);
        for (int j=0; j< count[i] / 20; ++j)
            fputc('*', stdout);
        fputc('\n', stdout);
    }     
}

The output will be similar.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
  • Thank you very much for this info. However my professor would like us to use the std namespace. Is there anyway that this could be easily converted using only std? @WhozCraig – FutureBallerCoder Jan 14 '21 at 18:02
  • The first list is proper C++. If you're referring to something like `using namespace std;`, just don't, and tell your prof as much. See this: [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice?) . Better to break terrible habits now rather than later. – WhozCraig Jan 14 '21 at 18:05
  • Okay thank you for that tip. And if you could is there anyway you could explain the printf("%02d-%02d : ", i*10, (i+1)*10-1); to me? @WhozCraig. – FutureBallerCoder Jan 14 '21 at 18:07
  • The second code list is just a "C" version of the same task. The format specifiers for `printf` that you see are telling it that two `int` values will be received. Each should br printed as 2-digit decimal values, with zero-filling on the left. Hopefully it's obvious that's only there for the first line of the histogram (values 0 and 9). Play with it. Take out the `02` modifiers, leaving just `%d-%d` and see what happens to the output, particularly the first line. It will make sense then. – WhozCraig Jan 14 '21 at 18:10
  • Thank you so much for not only helping with the code but explaining it! – FutureBallerCoder Jan 14 '21 at 18:11
  • For reference: [`printf` info](https://en.cppreference.com/w/c/io/fprintf). Bookmark that site, btw, it really is the best online reference for C++ and C standard library and language information. – WhozCraig Jan 14 '21 at 18:13
  • Will do. Thanks a ton. – FutureBallerCoder Jan 14 '21 at 18:17