954,164 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bar Graph Formatting

Hi there.
I am producing a bar graph which uses values from a array.
I have tried many different ways of getting a fixed size bar graph.
Currently it prints the graph correctly with bars both above and below the x axis it also prints the reverse if the user inputted value is negative.
The problem i have is that at the moment if the user enters a large value say 100 the graph step down 100 lines to the x axis making the graph over 100 lines big (as negative bar graph is also included).
I have tried to use an equation inside the main for loop however it plays up with the graph.
Thanks for your sugesstions.

double array1[61];
double val1;
int q;
double input;

double array1[61];
double val1;
int q;
double input;

for (val1 = 50; val1 >= 0; val1--)
    {
	  cout << setw(6) << input--;

        for (q = 0; q <= 60; q++)
        {
            if (array1[q] >= val1)
            {
                cout << "#";
            }
            else
            {
                cout << " ";
            }
        }
        cout << endl;
    }



    for (val1 = 0; val1 >= -50; val1--)
    {
        cout << setw(6) << input--;                  

        for (q = 0; q <= 60; q++)
        {
            if (array1[q] >= val1)
            {
                cout << " ";
            }
            else
            {
                cout << "#";
            }
        }
        cout << endl;
    }

    cout << endl;

}
meanace
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

Do you want to?

- clip extreme values, say if ( x > 25 ) x = 25 ?
- scale the graph (max=100), so px = x * 25 / max

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Hi
Id like to scale the graph so that if the user inputs a value the graph is shown at a reasonable size which works with both negative and positive inputted values.
Thank you.

meanace
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

OK, so start by calculating the input ranges, then you can create scale factors to map the input range to the output range.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

Ive tried to make an equation in the for loop which decreases at a even scale but the graph plays up.

meanace
Newbie Poster
5 posts since Sep 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You