Created
August 30, 2017 19:49
-
-
Save rbenvenuto/f7a70c8471ea1b0296851d2d4a0ef99f to your computer and use it in GitHub Desktop.
Exercício com matrizes em C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#define LINHAS_A 4 | |
#define COLUNAS_A 5 | |
#define ELEMENTOS_B 4 | |
#define ELEMENTOS_C 5 | |
// Efetuar a leitura de 20 valores | |
// inteiros em uma Matriz A com 4 linhas | |
// e 5 colunas | |
// Construir um Vetor B para 4 elementos | |
// que seja formado pelo somatório | |
// dos elementos de cada linha da Matriz A | |
// Construir também um Vetor C para 5 | |
// elementos, cada elemento é o resultado | |
// da somatória de cada coluna da Matriz A | |
int matA[LINHAS_A][COLUNAS_A]; | |
int vetB[ELEMENTOS_B]; | |
int vetC[ELEMENTOS_C]; | |
int i, j; | |
int main() | |
{ | |
for(i=0; i<LINHAS_A; i++) | |
{ | |
for(j=0; j<COLUNAS_A; j++) | |
{ | |
printf("Digite [%d]->[%d]\n", i, j); | |
scanf("%d", &matA[i][j]); | |
} | |
} | |
// Gerando valores do vetB | |
printf("\n*** Gerando valores do vetB ***\n(somatório das linhas de matA)\n"); | |
for(i=0; i<ELEMENTOS_B; i++) | |
{ | |
for(j=0; j<COLUNAS_A; j++) | |
{ | |
vetB[i] += matA[i][j]; | |
} | |
printf("Valor do vetB[%d]: %d\n", i, vetB[i]); | |
} | |
//Gerando valores do vetC | |
printf("\n*** Gerando valores do vetC ***\n(somatório das colunas de matA)\n"); | |
for(i=0; i<ELEMENTOS_C; i++) | |
{ | |
for(j=0; j<LINHAS_A; j++) | |
{ | |
vetC[i] += matA[j][i]; | |
} | |
printf("Valor do vetC[%d]: %d\n", i, vetC[i]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment