I need to know where in that code is my mistake - MATRIX C

Asked

Viewed 69 times

3

In the letter i was to give 9,2 but the result that shows is 9,50.

How to find out which class has the most homogeneity in relation to grades?

    float turma1[4][5] ={
                       {8.2,9.2,8.7,6.0,7.0},
                       {3.0,3.4,9.2,9.8,1.9},
                       {4.0,5.2,8.2,7.2,8.7},
                       {10,4.8,8.2,6.7,8.2}
                        };
    float turma2[4][5] ={
                        {6.5,6.5,7.2,7.2,7.8},
                        {9.0,9.2,9.1,8.0,8.2},
                        {3.5,7.9,6.9,9.2,9.2},
                        {8.9,9.2,9.7,10,8.5}
                        };
    int i,j;

 //i) Qual nota foi a mais frequente na turma 2?
    float notaMaisFrequente;

    for(i=0;i<4;i++){
        for(j=0; j<5;j++){
            if(turma2[i][j] == turma2[i][j]){
                turma2[i][j] = turma2[i][j]+1;
                notaMaisFrequente = turma2[i][j];
            }
        }
}

printf("Nota mais frequente na Turma 2: %.2f",notaMaisFrequente);
  • I compiled here and appeared: Student 0 of Class 2 had the highest average. Most frequent grade in Class 2: 9.50

  • I’m analyzing your code, there’s a logic error on the condition: if(class 2[i][j] == class 2[i][j]) I’m just thinking of a way for a variable to receive which vector repeats more

  • My friend, I’ll have to leave soon, so I’ll try to answer your question until tomorrow, in case I don’t make it, I’ll offer a reward for someone from the community to help!

  • Okay, I’m still grateful you tried to help me

  • The error, as has already been said, is in the if(turma2[i][j] == turma2[i][j]), what you have to do is save all the numbers that came out and, in case the number has already come out once, you have to add 1 in the number of times it came out...

2 answers

2


Using brute force, you could solve the letter (i) like this:



//i) Qual nota foi a mais frequente na turma 2?
    float notaMaisFrequente = 0;
    int quantidade = 0;

    int q = 0;
    int a, b;

   for(i=0;i<4;i++){
      for(j=0; j<5;j++){
         for(a = 0; a < 4; a++){
            for(b = 0; b < 5; b++){
               if(turma2[i][j] == turma2[a][b]){
                  q++;
               }
            }
         }
         if(q > quantidade){
            quantidade = q;
            notaMaisFrequente = turma2[i][j];
         }
         q = 0;
      }
   }

printf("Nota mais frequente na Turma 2: %.2f",notaMaisFrequente);

If a more efficient algorithm is required, I imagine that would inevitably require a data structure with dynamic memory allocation, to store the different notes and the amount of times each is repeated. This could get pretty messy.

  • Thank you very much, it helped a lot :D

0

if(turma2[i][j] == turma2[i][j]){ // Se 8,5 é igual a 8,5
    turma2[i][j] = turma2[i][j]+1; // Soma 8,5+1 e sobrepõem o valor de turma2[i][j]
    notaMaisFrequente = turma2[i][j]; // notaMaisFrequente = 9,5

The result is 9.5 because in this part of the code you just check if a number is equal to itself, then that if will always be true. So the value of noteMaisFrequent will be the last class value2 summed 1.

  • @bbyink if you want a hint on how to solve your problem follow the code link: https://pastebin.com/SMWyuDvW

  • Ah, vdd... had not come to that reasoning :D Thank you

Browser other questions tagged

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