Please,can someone hlp me to shorten this code using arrays?

#include<iostream> // std, cout, endl
#include<fstream> // ifstream
#include<cstdlib> // exit()

#define FILE_NAME "2018-CSEE-S0800.txt"

using namespace std;

int main() {
// Open the numbers file
ifstream resultsFile(FILE_NAME);

// Ensure the file was opened successfully
if (resultsFile.is_open() == false) {
cout << "Error: could not open the file \"" << FILE_NAME << "\""
<< endl << "Quitting..." << endl;

exit(1); // Terminate the program
}

// Process the result's file
string studentID;
char gender; int counter_7 = 0;
int points;
int counter_8 = 0; int counter_9 = 0; int counter_10 = 0;
int counter_11= 0; int counter_12 = 0; int counter_13 = 0;
int counter_14 = 0; int counter_15 = 0; int counter_16 = 0;
int counter_17 = 0; int counter_18 = 0; int counter_19 = 0;
int counter_20 = 0; int counter_21 = 0; int counter_22 = 0;
int counter_23 = 0; int counter_24= 0; int counter_25 = 0;
int counter_26 = 0; int counter_27 = 0; int counter_28 = 0;
int counter_29 = 0; int counter_30 = 0;
while(resultsFile>> studentID>> gender>> points) {
cout << studentID << " " << gender << " " << points << endl;
// Counting the points
if (points == 7) counter_7++; if (points == 14) counter_14++;
if (points == 8) counter_8++; if (points == 9) counter_9++;
if (points == 10) counter_10++;if (points == 11) counter_11++;
if (points == 12) counter_12++;if (points == 13) counter_13++;
if (points == 15) counter_15++;if (points == 16) counter_16++;
if (points == 17) counter_17++;if (points == 18) counter_18++;
if (points == 19) counter_19++;if (points == 20) counter_20++;
if (points == 21) counter_21++;if (points == 22) counter_22++;
if (points == 23) counter_23++;if (points == 24) counter_24++;
if (points == 25) counter_25++;if (points == 26) counter_26++;
if (points == 27) counter_27++;if (points == 28) counter_28++;
if (points == 29) counter_29++;if (points == 30) counter_30++;
}
// Displaying the results
cout << "\n" ;
cout << "Total count of points" << endl;
cout << "\n";
if (counter_7 > 0) cout << "7 points: " << counter_7 << " "<<"students"<<endl;
if (counter_8 > 0) cout << "8 points: " << counter_8 << " "<<"students" <<endl;
if (counter_9 > 0) cout << "9 points: " << counter_9 << " "<<"students" <<endl;
if (counter_10> 0) cout << "10 points: " << counter_10<< " " <<"students"<<endl;
if (counter_11> 0) cout <<"11 points: " << counter_11<< " " <<"student" <<endl;
if (counter_12> 0) cout <<"12 points: " << counter_12<< " " <<"student" <<endl;
if (counter_13> 0) cout <<"13 points: " << counter_13<< " " <<"students"<<endl;
if (counter_14 > 0) cout <<"14 points: " << counter_14<< " " <<"students"<<endl;
if (counter_15> 0) cout <<"15 points: " << counter_15<< " " <<"students"<<endl;
if (counter_16> 0) cout <<"16 points: " << counter_16<< " " <<"students"<<endl;
if (counter_17> 0) cout <<"17 points: " << counter_17<< " " <<"students"<<endl;
if (counter_18> 0) cout <<"18 points: " << counter_18<< " " <<"students"<<endl;
if (counter_19> 0) cout <<"19 points: " << counter_19<< " " <<"students"<<endl;
if (counter_20> 0) cout <<"20 points: " << counter_20<< " " <<"students"<<endl;
if (counter_21> 0) cout <<"21 points: " << counter_21<< " " <<"students"<<endl;
if (counter_22> 0) cout <<"22 points: " << counter_22<< " " <<"students"<<endl;
if (counter_23> 0) cout <<"23 points: " << counter_23<< " " <<"students"<<endl;
if (counter_24> 0) cout <<"24 points: " << counter_24<< " " <<"students"<<endl;
if (counter_25> 0) cout <<"25 points: " << counter_25<< " " <<"students"<<endl;
if (counter_26> 0) cout <<"26 points: " << counter_26<< " " <<"students"<<endl;
if (counter_27> 0) cout <<"27 points: " << counter_27<< " " <<"students"<<endl;
if (counter_28> 0) cout <<"28 points: " << counter_28<< " " <<"students"<<endl;
if (counter_29> 0) cout <<"29 points: " << counter_29<< " " <<"students"<<endl;
if (counter_30> 0) cout <<"30 points: " << counter_30<< " " <<"student"<<endl;
// Close open resources
resultsFile.close();

return 0;
Last edited on
Whenever you start sticking number suffixes on your variables, you make an array.

> if (points == 7) counter_7++;
Say
int counter[31]; // 0 to 6 unused, 7 to 30 are valid.

Then all your increment code becomes one line
counter[points]++;

I'll leave you to figure out a for loop to print the results (easy exercise for you to attempt).
Thanks,but im a novice programmer so if possible please help with the short code.It will help me.
Thanks for the help though!
> Apr 7, 2019 at 10:18am
> Apr 7, 2019 at 10:58am
You're going to remain a novice programmer if you're just going to give up after 20 minutes without even making an effort.

Please give me the hints.Im a complete beginner
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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    const int MIN_POINTS = 7 ;
    const int MAX_POINTS = 30 ;

    // we create an array of size MAX_POINTS+2
    // we intend to store points less than MIN_POINTS in point_counts_array[0]
    // and points greater than MAX_POINTS in point_counts_array[MAX_POINTS+1]
    // point p in the interval [MIN_POINTS,MAX_POINTS] is stored in point_counts_array[p]
    int point_counts_array[MAX_POINTS+2] = {0} ; // initialised to all zeroes

    const char file_name[] = "2018-CSEE-S0800.txt" ; // avoid the #define

    std::ifstream file(file_name) ;
    if( !file.is_open() )
    {
        std::cout << "could not open input file \"" << file_name << "\"\n" ;
        return 1 ; // returning from main quits the program in an orderly manner
    }

    std::string student_id ;
    char gender ;
    int points ;

    // read points and update the counts in point_counts_array
    while( file >> student_id >> gender >> points ) // for each student info read
    {
        // note: though we read student_id and gender in this program,
        // we do not use them after that

        if( points < MIN_POINTS ) ++point_counts_array[0] ;
        else if( points > MAX_POINTS ) ++point_counts_array[MAX_POINTS+1] ;
        else ++point_counts_array[points] ;
    }

    // print results
    std::cout << "point counts\n---------------\n"
              << "less than " << MIN_POINTS << ": " << point_counts_array[0] << '\n' ;

    for( int p = MIN_POINTS ; p <= MAX_POINTS ; ++p )
        std::cout << p << " points: " << point_counts_array[p] << '\n' ;

    std::cout << "greater than " << MAX_POINTS << ": " << point_counts_array[MAX_POINTS+1] << '\n' ;

    // the file is automagically closed when the file object goes out of scope
    // there is an implicit return 0 ; at the end of main
}
Sir,thanks but I think that there is misunderstanding.A file of results.On eachline: a student ID, gender and his/her NECTA
* points are given. Complete the provided program to count how many
* students got each of the points. That is, if available: howmany
* students got 7 points, how many got 8 points, etc. As an example,
* below is the count of the last four points.
*
* 25 points: 3 students
* 26 points: 3 students
* 29 points: 2 students
* 30 points: 1 student
Ohh ok sir thanks a lot!
Anyone please help me to update the code to get the specific students on each points
Example 25 points: 3 students ( student1, student2,student3)
This is a good opportunity for you to learn about the standard library vector.
https://cal-linux.com/tutorials/vectors.html

