Hello all. I need help with a classwork assignment. The original task I have to do is use a single-dimensional array and print a bar chart (using asterisks) for these ages: 14-17, 18-20, 19-21, 22-24, 25-27, 28-30, 31-33, 34-36, 37-39, 40-100. These are the students in each age range: 1, 2 , 4, 8, 20,8,7,4,3,1. The code I've modified comes from a very similar example of the c++ deitel book fig. 7.9 (in case anyone had it). I'm very new to c++ still and it's hard to try and pull things we've gone over and apply them here. Any help is welcome thanks in advance.
You need a "18-20" as well. The calculation in line 21 is wrong. Need a (i-2) in there. Line 18 should be if (9 == i).
But you could replace the if (..) elseif (..) else sequence with a switch() statement with a case for every age range. Much simpler, less likely to make a mistake, easier to modify, easier to read.
See: http://www.cplusplus.com/doc/tutorial/control/
for something like:
1 2 3 4 5 6 7
switch {
case 0: cout << "14-17; break;
case 1: cout << "18-20; break;
...
case 9: ....
default: ...
}