Mean of matrix columns

Asked

Viewed 57 times

0

Questão

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;
}
  • 1

    if((l==1)&&(l==3)) that if is wrong, it is impossible for the variable "l" to be 1 and 3 at the same time, just like that other if if((l==0)&&(l==1))

  • 1

    What a silly mistake rs, had modified one if and forgot the other. I changed the two and it worked, thanks for the help.

No answers

Browser other questions tagged

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