0
The code is wrong to print the average and fill the second matrix.
The code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float matriz[3][6];
float SomaImp = 0, SomaDQ = 0, SomaPS = 0;
//preenchendo matriz
for(int r = 0; r < 3; r++)
{
for(int l = 0; l < 6; l++)
{
matriz[r][l] = (rand() % 257)/10;
}
}
//Exibindo matriz
for(int r = 0; r < 3; r++)
{
for(int l = 0; l < 6; l++)
{
printf("%.2f ", matriz[r][l]);
}
printf("\n");
}
//Somando as colunas impares
for(int r = 0; r < 3; r++)
{
for(int l = 0; l < 6; l++)
{
if((l%2!=0)&&(l!=0))
{
SomaImp += matriz[r][l];
}
}
}
//media da 2a e 4a colunas
for(int r = 0; r < 3; r++)
{
for(int l = 0; l < 6; l++)
{
if((l==1)&&(l==3))
{
SomaDQ += matriz[r][l];
}
}
}
//alterando os valores da 6a colunas
for(int r = 0; r < 3; r++)
{
for(int l = 0; l < 6; l++)
{
if((l==0)&&(l==1))
{
SomaPS += matriz[r][l];
}
}
matriz[r][5] = SomaPS;
}
printf("\nSoma das coluna impares %.2f, Média das colunas 2 e 4 %.2f\n\n",
SomaImp, SomaDQ);
printf("Esta é a matriz alterada\n");
for(int r = 0; r < 3; r++)
{
for(int l = 0; l < 6; l++)
{
printf("%.2f ", matriz[r][l]);
}
printf("\n");
}
return 0;
}
if((l==1)&&(l==3))
thatif
is wrong, it is impossible for the variable "l" to be 1 and 3 at the same time, just like that other ifif((l==0)&&(l==1))
– Ricardo Pontual
What a silly mistake rs, had modified one if and forgot the other. I changed the two and it worked, thanks for the help.
– Rafael S.