In: Computer Science
8.3)
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 to 100 inclusive; then produce a chart similar to the one below that indicates how many input values fell in the range 1 to 10, 11 to 20, and so on. Print one asterisk for each value entered.
1 - 10 | *****
11 - 20 | **
21 - 30 | *******************
31 - 40 |
41 - 50 | *** 5
1 - 60 | ********
61 - 70 | **
71 - 80 | *****
81 - 90 | *******
91 - 100 | *********
8.4)
The lines in the histogram in PP 8.3 will be too long if a large number of values is entered. Modify the program so that it prints an asterisk for every five values in each category. Ignore leftovers. For example, if a category had 17 values, print three asterisks in that row. If a category had 4 values, do not print any asterisks in that row.
Please write the code simply, without using data structures.
/**********************************************/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// Declaringh constant
const int SIZE = 10;
// Creating an array
int freq[SIZE] = { 0 };
// Declaring variables
int n, num;
// Declaring variables
cout << "How many no of integers you want to enter ?";
cin >> n;
// Creating an array
int nos[n];
for(int i=0;i<n;i++)
{
nos[i]=rand()%(100) + 1;
}
for (int i = 0; i < n; i++)
{
switch (nos[i])
{
case 91 ... 100:
freq[9]++;
break;
case 81 ... 90:
freq[8]++;
break;
case 71 ... 80:
freq[7]++;
break;
case 61 ... 70:
freq[6]++;
break;
case 51 ... 60:
freq[5]++;
break;
case 41 ... 50:
freq[4]++;
break;
case 31 ... 40:
freq[3]++;
break;
case 21 ... 30:
freq[2]++;
break;
case 11 ... 20:
freq[1]++;
break;
case 1 ... 10:
freq[0]++;
break;
}
}
// Displaying the frequency distrubution
for (int i = 0; i < SIZE; i++)
{
cout << ((i * 10) + 1) << " - " << ((i * 10) +
10) << "|";
for (int j = 0; j < freq[i]; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
/**********************************************/
/**********************************************/
Output:
/**********************************************/
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
// Declaringh constant
const int SIZE = 10;
// Creating an array
int freq[SIZE] = { 0 };
// Declaring variables
int n, num;
// Declaring variables
cout << "How many no of integers you want to enter ?";
cin >> n;
// Creating an array
int nos[n];
for(int i=0;i<n;i++)
{
nos[i]=rand()%(100) + 1;
}
for (int i = 0; i < n; i++)
{
switch (nos[i])
{
case 91 ... 100:
freq[9]++;
break;
case 81 ... 90:
freq[8]++;
break;
case 71 ... 80:
freq[7]++;
break;
case 61 ... 70:
freq[6]++;
break;
case 51 ... 60:
freq[5]++;
break;
case 41 ... 50:
freq[4]++;
break;
case 31 ... 40:
freq[3]++;
break;
case 21 ... 30:
freq[2]++;
break;
case 11 ... 20:
freq[1]++;
break;
case 1 ... 10:
freq[0]++;
break;
}
}
// Displaying the frequency distrubution
for (int i = 0; i < SIZE; i++)
{
cout << ((i * 10) + 1) << " - " << ((i * 10) +
10) << "|";
for (int j = 0; j < freq[i]/5; j++)
{
cout << "*";
}
cout << endl;
}
return 0;
}
/**********************************************/
/**********************************************/
Output:
/**********************************************/