Output not displaying correctly

I am really close to getting this function to work. This function is to display a horizontal bar chart for quiz score values (from lowest score to largest score) to appear across the page and asterisk type bars are displayed vertically.

The output I get is below (with array values a[35]=5, a[50]=2, a[41]=6, a[42]=3, a[48]=4, a[42]=2) So int low = 35, int high = 50, int freq = 6). The asterisk for the score of 35 and 41 is correct. However, asterisk for the score of 42 is somehow shifted to the right.



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
  //This function takes in an array of integers, an integer for the lowest score, an integer for the highest
//score, and an integer for the largest frequency. It then outputs a readable bar chart graph in a horizontal  
//arrangement where the quiz score values appear across the page and the asterisks are displayed vertically..
void horizontalBarChart(int a[], int low, int high, int freq)
{
	cout << "Frequency: Score Horizontal Bar Chart" << endl;
	cout << " " << endl;	//spacing 

	for (int i = freq; i >= 1; i--)	//frequency starting from high to low
	{
		cout << "    " << "^" << "   " << i << ":";
		for (int j = low; j <= high; j++)	//looping through array
		{
			if (i == a[j])	//if frequency at index is less than or equal to frequecy count (not inlcuding 0 frequencies)
			{
				int spaces = (((j - low) + 1) * 3);	//number of spaces over to print asterisk
				for (int k = 1; k <= spaces; k++)	//printing number of spaces over 
				{
					cout << " ";	//print space
				}
				cout << "*";	//print asterisk
			}
		}
		cout << endl;
	}

	cout << "---------: ";	//prints axis
	for (int i = 0; i < ((high - low) + 1); i++)
	{
		cout << "--^";
	}
	cout << endl;

	cout << "    Score:" << "  ";	//prints axis 
	for (int i = low; i <= high; i++)
	{
		cout << i << " ";
	}
	cout << endl;
}
Last edited on
Topic archived. No new replies allowed.