Use if with matrices

Asked

Viewed 93 times

0

The question ends up being logically easy, but I’m having difficulty knowing how to use the if condition in the values of the matrix, I don’t know how to do to mess with the values of the matrix. my code got just below but I get lost in how to fit the if

inserir a descrição da imagem aqui

#include "stdio.h"

int main(void) {

float MatOriginal[3][5],MatFinal[3][5];

// entrada de dados
printf("escreva os valores da matriz original: \n");
  for(int i=0;i<3;i++){
    for(int j=0;j<5;j++){
  printf("elemento (%d)(%d)",i+1,j+1);
  scanf("%f",&MatOriginal[i][j]);
}
  }
  if(MatOriginal[3][5]<0)
    MatOriginal[3][5] = - MatOriginal[3][5]
// escrita da matriz
printf(" \n A matriz original informada é = \n");
for(int i=0; i<3; i++)
{
  printf(" \n ");
  for(int j=0; j<5; j++){
     printf(" %4.1f ", MatOriginal[i][j]);
}
}
return 0;
}

1 answer

1

First I’ll reformat your code to make it very visible. I’ll take the part where you put the if because I was wrong:

#include "stdio.h"

int main(void) {

    float MatOriginal[3][5], MatFinal[3][5];

    // entrada de dados
    printf("escreva os valores da matriz original: \n");
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 5; j++) {
            printf("elemento (%d)(%d)", i + 1, j + 1);
            scanf("%f", &MatOriginal[i][j]);
            // *** Colocar aqui.
        }
    }

    // escrita da matriz
    printf(" \n A matriz original informada é = \n");
    for (int i = 0; i < 3; i++) {
        printf(" \n ");
        for (int j = 0; j < 5; j++) {
            printf(" %4.1f ", MatOriginal[i][j]);
        }
    }
    return 0;
}

First of all, there’s a problem in the second block of ties for. There, you had to show the new matrix, not the original matrix.

The place where you have to put the if is where the // *** Colocar aqui. What you have to do is the following: Use the if to compare the value of MatOriginal[i][j] is greater than or equal to 0. If greater than or equal to zero, copy to MatOriginal[i][j] for MatFinal[i][j]. Otherwise, copy to -MatOriginal[i][j] for MatFinal[i][j].

Browser other questions tagged

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