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: 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
– rray
It smells like memory junk because of
maior=mat[0][0]
... Try to start themaior
with zero.– DH.
Looks like we missed initializing even the biggest.
– Rodrigo Guiotti
It worked. And because smaller is not necessary?
– user41836
@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.
– Bacco
@user41836 may be necessary yes, but maybe you haven’t tested with very high numbers. Try the lowest with all very large numbers.
– Bacco
How many decimal places an int can hold?
– user41836
@user41836 int has no decimal number, so it is called "integer".
– Bacco
@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.
– Bacco
I mean how many digits, when I type a very high value, it shows another value.
– user41836
@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.
– Bacco
Well, now I realize, I’d forgotten about it.
– user41836
@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.
@DH however they have already chosen one. It is for another occasion :)
– Bacco