Answer to Question #190786 in C++ for M KAMRAN

Question #190786

Let’s print a vertical bar graph based on the values stored in the array. Create an integer array of size 5. Fill it with random integers between 1 and 15. If the first element of the array is 5, you should print 5 “*” vertically. If the second element has 3 elements, then you should print 3 “*”vertically. A sample vertical bar graph is shown below:    

                      *

          *         *

*        *         *

*        *   *   *

*   *  *   *   *

3   1  4   2   5




1
Expert's answer
2021-05-09T17:00:33-0400
#include <iostream>


using namespace std;
int main(){
    //declare an array of size 5
    int arr[5];
    //initialize the array with random numbers between 1 and 15
    for(int n=0;n<5;n++){
        arr[n]=(rand()%15+1);
    }
    //initialize the highest number in the array
    int highest=arr[0];
    //find the highest number in the array
    for (int i=0;i<5;i++)
    {
        if (arr[i]>highest)
            highest=arr[i];
    }


    //draw the bar graph
    for (int i=highest;i>=1;i--) //to print rows
    {
       for (int j=0;j<=4;j++) //to print the columns
       {
            if (arr[j]>=i)
                cout<<"* ";
            else
                cout<<"  ";
       }
       cout<<endl;
    }
    //display the numbers
    for(int j=0;j<5;j++){
        cout<<arr[j]<<" ";
    }
    cout<<endl;
    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