Creating a "Bar Graph"

Hi. This is for a homework assignment when I take in some integers from an infile, sort them by their range of numbers and find that range's average value. Then i am asked to print to the outfile the score ranges and a bar graph of their averages. (using astericks as the bars and a heading that I've already created as the axis). I can't for the life of me figure out how to get the bar graph to print. Any insight you have would be helpful. If you need an exam of what I am talking about I can track one down... Cheers!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <fstream> // stream for input files
#include <iomanip>
#include <iostream> // stream for output files

using namespace std;

void printgraph(ofstream& outFile, int a); //function of issue
void printout(ofstream& outFile, int a[8]);

int main()
{
    int score;
    int scorerange[8] = {0,0,0,0,0,0,0,0};
    int scoreacc[8] = {0,0,0,0,0,0,0,0};
    int avg[8] = {0,0,0,0,0,0,0,0};
    int i;
    int list[27];

    ifstream inFile; //input file stream variable
    ofstream outFile; //output file stream variable

    inFile.open("scoresin.txt"); //WORKS
    if (!inFile) //WORKS
    {
        cout << "Cannot open the input file!" << endl;
        return 0;
    }

    outFile.open ("scoresout.out"); //WORKS

    cout << "Group                     Score" << endl; //WORKS
    cout << "  #  0 20 40 60 80 100 120 140 160 180 200" << endl; //WORKS

    for (i = 0; i < 26; i++) //WORKS
    {
        inFile >> list[i];

        score = list[i];

        if (0 < score && score < 25)
            scorerange[0]++, scoreacc[0] += score; // FILL IN FOR REST

        else if (24 < score && score< 50)
            scorerange[1]++, scoreacc[1] += score;

        else if (49 < score && score< 75)
            scorerange[2]++, scoreacc[2] += score;

        else if (74 < score && score< 100)
            scorerange[3]++, scoreacc[3] += score;

        else if (99 < score && score< 125)
            scorerange[4]++, scoreacc[4] += score;

        else if (124 < score && score< 150)
            scorerange[5]++, scoreacc[5] += score;

        else if (149 < score && score< 175)
            scorerange[6]++, scoreacc[6] += score;

        else if (174 < score && score< 201)
            scorerange[7]++, scoreacc[7] += score;
    }

    printout(outFile, scorerange); //WORKS

    for (i = 0; i++; i < 8)
    {
         avg[i] = scoreacc[i]/scorerange[i];
    }

    printgraph(outFile, avg[0]);
    printgraph(outFile, avg[1]);
    printgraph(outFile, avg[2]);
    printgraph(outFile, avg[3]);


    inFile.close(); //WORKS
    outFile.close(); //WORKS
    return 0;
}

void printgraph(ofstream& outFile, int a) // sub in for book
{
    int count;

    for (count = 1; count < a; count++)
        outFile<< '*' ;

    outFile<< endl;
}

void printout(ofstream& outFile, int a[8]) //WORKS
{
    outFile << "Students with scores betwen 0 - 24 are: " << a[0] << endl;
    outFile << "Students with scores betwen 25 - 49 are: " << a[1] << endl;
    outFile << "Students with scores betwen 50 - 74 are: " << a[2] << endl;
    outFile << "Students with scores betwen 75 - 99 are: " << a[3] << endl;
    outFile << "Students with scores betwen 100 - 124 are: " << a[4] << endl;
    outFile << "Students with scores betwen 125 - 149 are: " << a[5] << endl;
    outFile << "Students with scores betwen 150 - 174 are: " << a[6] << endl;
    outFile << "Students with scores betwen 175 - 200 are: " << a[7] << endl;
}
To create a barchart on the terminal, you will be writing out one row at a time.

You will write out the top row, then the next row, then the next, and so on until the bottom.

So first, work out how many rows you need.

How many rows do you need? How do you work that out?

Then work out how many columns you will need. This is the width of the barchart. How many columns do you need? How do you work that out?

This is programming. Thinking about the problem in such a way that the solution lends itself to a programmatic expression. All the rest is learning syntax and the tools available to you, but programming is thinking.
Last edited on
Thanks. I need to allow the bar graph to create it's own dimensions. I would do cout and just fill in stars if I could, but that's not allowed. I can't figure out how to get it to print the amount of stars that the average is... I've been at it for a week with this last chunk of the assignment totally stumped by it.
Hello firefoxfuzz,

Call me lazy, but what does your input file look like. I mighr be able to create one, but can not guaranty that it will work properly.

FYI lines 14, 15 and 16 can be written simply as: int scorerange[8]{ 0 }; and the same for the other two. This will initialize the first element as 0 and all the rest of the array. Lines 13, 17 and 18 should also be initialized. "outFile" should also have a check for not being open.

I will load up your program and see what I can come up with.

Hope that helps,

Andy
Hello firefoxfuzz,

After working with your program I found that the for loop at line 68 is backwards and does not execute.

One question I have is do you want a horizontal or vertical graph? for a horizontal graph I put the call to the "printgraph" function in the "printout" function after each "outFile" statement and added the average number after the bar. Just something I was playing with.

Andy
I need to allow the bar graph to create it's own dimensions.

That's what I already asked you. How many rows will it have? You need there to be enough rows to show the tallest bar, so you work out the height of the tallest bar, add however many rows you need for the axis, and that's the number of rows you'll output. If it's going to be different every time, you can do this... programmatically.

Perhaps now you've seen how to work out how many rows, you can come up with a way of working out how many columns there will be on your own.


I would do cout and just fill in stars if I could, but that's not allowed.

Wait, so you know how to do this with cout but you don't know how to do it to file? It's the exact same thing.
Last edited on
Topic archived. No new replies allowed.