0

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!

  • 1
    `scanf("%d", &value); if(value < 0){continue;} if(value > 10){continue;}` You're skipping here. You need warn the `data-feeder`(user) to enter values between `[0..10]` and read it again. – जलजनक Apr 01 '22 at 16:09
  • @SparKot I appreciate your concern about this but this isnt the problem i am facing right now. let me know if you can help! – Dns Stratos Apr 01 '22 at 16:15
  • What is the problem? You could replace `int max = 0;` with `int max = pin[0][0];` in function `better()`, but given that the values are limited to 0..10, setting it to 0 isn't a problem. The code of the `better()` function looks fine, even though I'd prefer the macro `tests` to be in upper-case — that's the standard convention for constants. As already noted, the input code is a bit dubious. Have you printed the `grades` array to check what you get? – Jonathan Leffler Apr 01 '22 at 16:19
  • @JonathanLeffler yeah the input part is working just fine. It's the better() function thats not working properly, it will not calculate the max. As for the macro tests, i didnt know about that , thanks for telling me. I will now update the code because i decided to do it with pointers but i still havent found the solution – Dns Stratos Apr 02 '22 at 17:17
  • https://stackoverflow.com/questions/14548753/passing-a-multidimensional-variable-length-array-to-a-function – pm100 Apr 02 '22 at 17:44
  • @pm100 unfortunately no, i am still trying to figure this out – Dns Stratos Apr 02 '22 at 18:06

1 Answers1

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;
}