calculate quotient in c language

0

The problem is based on a program that asks to calculate the sum of the elements above the secondary diagonal, calculate the sum of the secondary diagonal and divide the first by the second (matrices), that is: sum elements above of secondary diagonal / secondary diagonal sum. the quotient must be mandatorily sent from the main and the calculation of the quotient and the rest within a function. Here is the code:

int main( void ) {
  int     mat2A[TAM_MAT][TAM_MAT];
  float   cociente;

  puts("Probando parte 2-a.-");
  cargarMatParteA(mat2A, TAM_MAT, TAM_MAT, !CON_CERO, NEGA1);
  mostrarMatParteA(mat2A, TAM_MAT, TAM_MAT);

  if(calcularcociente_mia(mat2A, TAM_MAT, TAM_MAT, &cociente))
    printf("El cociente es %f.\n\n", cociente);
}   

int calcularcociente_mia(int mat[][TAM_MAT], int cantFi, int cantCo, float *cociente) {
  int suma=0;
  int  sumadiagonal=0;

  for (cantFi=TAM_MAT-1;cantFi>=0;cantFi--) {
    for(cantCo=0;cantCo<TAM_MAT-cantFi-1;cantCo++) {
      suma+= *(mat+cantFi*TAM_MAT+cantCo);
    }
  }

  for(cantFi=0,cantCo=TAM_MAT-1;cantFi<TAM_MAT;cantFi++,cantCo--) {
    sumadiagonal+= *(mat+cantFi*TAM_MAT+cantCo);
  }

  *cociente =  (float) suma/(float)sumadiagonal;

  return cociente;
}
    
asked by Luciano Pulido 26.03.2017 в 18:45
source

1 answer

0

I see that the function calcularcociente_mia is incorrectly defined since it is like int when it should return a float .

Furthermore, the way to access the elements of the matrix within the function was bad. Since arrays are vector vectors, doing *(mat+1) leads to the second row vector and making **(mat+1) would lead to the first element of the second row. Then a corrected version modified for tests.

#define TAM_MAT 2 //Para probar
float calcularcociente_mia(int mat[][TAM_MAT], int cantFi, int cantCo, float *cociente) {
  int suma=0;
  int sumadiagonal=0;

  for (cantFi=TAM_MAT-1;cantFi>=0;cantFi--) {
    for(cantCo=0;cantCo<TAM_MAT-cantFi-1;cantCo++) {
      suma+= mat[cantFi][cantCo];
    }
  }

  for(cantFi=0,cantCo=TAM_MAT-1;cantFi<TAM_MAT;cantFi++,cantCo--) {
    sumadiagonal+= mat[cantFi][cantCo];
  }

  *cociente =  (float) suma/(float)sumadiagonal;

  return *cociente;
}

int main ()
  {
  int     mat2A[TAM_MAT][TAM_MAT] = {{2,1},{3,2}}; //Inicializo para probar
  float   cociente;

  puts("Probando parte 2-a.-");
  //cargarMatParteA(mat2A, TAM_MAT, TAM_MAT, !CON_CERO, NEGA1);
  //mostrarMatParteA(mat2A, TAM_MAT, TAM_MAT);


  if(calcularcociente_mia(mat2A, TAM_MAT, TAM_MAT, &cociente))
    printf("El cociente es %f.\n\n", cociente);

  return 0;
}
    
answered by 19.05.2017 в 17:59