The code below won't compile; it contains two (deliberately introduced) errors.
Try to understand the code, correct the errors, compile and run, and study the output.

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
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iomanip>

// print all the student ids in the vector
void print_ids( const std::vector<std::string>& student_ids )
{
    std::cout << "( " ;
    // range base loop: http://www.stroustrup.com/C++11FAQ.html#for
    // auto: http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& id : student_ids ) std::cout << std::quoted(id) << ' ' ;
    std::cout << ")\n" ;
}

int main()
{
    const int MIN_POINTS = 7 ;
    const int MAX_POINTS = 30 ;

    // we create an array of size MAX_POINTS+2; each element of the array is a vector of student ids
    // we intend to store ids of students with less than MIN_POINTS in student_ids_vector[0]
    // and ids of students with greater than MAX_POINTS in student_ids_vector[MAX_POINTS+1]
    // ids of a student with point p in the interval [MIN_POINTS,MAX_POINTS] is stored in student_ids_vector[p]
    // vector: https://cal-linux.com/tutorials/vectors.html
    std::vector<std::string> student_ids_vector[MAX_POINTS+2] ; // default initialised to empty vectors

    const char file_name[] = "2018-CSEE-S0800.txt" ; // avoid the #define

    std::ifstream file(file_name) ;
    if( !file.is_open() )
    {
        std::cout << "could not open input file \"" << file_name << "\"\n" ;
        return 1 ; // returning from main quits the program in an orderly manner
    }

    std::string student_id ;
    char gender ;
    int points ;

    // read id and points and update the counts in student_ids_vector
    while( file >> student_id >> gender >> points ) // for each student info read
    {
        // note: though we read the gender, we do not use them after that

        // push_back: appends a new item to the end of the sequence
        if( points < MIN_POINTS ) student_ids_vector[0].push_back(student_id) ;
        else if( points > MAX_POINTS ) student_ids_vector[MAX_POINTS+1].push_back(student_id) ;
        else student_ids_vector[points].push_back(points) ;
    }

    // print results
    // note: the size() member of the vector gives the number of student ids in the vector
    std::cout << "point counts\n---------------\n"
              << "less than " << MIN_POINTS << ": " << student_ids_vector[0].size() << "   " ;
    print_ids( student_ids_vector[0] ) ;

    for( int p = MIN_POINTS ; p <= MAX_POINTS ; ++p )
    {
        std::cout << p << " points: " << student_ids_vector[p].size() << "  " ;
    }
        print_ids( student_ids_vector[p] ) ;

    std::cout << "greater than " << MAX_POINTS << ": " << student_ids_vector[MAX_POINTS+1].size() << "  " ;
    print_ids( student_ids_vector[MAX_POINTS+1] ) ;

    // the file is automagically closed when the file object goes out of scope
    // there is an implicit return 0 ; at the end of main
}
Topic archived. No new replies allowed.