带有指针的 void 函数
void function with pointers
该程序读取每个学生的学生人数和三个分数(考试),然后计算平均值,然后以表格格式显示信息。 由于某种原因,它不起作用。我必须使用 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。您没有打印您认为的分数,并且您很幸运,您没有因为超出数组的限制而出现段错误。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 如何将指针从一个void函数传递到另一个C++
- 为什么我在使用void函数时得到错误代码C2276
- 指针没有更新它在void函数内部指向的值
- 为什么我们要为avl树实现返回一个指向节点的指针,而不是void函数
- void 函数可以传递值参数吗?
- 在 void 函数中使用 #include 变量C++
- 如何在 C++ 中将 void 函数传递给 void 指针
- 使用运算符调用 void 函数时出错<set>
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- C++ :: 如何捕获由 void 函数创建的矩阵
- 如何将一个 void 函数传递给另一个 void 函数?
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- C++命名空间中调用 void 函数的错误
- 当数组位于两个循环之间时,您可以调用数组的 void 函数吗?
- 结构新手,我对如何从 void 函数中返回值并将其放入另一个函数感到困惑
- 查找素数:错误:控件到达非void函数的末尾