C++ 从数组中获取值并计算平均值,然后存储在数组中
我最近学习了二维数组。问题在AverageScores函数中。我试图计算每个学生的平均值,然后将计算出的值存储到不同的数组中,然后打印出来。我遇到了一些问题,但我知道我需要对这行值求和,计算平均值,打印,然后转到下一个学生 编辑:当我运行程序时,程序根本不计算平均值,而是打印测试数和一个不正确的负值。 例如,如果有两个测试和两个学生,程序将为第一个学生打印两次-9.25596e+61,然后为第二个学生打印两次C++ 从数组中获取值并计算平均值,然后存储在数组中,c++,arrays,multidimensional-array,average,C++,Arrays,Multidimensional Array,Average,我最近学习了二维数组。问题在AverageScores函数中。我试图计算每个学生的平均值,然后将计算出的值存储到不同的数组中,然后打印出来。我遇到了一些问题,但我知道我需要对这行值求和,计算平均值,打印,然后转到下一个学生 编辑:当我运行程序时,程序根本不计算平均值,而是打印测试数和一个不正确的负值。 例如,如果有两个测试和两个学生,程序将为第一个学生打印两次-9.25596e+61,然后为第二个学生打印两次 #include <iostream> #include <ioma
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
//constant declarations
const int MAX_STUDENTS = 30; //maximum number of students
const int MAX_TESTS = 10; //maximum number of tests
//function prototypes
void intro();
void ReadScores(double[][MAX_TESTS], int&, int&);
void PrintScores(const double[][MAX_TESTS], int, int);
void AverageScores(const double[][MAX_TESTS],
int,
int,
double[]);
int main()
{
//variable declarations
double scores[MAX_STUDENTS][MAX_TESTS],//array of test scores
studentAvgs[MAX_TESTS];
int numberOfStudents; //number of students in a class
int numberOfTests; //number of tests written
intro();
//read the each student’s test scores into an array scores
ReadScores(scores, numberOfStudents, numberOfTests);
//print each student’s scores
PrintScores(scores, numberOfStudents, numberOfTests);
AverageScores(scores, numberOfStudents, numberOfTests, studentAvgs);
_getch();
return 0;
}
void ReadScores(double scores[][MAX_TESTS], //array of test scores
int& numberOfStudents, //number of students read
int& numberOfTests) //number of tests read
{
int student; //row index used for students
int test; //column index used for tests
//prompt for and read the number of students and the number of tests
cout << "Enter the number of students(up to " << MAX_STUDENTS << ") :";
cin >> numberOfStudents;
cout << "Enter the number of tests(up to " << MAX_TESTS << ") : ";
cin >> numberOfTests;
//read the test scores into the array scores
for (student = 0; student < numberOfStudents; student++)
{
cout << "Enter the " << numberOfTests
<< " test scores for student# " << (student + 1) << endl;
for (test = 0; test < numberOfTests; test++)
cin >> scores[student][test];
}
}
void PrintScores(const double scores[][MAX_TESTS],
int numberOfStudents,
int numberOfTests)
{
int student;
int test;
for (student = 0; student < numberOfStudents; student++)
{
cout << "The test scores for student# " << (student + 1)
<< " are: " << endl;
for (test = 0; test < numberOfTests; test++)
cout << setw(3) << scores[student][test];
cout << endl << endl;
}
}
void intro() {
cout << setw(46) << "Welcome\n\n";
}
void AverageScores(const double scores[][MAX_TESTS],
int numberOfStudents,
int numberOfTests,
double studentAvgs[]){
int student,
test,
accumilator = 0;
for (student = 0; student < numberOfStudents; student++)
{
cout << "The average test score for student# " << (student + 1)
<< " is: " << endl;
for (test = 0; test < numberOfTests; test++) {
accumilator += scores[numberOfStudents][numberOfTests];
studentAvgs[test] = accumilator / numberOfTests;
cout << studentAvgs[numberOfTests] << endl;
}
}
}
#包括
#包括
#包括
使用名称空间std;
//常量声明
const int MAX_学生=30//学生人数上限
const int MAX_TESTS=10//最大测试次数
//功能原型
void intro();
无效读分数(双[][MAX_TESTS],int&,int&);
无效打印分数(常量双[][最大测试],整数,整数);
无效平均分数(常数双[]最大测试],
int,
int,
双[]);
int main()
{
//变量声明
双倍分数[MAX_STUDENTS][MAX_TESTS],//测试分数数组
学生VGS[最大测试];
int numberOfStudents;//一个班级的学生人数
int numberOfTests;//编写的测试数
简介();
//将每个学生的考试分数读入一个分数数组
阅读分数(分数、学生人数、测试人数);
//打印每个学生的分数
打印分数(分数、numberOfStudents、numberOfTests);
平均分数(分数、学生人数、测试人数、学生平均成绩);
_getch();
返回0;
}
无效读取分数(双倍分数[][MAX_TESTS],//测试分数数组
int&numberofstudent,//读取的学生数
int&numberOfTests)//读取的测试数
{
int student;//用于学生的行索引
int test;//用于测试的列索引
//提示并阅读学生人数和考试次数
cout相同,但使用STL(用于咯咯笑):
<代码>累加器+ =分数[学生数] [NoMultFoestTys];使用循环索引变量,而不是循环最大值。请考虑创建一个最小的、完整的和可验证的示例。
#include <iostream>
#include <string>
#include <vector>
#include <utility>
#include <numeric>
int main()
{
std::vector<std::pair<std::string, std::vector<double>>> studentScores;
studentScores.push_back(std::make_pair(std::string("Bob"), std::vector<double>{ 1.23, 3.23, 0.55 }));
studentScores.push_back(std::make_pair(std::string("John"), std::vector<double>{ 2.09, 2.22, 4.55, 1.28, 4.99 }));
studentScores.push_back(std::make_pair(std::string("Mary"), std::vector<double>{ 4.11, 0.2, 0.55, 3.88}));
for (auto studentScore : studentScores)
{
std::cout << studentScore.first << "'s average score is: ";
std::cout << std::accumulate(studentScore.second.begin(), studentScore.second.end(), static_cast<double>(0)) / static_cast<double>(studentScore.second.size()) << std::endl;
}
return 0;
}
Bob's average score is: 1.67
John's average score is: 3.026
Mary's average score is: 2.185