Can anyone help me choose a specific line from the matrix ? (c/c++ language)

Asked

Viewed 86 times

0

I’m doing an activity and she says:

(function - matrix and vector) Design a program that reads a 4x4 matrix and creates a 4-element vector consisting of the elements of a matrix line chosen by the user my problem is that I don’t know how to make it print exactly the line chosen by the user.

My show just turned out to be this

#include <stdio.h>
int main() {
 
float matriz[4][4];
int d;
/* Entrada de dados na matriz */
printf(" Informe o valor de cada elemento -----\n");
for(int i=0; i<2; i++){
 for(int j=0; j<2; j++)
 {
 printf(" \n matA(%d,%d) = ", i+1,j+1);
 scanf("%f", &matriz[i][j]);
 }
}
// escolha da linha
printf("escolha a linha desejada:");
scanf("%d",&d);

/* Escrita da matriz - saída formatada */
printf(" \n A matriz informada eh = \n");
for(int i=0; i<2; i++)
{
 printf(" \n ");
 for(int j=0; j<2; j++)
 printf(" %4.1f ", matriz[i][j]);
}
}
  • Have you considered not using a loop for the line (iindex i) and instead inform d? for(int j=0; j<2; j++) printf(" %4.1f ", matriz[d][j]);

1 answer

0


The solution is very simple, you take the user’s line and replace it in the line that goes through, so Oce will only go through the line that the user chooses. Here he is doing with a 2x2 matrix that was his example code:

int main() {
 
float matriz[4][4];
float vetor[4];
int d;
/* Entrada de dados na matriz */
printf(" Informe o valor de cada elemento -----\n");
for(int i=0; i<2; i++){
 for(int j=0; j<2; j++)
 {
 printf(" \n matA(%d,%d) = ", i+1,j+1);
 scanf("%f", &matriz[i][j]);
 }
}
// escolha da linha
printf("escolha a linha desejada:");
scanf("%d",&d);


 
 printf(" \n ");
 for(int j=0; j<2; j++){
 printf(" %4.1f ", matriz[d][j]);
  vetor[j]=matriz[d][j];
 }
 //imprime vetor com os numeros da linha
 printf(" \n ");
 for(int j=0; j<2; j++){
 printf(" %4.1f",  vetor[j]);}

}````

Browser other questions tagged

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