Problem with operations of elements in an array

-1

I have a problem trying to show the result of the division of the elements of each row of a two-dimensional array.

What you plan to do is: Divide the first element of the row of a two-dimensional array between the second element of the row, and the result between the third element of the row, so that, at the end of the row, display the result of that division, the problem is that when I run the program, showing me the result of each division in each row, it shows me the value of zero, could someone explain why? I already tried to change the type of data and the same thing keeps happening.

I hope you can help me.

#include <stdio.h>
#define SIZE 10

void sumarFilas(double a[][SIZE], int filas_sum);
void multiplicarFilas(double b[][SIZE], int filas_mul);
void dividirFilas(double c[][SIZE], int filas_div);

int main()
{
    int num_fil,i,j;

    printf("Ingresa el numero de filas: ");
    scanf("%d",&num_fil);
    printf("\n");

    double bid_array[num_fil][SIZE];

    for(i=0; i<num_fil; i++)
    {
        for(j=0; j<SIZE; j++)
        {
            printf("Ingresa el elemento que se encuentra en la posicion [%d][%d] del arreglo: ",i,j);
            scanf("%lf",&bid_array[i][j]);
        }
    }

    printf("\nLos elementos ingresados al arreglo son:");
    printf("\n");

    for(i=0; i<num_fil; i++)
    {
        for(j=0; j<SIZE; j++)
        {
          printf("     %.3lf",bid_array[i][j]); 
        }

        printf("\n");
    }

    sumarFilas(bid_array, num_fil);

    printf("\n");

    multiplicarFilas(bid_array, num_fil);

    printf("\n");

    dividirFilas(bid_array, num_fil);

    printf("\n");

    system("pause");
    return 0;
}

void sumarFilas(double a[][SIZE], int filas_sum)
{
    int i,j;

    double suma[filas_sum];

    for(i=0; i<filas_sum; i++)
    {
        for(j=0; j<SIZE; j++)
        {
            suma[i] += a[i][j];
        }
    }

    printf("\nLa suma de los elementos de cada fila del arreglo bidimensional es:");
    printf("\n");

    for(i=0; i<filas_sum; i++)
    {
        printf("%lf\n",suma[i]);
    }
}

void multiplicarFilas(double b[][SIZE], int filas_mul)
{
    int i,j;

    double multiplicar[filas_mul];

    // Inicializamos todos los elementos del arreglo a 1.

    for(i=0; i<filas_mul; i++)
    {
        multiplicar[i] = 1;
    }

    for(i=0; i<filas_mul; i++)
    {
        for(j=0; j<SIZE; j++)
        {
            multiplicar[i] *= (b[i][j]);
        }
    }

    printf("\nLa multiplicacion de los elementos de cada fila del arreglo bidimensional es:");
    printf("\n");

    for(i=0; i<filas_mul; i++)
    {
        printf("%lf\n",multiplicar[i]);
    }
}

void dividirFilas(double c[][SIZE], int filas_div)
{
    int i,j;

    long double dividir[filas_div];

    // Inicializmos todos los elementos del arreglo a 1.
    for(i=0; i<filas_div; i++)
    {
        dividir[i] = 1;
    }

    for(i=0; i<filas_div; i++)
    {
        for(j=0; j<SIZE; j++)
        {
            dividir[i] = dividir[i] / c[i][j]; 
        }
    }

    printf("\nLa division de los elementos de cada fila del arreglo bidimensional es:");
    printf("\n");

    for(i=0; i<filas_div; i++)
    {
        printf("%Lf\n",dividir[i]);
    }
}
    
asked by Jesús Fragoso 21.12.2016 в 21:20
source

1 answer

2

The program without any modification works well, but you have a problem dividing each number of the rows of the array the result is not what you expect how you commented on your question you are incorrectly dividing each number.

Incorrect way :

for (i = 0; i < filas_div; i++) {
    dividir[i] = 1;
}

for (i = 0; i < filas_div; i++) {
    for (j = 0; j < SIZE; j++) {
        dividir[i] = dividir[i] / c[i][j]; 
    }
}

You are using the first division as a numerator on the 1, obviously, and not the first number in the row of the arrangement, how do you want it, in an arrangement like this:

int arr[2][4] = {{2, 2, 2, 2}, {2, 2, 2, 2}};

The value of the division would be: 0.0625

The expected value is: 0.25

Correct way :

for (i = 0; i < filas_div; i++) {
  dividir[i] = c[i][0];
}

for (i = 0; i < filas_div; i++) {
  for (j = 1; j < SIZE; j++) {
    dividir[i] /= c[i][j];
  }
}
    
answered by 21.12.2016 в 22:20