带有指针的 void 函数

void function with pointers

本文关键字:void 函数 指针      更新时间:2023-10-16

该程序读取每个学生的学生人数和三个分数(考试),然后计算平均值,然后以表格格式显示信息。 由于某种原因,它不起作用。我必须使用 11 个函数编写此程序,因此无法更改格式、函数数量或主过程的任何重大更改。更新:我做了一些更改,现在我可以编译并运行它,它显示了所有内容

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;
}

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

scores 函数中,与它需要的偏移量偏移 1。您没有打印您认为的分数,并且您很幸运,您没有因为超出数组的限制而出现段错误。