Finished exercises from chapter 22
This commit is contained in:
parent
12971582ab
commit
2b7656a3da
|
@ -1,55 +1,62 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
void read_matrix(double ** matrix, int * x, int * y) {
|
||||
int i, j, j_max = 0, lin, col;
|
||||
char div;
|
||||
lin = col = 1;
|
||||
int main () {
|
||||
int i, j, k, lin[2], col[2], number, length, max_length = 0;
|
||||
float *** matrices;
|
||||
|
||||
/* Inicializa a matriz (vetor de vetores) antes do loop começar e, após este ter iniciado, redimenciona o número de linhas (vetores) conforme estas são acrescentadas */
|
||||
matrix = (double **) malloc (sizeof(double *));
|
||||
for (i = 0; div != '\n' || j != 0; i++){
|
||||
if (i == lin) {
|
||||
lin *= 2;
|
||||
matrix = (double **) realloc (matrix, lin * sizeof(double *));
|
||||
}
|
||||
printf("Este programa lê duas matrizes e devolve o resultado da multiplicação destas.\n");
|
||||
|
||||
/* Inicializa a linha (vetor) antes do loop começar e, após este ter iniciado, redimenciona o número de colunas (índices) conforme estas são acrescentadas*/
|
||||
matrix[i] = (double *) malloc (col * sizeof(double *));
|
||||
for (j = 0; div != '\n'; j++) {
|
||||
if (j == col) {
|
||||
col *= 2;
|
||||
matrix[i] = (double *) realloc (matrix[i], col * sizeof(double));
|
||||
}
|
||||
|
||||
if (scanf("%d%c\n", &matrix[i][j], &div)) {
|
||||
if (j > j_max)
|
||||
j_max = j;
|
||||
if (div == '\n')
|
||||
break;
|
||||
}
|
||||
else {
|
||||
if (j > 0 && j < j_max)
|
||||
for (; j <= j_max; j++)
|
||||
matrix[i][j] = 0;
|
||||
*x = i;
|
||||
*y = j_max + 1;
|
||||
return
|
||||
}
|
||||
for (i = 0; i < 2; i++) {
|
||||
printf("Digite, separadas por espaço, as dimenções em linhas e colunas da %dª matriz: ", i + 1);
|
||||
if (!(scanf("%d %d", &lin[i], &col[i])) || lin[i] == 0 || col[i] == 0) {
|
||||
printf("Valor inválido\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main () {
|
||||
int i, lenght, xa, ya, xb, yb;
|
||||
double ** matrixA, **matrixB;
|
||||
printf("Este programa lê duas matrizes e devolve o resultado da multiplicação destas. Digite, separados por espaços, os valores a serem armazenados na matriz. Pressione ENTER para iniciar uma nova linha, e pressione ENTER duas vezes para encerrar a matriz.\n");
|
||||
for (i = 0; i < 2; i++){
|
||||
printf("Descreva a %dª matriz\n", i + 1);
|
||||
if (i == 0)
|
||||
read_matrix (matrixA, &xa, &ya);
|
||||
else
|
||||
read_matrix (matrixB, &xb, &yb)/
|
||||
if (col[0] != lin[1]) {
|
||||
printf("Matrizes de tamanho incorreto: a largura da 1ª matriz (%d) é diferente da altura da segunda (%d). Estas matrizes não podem ser multiplicadas entre si.)\n", col[0], lin[1]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
matrices = malloc (3 * sizeof(matrices));
|
||||
for (i = 0; i < 2; i++) {
|
||||
matrices[i] = malloc (lin[i] * sizeof(matrices[i]));
|
||||
for (j = 0; j < lin[i]; j++)
|
||||
matrices[i][j] = malloc (col[i] * sizeof(matrices[i][j]));
|
||||
|
||||
printf("Digite, separados por espaço ou quebra de linha, os %d valores a serem depositados na %d matriz:\n", lin[i] * col[i], i + 1);
|
||||
for (j = 0; j < lin[i]; j++)
|
||||
for (k = 0; k < col[i]; k++)
|
||||
scanf(" %f", &matrices[i][j][k]);
|
||||
}
|
||||
|
||||
matrices[2] = malloc (lin[0] * sizeof(matrices[2]));
|
||||
for (i = 0; i < lin[0]; i++) {
|
||||
matrices[2][i] = malloc (col[1] * sizeof(matrices[2][i]));
|
||||
for (j = 0; j < col[1]; j++) {
|
||||
matrices[2][i][j] = 0;
|
||||
for (k = 0; k < col[0]; k++) {
|
||||
matrices[2][i][j] += matrices[0][i][k] * matrices[1][k][j];
|
||||
}
|
||||
|
||||
number = matrices[2][i][j];
|
||||
length = 0;
|
||||
do {
|
||||
number /= 10;
|
||||
length++;
|
||||
} while (number != 0);
|
||||
if (length > max_length)
|
||||
max_length = length;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Resultado:\n");
|
||||
for (i = 0; i < lin[0]; i++) {
|
||||
for (j = 0; j < col[1]; j++)
|
||||
printf("%*.2f ", max_length, matrices[2][i][j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -27,10 +27,10 @@ int main () {
|
|||
}
|
||||
|
||||
max = pascal[i - 1][k / 2];
|
||||
while (max != 0) {
|
||||
width++;
|
||||
do {
|
||||
max /= 10;
|
||||
}
|
||||
width++;
|
||||
} while (max != 0);
|
||||
|
||||
k = 0;
|
||||
printf("\n");
|
||||
|
|
Loading…
Reference in New Issue