Largest number in array, does not display

Asked

Viewed 72 times

0

This code does not show how the largest element inserted in the matrix should be or the correct position. What’s the mistake?

#include <stdio.h>
#include <stdlib.h>
#define lin 4
#define col 4
int main()
{
   int mat[lin][col], i, j, maior=mat[0][0], pos_i, pos_j;

   printf("Informe os elementos da matriz\n");
   for(i=0;i<lin;i++){
      for(j=0;j<col;j++){
         printf("[%d][%d] = ", i,j);
         scanf("%d", &mat[i][j]);
         if(mat[i][j] > maior) {
            maior=mat[i][j];
            pos_i=i;
            pos_j=j;
         }
        }
   }
   printf("O maior elemento da matriz: %d\n", maior);
   printf("Posicao: [%d][%d]\n", pos_i,pos_j);
   return 0;
}

So he displays: inserir a descrição da imagem aqui For lower matrix value it displays correctly, but for higher not.

  • Could it be a little more specific? perhaps a practical example of what the mistake would be

  • 5

    It smells like memory junk because of maior=mat[0][0]... Try to start the maior with zero.

  • 1

    Looks like we missed initializing even the biggest.

  • It worked. And because smaller is not necessary?

  • 2

    @DH besides being correct what you said, it wouldn’t make any sense to take the "largest" of a matrix that has not yet received data.

  • @user41836 may be necessary yes, but maybe you haven’t tested with very high numbers. Try the lowest with all very large numbers.

  • How many decimal places an int can hold?

  • @user41836 int has no decimal number, so it is called "integer".

  • 1

    @DH doesn’t want to post an answer? I think it would be fair for the chosen one to be yours because you first identified the error.

  • I mean how many digits, when I type a very high value, it shows another value.

  • @user is not "digits", but the highest value. It is usually 2147483647, and this minus one in the negative. If it is unsigned int, then what would be negative you get the more in positive.

  • Well, now I realize, I’d forgotten about it.

  • 2

    @Bacco nah, no stress with the reputation, what matters is to help :) Besides, I just looked at it while building on the company system, nor should I be looking here aushauhsuahsua

  • @DH however they have already chosen one. It is for another occasion :)

Show 9 more comments

3 answers

0

You didn’t initialize your matrix and initialized higher with the value of mat[0][0], which you do not know which is.

Try it this way:

   int mat[lin][col];
   int i, j, pos_i, pos_j;

   /* inicializa a matriz */
   for(i = 0; i < lin; i++){
      for(j = 0; j < col; j++){
          mat[i][j] = 0;
      }
   }

   int maior;

   printf("Informe os elementos da matriz\n");
   for(i = 0; i < lin; i++){
      for(j = 0; j < col; j++){
         printf("[%d][%d] = ", i,j);
         scanf("%d", &mat[i][j]);

         /* atualiza maior se for a primeira interacao, */
         /* ou se o valor corrente for maior            */
         if( (i == 0 && j == 0) || (mat[i][j] > maior) ) {
            maior=mat[i][j];
            pos_i=i;
            pos_j=j;
         }
      }
   }

0

You should always initialize the variables and initialize a variable with another that has not initialized does not resolve, as done on the line:

maior=mat[0][0]

The variables in C and C++ will come with the values that were previously in the memory to gain a small advantage... this means that it is not guaranteed that they start with 0.

It was done so because at the time it was a significant performance advantage (if you were to assign a value to the variable later and use it). Nowadays it’s not much anymore and should always boot, but this work today is done by the programmers of this language.

0


int main()
{
   int mat[lin][col], i, j, maior=mat[0][0], pos_i, pos_j;
   //                             ^^^^^^^^^ LIXO

When you do the assignment to maior the content of mat[0][0] is junk.
You have to do that assignment afterward to assign values to the array.

Browser other questions tagged

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