0
Could someone help me on the following question:
Create a program that fills a 6x4 matrix with integers, calculate and show how many elements of this matrix are greater than 30, and then assemble a second matrix with the different elements of 30. In place of number 30, of the second matrix, put the number zero.
I made the following code but this giving the following error: stack Smashing Detected: terminated and I’m not getting to know why. I’m using Netbeans as an IDE.
#include <stdio.h>
#include <stdlib.h>
#define L 2
#define C 2
int main(int argc, char** argv) {
int matriz[L][C], mat2[L][C];
int i, j, pL = 0, pC = 0, contMaior30 = 0, contIgual30 = 0, contDif30 = 0;
for (i = 0; i < L; i++) {
for (j = 0; j < C; j++) {
printf("M[%d][%d] = ", i, j);
scanf("%d", &matriz[i][j]);
}
}
//Laço para fazer a verificação dos valores digitados
for (i = 0; i < L; i++) {
for (j = 0; j < C; j++) {
if (matriz[i][j] <= 30) {
if (matriz[i][j] == 30) {
mat2[pL][pC] = 0;
} else if (matriz[i][j] < 30) {
mat2[pL][pC] = matriz[i][j];
}
contDif30++;
pL++;
pC++;
}
//Contar a quantidade de números maiores que 30
else {
contMaior30++;
}
}
}
//Não sei como controlar a impressão de segunda matriz
printf("\nImpressão do segundo vetor: \n");
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
printf("\tM2: %d \n", mat2[i][j]);
}
}
return (EXIT_SUCCESS);
}
Fight man, but I still don’t understand how I’m going to make the control of the for that will print the second matrix. Could help me in this tbm?
– Thiago Henrique Domingues
@Thiagohenriquedomingues No problem. Which control ? The printing of the second matrix is not as you want ? What is missing ?
– Isac
In order to count the values <= 30. Because I created a 2x2 matrix for tests only, the exercise requires a 6x4. In case you would need a variable to count these values, for when printing do not print every matriz2;
– Thiago Henrique Domingues
I don’t know if it’s clear.
– Thiago Henrique Domingues
@Thiagohenriquedomingues The size of the matrix does not influence logic. And the count of those over 30, under 30 and equal to 30 is already being done. Which count is missing ? " for when printing not to print every matriz2" - which ones are not to print ? Could you give an example of a correct print ?
– Isac
Input: 50, 6, 1 ,30 | Expected exit: 6,1,0 | Exit obitida: 654948592,6,1,0
– Thiago Henrique Domingues
@Thiagohenriquedomingues But the
50
doesn’t show up on the way out because it’s bigger than30
that’s it ?– Isac
I would like you to print some values less than or equal to 30 (where it will be replaced by 0)
– Thiago Henrique Domingues
Especially, I wanted to find a way to skip the position 50 is and print the others
– Thiago Henrique Domingues
@Thiagohenriquedomingues I already edited the answer to contemplate this part as well. See if that’s what I was looking for.
– Isac
Exactly, I was going to tell you that I had already solved, I did the same thing that you did.
– Thiago Henrique Domingues
Thank you so much for your help, man, and I’m sorry about the haha grind
– Thiago Henrique Domingues
@Thiagohenriquedomingues No problem. We’re here to help :)
– Isac