0

Hail. I'm having trouble with this program...here is the discription...

Description: Design and implement an application that creates a histogram that allows you to visually inspect the frequency distribution of a set of values. The program should read in an arbitrary number of integers that are in the range 1 – 100 inclusive, then produce a chart similar to the one below that indicates how many input values fell in the range of 1 to 10, 11 – 20, and so on. Print one asterisk for each value entered. After the histogram has been displayed, allow the user to clear the screen and create another one as many times as he wishes.

 1 – 10 | **************************
11 – 20 | ***
21 – 30 |******
31 – 40 |**
41 – 50 |*
51 – 60 |
61 – 70 |************************************
71 – 80 |***
81 – 90 |****************
91 – 100    | **

Now heres the code that i have dont so far.

#include <iostream>
#include <iomanip>
using namespace std;

    //local constants
    int quit = -1;

    //local variables
    int data;
/**************************start main program*********************/



int main()
{
    const int arraySize = 15;
    int array[arraySize] = {0};

    for(int a = 0; a < arraySize; a++)
    {
            if(a == 0)
                 cout <<"Enter a number please.\n";
                 else
                 cout <<"Enter another number please.\n";
            cin >> array[a];
    }

    cout<<"Here are the details of your entries.\n"
        <<"Element"<<setw(20)<<"Value"
        <<setw(25)<<"Histogram"<<endl;

    for(int i = 0; i < arraySize; i++)
    {
    cout<<setw(7)<<i<<setw(20)<<array[i]<<setw(17);

        for(int j = 0; j < array[i]; j++)
        cout <<"*";

        cout <<endl;
    }



system("PAUSE");
return 0;
} 

--------------------------------------------------------------

My questions and what i need help with is ...

-How can i make the array any size, so it can quit on the 2nd number entered or the 27th number entered?

-How can i make it look how its suppose to be. I coded it this way because i wanted something to post and im not sure how i can make it so its the correct way as in discription.

Thanks in advance

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by Narue
0

>-How can i make the array any size, so it can quit on the 2nd
>number entered or the 27th number entered?

That really has nothing to do with the array size. The histogram array only has ten elements, and each element is a count of the numbers that fit into the specified range for each element. What you really want is a sentinel for the input loop:

int num;

cout<<"Enter a list of numbers: ";

while ( cin >> num ) {
  if ( num >= 1 && num <= 10 )
    ++array[0];
  else if ( num >= 11 && num <= 20 )
    ++array[1];
  // ...
  else if ( num >= 91 && num <= 100 )
    ++array[9];
  else
    break;
}

In that example, any number outside of the range [1-100] causes the loop to end.

>-How can i make it look how its suppose to be.
Well, actually having the specified data should help you see ways to display it. Currently you don't have the specified data, so printing the correct histogram is impossible.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.