samedi 29 août 2015

Creating a bar graph in C++ that turns numbers from an array into 'spaces' and '*'s'

I was just wondering if I am on the right track to creating a program that creates a bar graph out of spaces and asterisks and stops when it hits the number 0 (the highest number would be 100 also). So if this was entered: 1 2 3 4 3 2 1 10 0, an outcome like this would appear:

       *
       *
       *
       *
       *
       *
   *   *
  ***  *
 ***** *
********

As of right now it works but I need it to appear vertically as shown in the outcome. Any tips or suggestions are appreciated. thanks

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

int main()
{
    const int MAX = 100;
    int values[MAX];
    int input_number;
    int total_number = 0;
    int largest_number = 0;

    for (int i = 0; i < MAX; i++)
    {
        cin >> input_number;
        if (input_number != 0)
        {
                total_number++;
                values[i] = input_number;
        }

    else if (input_number == 0)
    {
        for(int t = 0;t<total_number;t++)
        {
            if(values[t]>largest_number)
            largest_number = values[t];
        }

    for (int j = 0; j <largest_number; ++j)
    {
        for (int i = 0; i <total_number; ++i)
        cout << (j+values[i] >= largest_number ? '*' : ' ');
    }
    break;
    }
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire