Good day! I have to make a program where 2 matrices are added from a file, but I have no idea how to put the codes together. The image shows what I am asked for and in the second program at the time of execution, it does not matter what numbers are in the text file, it always sets zero. Somebody could help me?. The program that I am occupying is lcc-win
The statement is as follows:
The program does the following:
asks the user for the name of the data entry file (numerical data file: the first two integers indicating the number of rows and columns of the matrices, then the floating point numerical data of the two matrices , one per row).
performs the sum of the matrices showing the results on the screen.
asks the user the name of an output file.
saves the three matrices in the output file with matrix structure by rows and columns.
close the file and it ends.
And the programs that I have right now are the following:
/* Suma de matrices */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j, m, n; /* variables para matrices */
printf("Suma de matrices.\n");
printf("Introduce numero de filas: ");
scanf("%2d", &m);
printf("Introduce numero de columnas: ");
scanf("%2d", &n);
float a[m][n];
float b[m][n];
float c[m][n];
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) {
printf("Introduce elemento A[%2d,%2d]: ", i, j);
scanf("%f", &a[i - 1][j - 1]);
}
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) {
printf("Introduce elemento B[%2d,%2d]: ", i, j);
scanf("%f", &b[i - 1][j - 1]);
}
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
c[i - 1][j - 1] = a[i - 1][j - 1] + b[i - 1][j - 1];
printf("'nMatriz A.\n");
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
printf(" %6.2f ", a[i - 1][j - 1]);
printf("\n");
}
printf("\nMatriz B.\n");
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
printf(" %6.2f ", b[i - 1][j - 1]);
printf("\n");
}
printf("\nMatriz C.\n");
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
printf(" %6.2f ", c[i - 1][j - 1]);
printf("\n");
}
return 0;
}
/* Este programa lee dos matrices */
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
int main(void)
{
int m, n, i, j, k = 0;
char s_entrada[60];
clrscr();
printf("Lee dos matrices de punto flotante\n");
printf("a partir de un archivo.\n");
printf("Los dos primeros numeros son los que indican el numero de filas y columnas.\n");
printf("Se leen los datos a los arreglos a[m][n] y b[m][n]\n\n");
printf("Archivo de entrada");
while ((s_entrada[k] = getchar()) != '\n')
k++;
s_entrada[k] = '/* Suma de matrices */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i, j, m, n; /* variables para matrices */
printf("Suma de matrices.\n");
printf("Introduce numero de filas: ");
scanf("%2d", &m);
printf("Introduce numero de columnas: ");
scanf("%2d", &n);
float a[m][n];
float b[m][n];
float c[m][n];
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) {
printf("Introduce elemento A[%2d,%2d]: ", i, j);
scanf("%f", &a[i - 1][j - 1]);
}
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++) {
printf("Introduce elemento B[%2d,%2d]: ", i, j);
scanf("%f", &b[i - 1][j - 1]);
}
for (i = 1; i <= m; i++)
for (j = 1; j <= n; j++)
c[i - 1][j - 1] = a[i - 1][j - 1] + b[i - 1][j - 1];
printf("'nMatriz A.\n");
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
printf(" %6.2f ", a[i - 1][j - 1]);
printf("\n");
}
printf("\nMatriz B.\n");
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
printf(" %6.2f ", b[i - 1][j - 1]);
printf("\n");
}
printf("\nMatriz C.\n");
for (i = 1; i <= m; i++) {
for (j = 1; j <= n; j++)
printf(" %6.2f ", c[i - 1][j - 1]);
printf("\n");
}
return 0;
}
';
k = 0;
FILE* entrada;
entrada = fopen(s_entrada, "r");
if (entrada == NULL)
printf("Error al abrir archivo de entrada.\n");
else {
printf("Exito al abrir: %s \n", s_entrada);
fscanf(entrada, "%d", &m);
fscanf(entrada, "%d", &n);
double a[m][n];
double b[m][n];
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
fscanf(entrada, "%1f", &a[i][j]);
for (i = 0; i < m; i++)
for (j = 0; j < n; j++)
fscanf(entrada, "%1f", &b[i][j]);
fclose(entrada);
printf("\n\nArreglo a[%2d][%2d].\n", m, n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("%10.21f ", a[i][j]);
printf("\n");
}
printf("\n\nArreglo b[%2d][%2d].\n", m, n);
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++)
printf("%10.21f ", b[i][j]);
printf("\n");
}
}
return 0;
}