Answer to Question #233891 in C++ for knight_manakal

Question #233891

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 *


1
Expert's answer
2021-09-06T07:05:13-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS