Displaying multiple characters

I ask the user to input numbers of students which creates a dynamic array. Then the user inputs the names and grades of each student. I then use this function to display asterisks for each grade in the group. So if there were 3 100% then 3 asterisks will display in the 100 group. My question is: how do I shrink my code so that it does the same thing without using such a large code


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
void displayGrade(int num, Student *myStudents)
{
    int g;
    int t1=0, t2=0, t3=0, t4=0, t5=0, t6=0, t7=0, t8=0, t9=0, t10=0, t11=0;
    
    for(int i = 0; i < num; i++)
    {
        g = myStudents[i].getGrade();
        
        if(g <= 9)
            t1++;
        else if ( g > 9 && g <= 19)
            t2++;
        else if ( g > 19 && g <= 29)
            t3++;
        else if ( g > 29 && g <= 39)
            t4++;
        else if ( g > 39 && g <= 49)
            t5++;
        else if ( g > 49 && g <= 59)
            t6++;
        else if ( g > 59 && g <= 69)
            t7++;
        else if ( g > 69 && g <= 79)
            t8++;
        else if ( g > 79 && g <= 89)
            t9++;
        else if ( g > 89 && g <= 99)
            t10++;
        else t11++;
    }
    
    cout << "Grade Distribution: " << endl;
    cout << setw(10) << "0 - 9 : ";
    for(int i = 0; i < t1; i++)
    {
        cout << "*";
    }
    
    cout << endl << "10 - 19 : ";
    for(int i = 0; i < t2; i++)
    {
        cout << "*";
    }
    
    cout << endl << "20 - 29 : ";
    for(int i = 0; i < t3; i++)
    {
        cout << "*";
    }
    
    cout << endl << "30 - 39 : ";
    for(int i = 0; i < t4; i++)
    {
        cout << "*";
    }
    
    cout << endl << "40 - 49 : ";
    for(int i = 0; i < t5; i++)
    {
        cout << "*";
    }
    
    cout << endl << "50 - 59 : ";
    for(int i = 0; i < t6; i++)
    {
        cout << "*";
    }
    
    cout << endl << "60 - 69 : ";
    for(int i = 0; i < t7; i++)
    {
        cout << "*";
    }
    
    cout << endl << "70 - 79 : ";
    for(int i = 0; i < t8; i++)
    {
        cout << "*";
    }
    
    cout << endl << "80 - 89 : ";
    for(int i = 0; i < t9; i++)
    {
        cout << "*";
    }
    
    cout << endl << "90 - 99 : ";
    for(int i = 0; i < t10; i++)
    {
        cout << "*";
    }
    
    cout << endl << setw(10) << "100: ";
    for(int i = 0; i < t11; i++)
    {
        cout << "*";
    }
}

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void displayGrade( int num, const Student* myStudents )
{
    int num_asterisks[11] {} ; // initialise to all zeroes

    for( int i = 0; i < num; ++i )
    {
        const int grade = myStudents[i].getGrade();
        if( grade > 0 && grade < 100 ) ++num_asterisks[ grade/10 ] ;
        else if( grade == 100 ) ++num_asterisks[10] ;
        // else: invalid grade less than zero or more than 100
    }

    std::cout << "Grade Distribution:\n" ;
    for( int i = 0 ; i < 10 ; ++i ) // for grades 0 - 99
    {
        const int lb = i*10 ;
        const int ub = lb + 9 ;
        std::cout << std::setw(2) << lb << "-" << ub << " : "
                  << std::string( num_asterisks[i], '*' ) << '\n' ;
    }

    // for grade 100
    std::cout << std::setw(8) << "100 : " << std::string( num_asterisks[10], '*' ) << '\n' ;
}
Topic archived. No new replies allowed.