【发布时间】:2021-06-23 08:17:54
【问题描述】:
所以这是我的代码,它试图使用不同的方法找到最高的平均值,我设法得到了前两个,但我被困在第三种方法上,它需要一个指向 student(struct) 类型指针的指针。
#include <iostream>
using namespace std;
struct student
{
char Name[100];
float avg;
};
student highest(student A[], int size) {
student max = A[0];
for (int i = 1; i < size; i++)
if (max.avg < A[i].avg)
max = A[i];
return max;
}
student* highest1(student A[], int size) {
int highest = 0;
for (int i = 0; i < size; i++) {
if (A[highest].avg < A[i].avg)
highest = i;
}
student* max = &A[highest];
return max;
}
student highest2(student** A, int row, int col) {
int maxi=0, maxj=0;
int i = 0, j = 0;
for ( i = 0; i < row; i++) {
for ( j = 0; j < col; j++)
if (A[maxi][maxj].avg < A[i][j].avg)
maxi = i; maxj = j;
}
return A[maxi][maxj];
}
int main() {
cout << "please enter teh number of students " << endl;
int x;
int row, col, test;
cin >> x;
student *A = new student[x];
student B[3][2];
cout << "please enter the name of the "<<x<<" students " << endl;
for (int i = 0; i < x; i++) {
gets_s(A[i].Name, 100);
}
cout << "please enter the average for" <<x<<" students " << endl;
for (int i = 0; i < x; i++)
cin >> A[i].avg;
//cout << "the highest grade is " << highest(A,x).avg << endl;
//cout << highest1(A, x)->avg;
highest2(B, 3, 2);
}
当然,在将信息输入 3x2 矩阵后,我必须检查一下,这就是我不明白如何将其发送到函数的问题,我尝试了一些方法,但我会得到代码将只检查 1 行的逻辑错误。
【问题讨论】:
-
student B是一个二维的未初始化结构。你在这里的意图是什么:highest2(B, 3,2);?也许您需要弄清楚为什么B存在以及它的目的是什么,因为从您发布的代码中很难分辨。 -
另外,
student**不是学生的二维数组。所以就在那里,你的函数声明是一个非首发。 -
student**是指向指针的指针 -
@Osama 我担心你会这么说。这种风格的 C++ 与行业标准相去甚远。我理解低级操作的教学吸引力,我只是想知道使用 C 来教他们是否更好。
-
@AlessandroTeruzzi 我怎样才能获得更多我明年申请谷歌实习的工业材料作为开始,但我不知道之后我会从哪里开始