LOWEST VALUE OF EACH MATRIX ROW

Asked

Viewed 45 times

0

I need to find the lowest value of each matrix row. The values appear missing or the most (much more). Enunciado da questão

What I have so far:

#include <stdio.h>

#include <stdlib.h>
 
int main()
{

    float notas[][3] = {0};

    float menor = 0.0;
    
    //preenchendo matriz
    for(int aluno = 0; aluno < 10; aluno++)
    {
        for(int prova = 0; prova < 3; prova++)
        {
            notas[aluno][prova] = rand() % 10;    
        }
    }
    
    //Exibindo as notas
    printf("\t  COL 0      COL 1      COL 2\n");
    for(int aluno = 0; aluno < 10; aluno++)
    {
        printf("LINHA %d  - ", aluno);
        for(int prova = 0; prova < 3; prova++)
        {
            printf("%.2f      ", notas[aluno][prova]);   
        }
        printf("\n");
    }
 
    int Pior = 0;
    int Vpior[3] = {0};
 
    for(int aluno = 0; aluno < 10; aluno++)
    {
        Pior = 0;
        menor = notas[0][0];
        for(int prova = 0; prova < 3; prova++)
        {
            if(notas[aluno][prova]<menor)//
            {
                menor = notas[aluno][prova];
                Pior++;
            } 
        }
        Vpior[aluno] = Pior;
   }

    printf("\n");

    for(int r = 0; r < 3; r++){
        printf("Piores na:\nProva %d = %d\n", 
        r+1, Vpior[r]);
    }
    return 0;
} 
  • Your grade statement is wrong, use: float notas[10][3] = {0};. Note that to use notas[aluno][prova] = rand() % 10; no one can take a 10, the notes will vary between 0 and 9. Here: menor = notas[0][0]; should be menor = notas[aluno][0];. First you have to determine the worst grade and then sweep all students to count those who took this grade (you are counting how many times you changed the worst grade). Be careful to count the worst grade of a student only once.

  • I made the modification you said, but the result was the same as before, still, thanks for the help.

1 answer

0


According to the comment would be:

#include <stdio.h>
#include <stdlib.h>
 
int main() {
    float notas[10][3] = {0};
    //preenchendo matriz
    for(int aluno = 0; aluno < 10; aluno++) {
        for(int prova = 0; prova < 3; prova++) {
            notas[aluno][prova] = (float) (rand() % 101) / 10;
        }
    }
    //Exibindo as notas
    printf("\t  COL 0  COL 1  COL 2\n");
    for(int aluno = 0; aluno < 10; aluno++) {
        printf("LINHA %d  - ", aluno);
        for(int prova = 0; prova < 3; prova++) {
            printf("%.2f  ", notas[aluno][prova]);   
        }
        printf("\n");
    }
     
    int Vpior[3] = {0, 0, 0};
     
    for(int aluno = 0; aluno < 10; aluno++) {
        int menor = 0;
        for(int prova = 0; prova < 3; prova++) {
            if(notas[aluno][prova] < notas[aluno][menor]) {
                menor = prova;
            }  
        }
        Vpior[menor]++;
    }

    printf("\n");

    for(int r = 0; r < 3; r++){
        printf("Piores na:\nProva %d = %d\n", r+1, Vpior[r]);
    }
    return 0;
} 

See working in https://ideone.com/GErQab

  • Thank you so much for your help. I would like to understand better, why put smaller in the test place, in the IF?

  • Instead of keeping the lowest grade, the lowest grade index is kept (it will always be considered the first found, even if there are equal grades), so it is easier to accumulate per test (Vpior).

  • Thank you very Much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.