如何找到不知道它大小的矩阵的最大值
变量“学生”由用户给出,然后我必须在函数中使用它来找到矩阵的最大值。请注意,“学生”是矩阵的行,我必须使用它。 “学生”需要包括在该功能的论点中。 同样,更好的功能无法正常工作,我无法弄清楚原因。 这些是编译器消息:
警告:从不兼容的指针类型[-wiNcompatible-Pointer-types]传递“更好”的参数1] 73 | int max =更好(*等级,学生);
注意:预期的“ int **”,但参数是类型'int *' 5 | int更好(int ** pin,int学生){
我还会收到一个警告,以将指针与函数中的整数进行比较()
我的输入数字是1 2 0 0 0 0 0 0 3 5 1 10和返回的最大值等于2。
#include <stdio.h>
#define TESTS 5
int better(int **pin,int students){
int max = 0;
int k = 0;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(*(pin + k) >= 0 && *(pin + k) <= 10){
if(max < *(pin + k)){
max = *(pin + k);
}
}
k++;
}
}
return max;
}
int main(){
int students;
printf("Grades should always be a number between 0-10!\n\n");
printf("Enter number of students: ");
scanf("%d", &students);
int grades[students][TESTS], value;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("Enter a value: ");
scanf("%d", &value);
if(value < 0){continue;}
if(value > 10){continue;}
grades[i][j] = value;
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(grades[i][j] < 0 || grades[i][j] > 10){
grades[i][j] = 0;
}
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("%d ", grades[i][j]);
}
printf("\n");
}
int max = better(*grades,students);
printf("\nmax of grades is: %d", max);
return 0;
}
如果SB可以帮助我,我将不胜感激!
the variable "students" is given by the user and then I have to use it in a function to find the max of a matrix. Note that "students" is the matrix's rows and I have to use it.
"students" NEEDS to be included in the function's arguments.
Also the better function isn't working properly and I cant figure out why.
These are the compiler messages:
warning: passing argument 1 of 'better' from incompatible pointer type [-Wincompatible-pointer-types]
73 | int max = better(*grades,students);
note: expected 'int **' but argument is of type 'int *'
5 | int better(int **pin,int students){
I am also getting a warning for comparing a pointer to an integer in function better()
My inputed numbers are 1 2 0 0 0 0 3 5 1 10 and the max that is returned is equal to 2.
#include <stdio.h>
#define TESTS 5
int better(int **pin,int students){
int max = 0;
int k = 0;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(*(pin + k) >= 0 && *(pin + k) <= 10){
if(max < *(pin + k)){
max = *(pin + k);
}
}
k++;
}
}
return max;
}
int main(){
int students;
printf("Grades should always be a number between 0-10!\n\n");
printf("Enter number of students: ");
scanf("%d", &students);
int grades[students][TESTS], value;
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("Enter a value: ");
scanf("%d", &value);
if(value < 0){continue;}
if(value > 10){continue;}
grades[i][j] = value;
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
if(grades[i][j] < 0 || grades[i][j] > 10){
grades[i][j] = 0;
}
}
}
for(int i = 0; i < students; i++){
for(int j = 0; j < TESTS; j++){
printf("%d ", grades[i][j]);
}
printf("\n");
}
int max = better(*grades,students);
printf("\nmax of grades is: %d", max);
return 0;
}
I'd appreciate it if sb could help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)