如何找到不知道它大小的矩阵的最大值

发布于 2025-01-18 18:00:14 字数 1843 浏览 0 评论 0原文

变量“学生”由用户给出,然后我必须在函数中使用它来找到矩阵的最大值。请注意,“学生”是矩阵的行,我必须使用它。 “学生”需要包括在该功能的论点中。 同样,更好的功能无法正常工作,我无法弄清楚原因。 这些是编译器消息:

警告:从不兼容的指针类型[-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小帐篷 2025-01-25 18:00:15
#include <stdio.h>
#define TESTS 5

int better(int students, int grades[students][TESTS]){
    
    int max = 0;

    for(int i = 0; i < students; i++){
        
        for(int j = 0; j < TESTS; j++){
        
            if(max < grades[i][j]){
                max = grades[i][j];
            }

        }
    }
    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++){
        printf("Enter values for No.%d student:\n", i+1);
        for(int j = 0; j < TESTS; j++){

        printf("Enter a value: ");
        scanf("%d", &value);
        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(students, grades);
    printf("Max grade is: %d", MAX);
    return 0;
}
#include <stdio.h>
#define TESTS 5

int better(int students, int grades[students][TESTS]){
    
    int max = 0;

    for(int i = 0; i < students; i++){
        
        for(int j = 0; j < TESTS; j++){
        
            if(max < grades[i][j]){
                max = grades[i][j];
            }

        }
    }
    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++){
        printf("Enter values for No.%d student:\n", i+1);
        for(int j = 0; j < TESTS; j++){

        printf("Enter a value: ");
        scanf("%d", &value);
        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(students, grades);
    printf("Max grade is: %d", MAX);
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文