Array Bar Chart

Hey everyone, I need help with one thing on this array. I already asked about this particular program, but I still need help. My problem is on line 22. The age groups are suppose to be displayed like this: 14-17, 18-20, 19-21, 22-24, 25-27, 28-30, 31-33, 34-36, 37-39, 40-100. I assume line 22 is meant to output the rest of the groups other than the ones I stated in my if..else statements. Can anyone figure out how to get the correct outputs?

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
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;

int main()
{
	const size_t arraySize = 10;
	array < unsigned int , arraySize > n = { 1, 2, 4, 8, 20, 8, 7, 4, 3, 1 };

	cout << "Age Range: " << endl;

	for ( size_t i = 0; i < n.size(); ++i )
	{
		if ( 0 == i )
			cout << "14-17: ";
		else if ( 1 == i )
			cout << "18-20:";
		else if ( 9 == i )
			cout << "40-100: ";
		else
			cout << i * 9 + 1 << "-" << (i - 2) << ": ";

	for ( unsigned int stars = 0; stars < n[i]; ++stars )
		cout << '*';
		cout << endl;
	}
}
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
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;

int main()
{
	const size_t arraySize = 10;
	array < unsigned int, arraySize > n = { 1, 2, 4, 8, 20, 8, 7, 4, 3, 1 };
	int k = 19;

	cout << "Age Range: " << endl;

	for (size_t i = 0; i < n.size(); ++i)
	{
		if (i == 0)
			cout << "14-17: ";
		else if (i == 1)
			cout << "18-20: ";
		else if (i == 9)
			cout << "40-100: ";
		else{
			cout << k << "-";
			k += 2;
			cout << k << ": ";
			k++;
		}

		for (unsigned int stars = 0; stars < n[i]; ++stars)
			cout << '*';
		cout << endl;
	}
}
Sweet thanks a ton!
Topic archived. No new replies allowed.