Write a C++ program that read numbers from an integer array and graph the information in the form of bar chat. Sample output is given below. Element Value Histogram 0 19 ******************* 1 3 *** 2 15 *************** 3 7 ******* 4 11 *********** 5 9 ********* 6 13 ************* 7 5 ***** 8 17 ***************** 9 1 *
#include <bits/stdc++.h>
using namespace std;
void Barchart(int array[], int x)
{
int Max = *max_element(array, array + x);
for (int i = Max; i >= 0; i--) {
cout.width(2);
cout << right << i << " | ";
for (int j = 0; j < x; j++) {
if (array[j] >= i)
cout << " x ";
else
cout << " ";
}
cout << "\n";
}
for (int i = 0; i < x + 3; i++)
cout << "---";
cout << "\n";
cout << " ";
for (int i = 0; i < x; i++) {
cout.width(2);
cout << right << array[i] << " ";
}
}
int main()
{
int array[10] = { 19, 3, 15, 7, 11, 9,
13, 5, 17, 1 };
int x = sizeof(array) / sizeof(array[0]);
Barchart(array, x);
return 0;
}
Comments
Leave a comment