1

This program reads number of students and three scores (exams) for every student, then calculates the average, and then displays information in tabular format. For some reason it is not working. I have to write this program using 11 functions so it is not possible to change the format, number of functions, or any big change in the main procedure. Update: I made couple of changes and now I can compile and run it and it displays everything

int student_num (const string & prompt);
void scores (int scores1[], int scores2[],int scores3[], int students);
void sum (int scores1[], int scores2[],int scores3[], int totals[], int students);
double average (int totals[], int students);
double find_max(const int totals[], const int students);
double find_min(const int totals[], const int students);
int above_ave (const int totals[], int students, double ave);
void display (int const scores1[], int const scores2[], int const scores3[], int const totals[], int students, double ave, int mini, int maxi, int n);
void header ();
void body(int const exam1, int const exam2, int const exam3, int total_grade, int st_id, double ave);
void bottom (int students, double ave, int mini, int maxi, int n);
int main()
{
    int * scores1;
    int * scores2;
    int * scores3;
    int * totals;
    int students(0);
    double avg(0.0);
    int min, max, above;
    string prompt = "Enter number of students: ";
    students = student_num (prompt);
    scores1 = new int [students];
    scores2 = new int [students];
    scores3 = new int [students];
    totals = new int [3];
    scores (scores1, scores2,scores3,students);
    sum (scores1, scores2, scores3, totals, students);
    int ave = average (totals, students);
    int maxi = find_max(totals, students);
    int mini = find_min(totals, students);
    int n = above_ave (totals, students, ave);
    display (scores1, scores2, scores3, totals, students, ave, mini, maxi, n);
    delete scores1;
    delete scores2;
    delete scores3;
    delete totals;
    return 0;
}
int student_num (const string & prompt)
{
    int students, m;
      cout << prompt;
      cin >> students;
          }
return students;
}

void scores (int scores1[], int scores2[],int scores3[], int students)
{
    for (int i(0); i<students; i++)
    {
        cout << "Enter the three scores" << i+1<< ":";
        cin >> scores1 [i];
        cin >> scores2 [i];
        cin >> scores3 [i];
    }
}
void sum (int scores1[], int scores2[],int scores3[], int totals[], int students)
{
    for (int i(0); i< students; i++)
    {
        totals[i] = scores1[i] + scores2[i] + scores3[i];
    }
}
double average (int totals[], int students)
{
    double ave(0), sum (0);
    for (int i(0); i<students; i++)
    {
        sum += totals [i];
    }
    ave = sum/students;
    return ave;
}
double find_max(const int totals[], const int students)
{
    double maxi(0); // maximum value of the array
    for (int i=0; i<students; i++) //i= loop variable
    {
        if (totals[i] > maxi)
        {
            maxi = totals[i];
        }
    }
    return(maxi);
}
double find_min(const int totals[], const int students)
{
    double mini = totals [0];
    for (int i=1; i<students; i++)
    {
        if (totals[i] < mini)
        {
            mini = totals[i];
        }
    }
    return(mini);
}
int above_ave (const int totals[], int students, double ave)
{
    int n(0), i(0);
    for (int i(0); i<students; i++)
    {
        if (totals[i] > ave)
        {
            n++;
        }
    }
    return n;
}
void display (int const scores1[], int const scores2[], int const scores3[], int const totals[], int students, double ave, int mini, int maxi, int n)
{
    header();
    for (int i(0); i<students; i++)
    {
        int exam1 = scores1[i];
        int exam2 = scores2[i];
        int exam3 = scores3[i];
        int total_grade = totals[i];
        int st_id = i;
        body (exam1, exam2, exam3, total_grade, st_id, ave);
    }
    bottom (students, ave, mini, maxi, n);
}
void body (int const exam1, int const exam2, int const exam3, int total_grade, int st_id, double ave)
{
    cout << setw(7) << right << fixed << st_id+1;
    cout << setw(10) << left << fixed << exam1;
    cout << setw(10) << left << fixed << exam2;
    cout << setw(10) << left << fixed << exam3;
    cout << setw(5) << left << fixed << total_grade;
    if (total_grade > ave)
    {cout << setw(2)<<right << "+"<< endl;}
    else
    {cout << setw(2)<<right << "-"<< endl;}
}
void bottom (int students, double ave, int mini, int maxi, int n)
{
   cout << setw(40) << left << "The number of students is:" << setw(7) << left << students<< endl;
    cout << setw(40) << left << "The avg total score (rounded) is" << setw(7) << left << int (ave)<< endl;
    cout << setw(40) << left << "The maximum total score is:" << setw(7) << left << maxi << endl;
    cout << setw(40) << left << "The minimum total score is:" << setw(7) << left << mini << endl;
    cout << setw(40) << left << "Total scores at or above the avg is:" << setw(7) << left << n << endl;

}
14
  • Can you please show the line and the compiler error? Commented Nov 16, 2013 at 4:09
  • Ehh. If your teacher told you you had to write code like this, with these exact interfaces, he's doing you a disservice. Personally, i'd have a Student class/struct holding the grades, and pass one vector (not pointer, not array) of Students, rather than three separate arrays of related data. Commented Nov 16, 2013 at 4:19
  • I know, these assignments are useless. can you help me with it? Commented Nov 16, 2013 at 4:24
  • @FKaria just updated the question. :) Commented Nov 16, 2013 at 4:24
  • 1
    You are indexing the scores arrays starting at 1 in the scores function. You should start at zero and add 1 to i in your cout call instead of starting i at 1. Commented Nov 16, 2013 at 4:58

1 Answer 1

2

The line

for (int i(1); i<students+1; i++)

in the scores function is offset by 1 from what it needs to be. You are not printing the scores that you think you are, and you are lucky that you are not getting a segfault for exceeding the limits of the arrays.

Sign up to request clarification or add additional context in comments.

2 Comments

Since I want to display the id for each student (1, 2, 3,..) I did it. Thanks, I'm changing it
Either add 1 to print out the student ID, or subtract 1 for the indexing. Or call the first student "student 0". It has a certain Dr Who quality (like "Prisoner Zero").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.