Sum of matrices from a text file

0

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; }
        
    asked by Samantha 23.05.2018 в 09:13
    source

    1 answer

    1

    Reading of character strings

    To read the name of the file, it is not necessary to complicate life in this way:

    while ((s_entrada[k] = getchar()) != '\n')
            k++;
        s_entrada[k] = '
    scanf("%s",s_entrada);
    
    ';

    Just use scanf :

    double **a = (double**)malloc(m*sizeof(double*));
    for( int i=0; i<m; i++ )
      a[i] = (double*)malloc(n*sizeof(double));
    

    The result will be practically the same. The most obvious disadvantage is that in this case the file can not have spaces ... although there are nicer alternatives to the one you have proposed for it. The advantages:

    • Less code = > fewer error points
    • More readable = > more difficult to screw up

    Do not use VLA

    It is recurring so, for not repeating, I refer to, for example, this other answer where I explain the subject.

    The fact is that the memory of the matrices you would have to reserve it like this:

    for( int i=0; i<m; i++ )
      free(a[i]);
    free(a);
    

    And when using dynamic memory, do not forget to release it:

    double** NuevaMatriz(int filas, int columnas)
    {
      double **a = (double**)malloc(m*sizeof(double*));
      for( int i=0; i<m; i++ )
        a[i] = (double*)malloc(n*sizeof(double));
    }
    
    void LiberaMatriz(double **matriz)
    {
      for( int i=0; i<m; i++ )
        free(a[i]);
      free(a);      
    }
    

    If you find it a cumbersome code (which it is), simply isolate it in a function to make it more reusable:

    float  num1 = 1.5;
    double num2 = 1.5;
    printf("%f %f",num1,num2);
    

    When using decimals, reading is not the same as writing

    When you want to print a decimal number you usually use %f , which works very well both for float as for double :

    float  num1;
    double num2;
    scanf("%f %f",&num1,&num2); // <<--- OJO!!!
    

    And this will work fine since printf will convert num2 to float to print it.

    But the reading does not work the same:

           | 0x00 | 0x01 | ... | 0x08 | 0x09 | 0x0A | 0x0B | ... | 0x1F | 0x20 | ... | 0x3F | 0x40 |
    float  | SIGN | EXPONENTE  | MANTISA                                       | SIN USO ...       |
    double | SIGN | EXPONENTE                              | MANTISA                               |
    

    And the reason is that now you are passing pointers and while the variables by value can be promoted, the pointers do not have this characteristic.

    float occupies 4 bytes, while double , 8. And not only that, but also the size of the mantissa and the exponent also changes. scanf does not know if the pointer belongs to a type float or a type double ... as you tell it to read a float (which is what represents the %f , scanf is limited to store the data read in float format, which results in a strange number for the type double :

    for (i = 0; i < m; i++)
      for (j = 0; j < n; j++)
        fscanf(entrada, "%lf", &a[i][j]);
    

    To read double you have to use %lf :

    while ((s_entrada[k] = getchar()) != '\n')
            k++;
        s_entrada[k] = '
    scanf("%s",s_entrada);
    
    ';

    The difference with your code is that you are using %1f , that is, a one instead of the letter ele .

    Sum of matrices

    Once you have the matrices read, adding them is trivial enough that you copy what you have done (if that code is really yours) in the first program the loop that makes this sum.

    This part I do not do to you because otherwise you will not learn.

    Save the data in the file

    This part has not been started yet, so at the moment it has no errors and, therefore, there is nothing to comment on.

        
    answered by 23.05.2018 / 10:23
    source