Help with basic C exercise

0

I am beginning to study the c language and I need some help with this exercise.

It's almost all done and just need to compile it and see if it works, but I'm not sure how to do the last part.

When option 4 of the menu is chosen, the program must take a tour of the matrix (I think it is a tour) and check if there are values in the first column that are greater than 1, in the second column they are higher to 0.2 and in the third column that are greater than 0.02. If you find these values, a message will appear that indicates that there is at least one anomalous value, otherwise a message will appear indicating that the values you have entered are correct.

This is my code:

#include <conio.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

#define NUMPIPETAS 3
#define MAXMEDICIONES 5

void leerMediciones(int m[NUMPIPETAS][MAXMEDICIONES]);
void imprimirMediciones(int m[NUMPIPETAS][MAXMEDICIONES]);
void calcularPromedio (int m[NUMPIPETAS][MAXMEDICIONES]);
void calcularDesviacionTipica(int m[NUMPIPETAS][MAXMEDICIONES]);
void calcularErrorAleatorio(int m[NUMPIPETAS][MAXMEDICIONES]);
void buscarAnomalos(int m[NUMPIPETAS][MAXMEDICIONES]);

void main(){

    int matriz[NUMPIPETAS][MAXMEDICIONES];
    int m[NUMPIPETAS][MAXMEDICIONES], sumaColumnas[NUMPIPETAS];
    float x[15];
    int opc=0;
    char choice;

    do {
        leerMediciones(m);
        system("cls");

        printf("-----------MENU-----------\n\n");
        printf("1.- Introducir las mediciones\n");
        printf("2.- Imprimir mediciones\n");
        printf("3.- Calcular error aleatorio\n");
        printf("4.- Buscar valores anomalos\n");
        printf("0.- Salir\n\n");
        printf("Elige una opcion: ");

        scanf("%d",&opc);
        printf("----------------------------\n");

        switch(opc)
        {
            case 1: leerMediciones(matriz);
                    break;

            case 2: imprimirMediciones(matriz);
                    break;

            case 3: calcularPromedio (matriz);
                    calcularDesviacionTipica(matriz);
                    calcularErrorAleatorio(matriz);
                    break;

            case 4: buscarAnomalos(matriz);
                    break;

            default:printf("Opcion no valida. Por favor, vuelva a elegir. \n");
        }
      }while(choice !=4);

        imprimirMatriz(matriz);
     }

void leerMediciones(int m[NUMPIPETAS][MAXMEDICIONES]){

    int i,j;

    printf("Introduzca los elementos de la matriz %dx%d\n",NUMPIPETAS,MAXMEDICIONES);//deberia poner aqui i y j en vez de numpipetas y maxmediciones??

      for(i=0;i<NUMPIPETAS;i++){
          for(j=0;j<MAXMEDICIONES;j++){

            printf("Fila %d - Columna %d: ", i,j);
            scanf("%d", &m[i][j]);
         }
      }
}

void imprimirMediciones(int m[NUMPIPETAS][MAXMEDICIONES]){

    int i,j;
    printf("Impresion de la Matriz:\n");

       for(i=0;i<NUMPIPETAS;i++){
           for(j=0;j<MAXMEDICIONES;j++){
               printf("%d ", m[i][j]);
           }
                printf("\n");
       }
}

void calcularPromedio (int m[NUMPIPETAS][MAXMEDICIONES]){

   int i,j;
   float sumaTotal, sumaColumnas[NUMPIPETAS], suma, media;
/*Ahora calculamos la media*/
/*Pero primero hacemos la suma total*/

    sumaTotal=0;

     for(i=0;i<NUMPIPETAS;i++){
        for(j=0;j<MAXMEDICIONES;j++){
            sumaTotal = sumaTotal + m[NUMPIPETAS][MAXMEDICIONES];
                     }
                }

            /*Calculamos la suma de cada columna*/
                for(j=0;j<MAXMEDICIONES;j++){
                    suma = 0;

                    for(i=0;i<NUMPIPETAS;i++){
                        suma = suma + m[NUMPIPETAS][MAXMEDICIONES];
                    }

                  sumaColumnas[j] = suma;

            /*Ahora hacemos la media de cada columna*/
                  media = suma/5;
                }
}

void calcularDesviacionTipica(int m[NUMPIPETAS][MAXMEDICIONES]){

   int i,j;
   float suma1=0, media, varianza, desviacion_tipica;
   int x[i];
/*Calculamos la desviacion tipica*/

    for(i=0;i<NUMPIPETAS;i++){
        for(j=0;j<MAXMEDICIONES;j++){
                {
                 suma1 = suma1 + pow((media - x[i]), 2);
                }
        }
    }

    varianza = suma1 / 4;
    desviacion_tipica = sqrt(varianza);
        printf("Desviacion tipica = %.2f\n", desviacion_tipica);
        getch();
}

void calcularErrorAleatorio(int m[NUMPIPETAS][MAXMEDICIONES]){

   float error_aleatorio, desviacion_tipica;
    error_aleatorio = desviacion_tipica/sqrt(5);
        printf("Error aleatorio = %.2f\n", error_aleatorio);
        getch();
}

void buscarAnomalos(int m[NUMPIPETAS][MAXMEDICIONES]){
    //buscar numeros mayores que 1 en la primera columna
    //mayores que 0.2 en la segunda columna y que 0.02 en la tercera columna
 int i,j;

/*Vamos a hacer un recorrido para calcular los valores anomalos*/
    for(j=0;j<MAXMEDICIONES;j++){
         for(i=0;i<NUMPIPETAS;i++){
            if
         }
    }
}
    
asked by Marta 11.12.2016 в 01:32
source

3 answers

1

Notice that in the definition of the function ...

void buscarAnomalos(int m[NUMPIPETAS][MAXMEDICIONES]);

... is incomplete, you should add:

/*Vamos a hacer un recorrido para calcular los valores anomalos*/
for(j=0;j<MAXMEDICIONES;j++){
      for(i=0;i<NUMPIPETAS;i++){

       /*Esto es nuevo*/
       if(i == 0 && m[i][j] > 1){
        /*Número anómalo*/
        printf("Número mayor a 1 en la primera columna"); 
       }
       if(i == 1 && m[i][j] > 0.2){
        /*Número anómalo*/
        printf("Número mayor a 0.2 en la Segunda columna"); 
       }
       if(i == 2 && m[i][j] > 0.02){
        /*Número anómalo*/
        printf("Número mayor a 0.02 en la tercera columna"); 

        /*FIN DE LO NUEVO*/
       }
     }
}
printf("La matriz está correcta");

Personally I would turn the for , I would go first by column and then by row.

Within if you would have to place your implementation.

    
answered by 12.12.2016 в 13:08
0

For what I read, you already have almost everything done, no? I do not understand what difficulty you see there. The only thing that you should do is within the nested loop of looking for Anomalies () is to see if i is the first, second or third column, and depending on that look if the value is greater than what you said.

//primera columna
if(i==0 && m[j][i]>1){
   //Valor anómalo
}
//segunda columna
if(i==1 && m[j][i]>0.2){
   //Valor anómalo
}
//tercera columna
if(i==2 && m[j][i]>0.02){
   //Valor anómalo
}

And those would be the three cases I think. within the comments, you should add your implementation to deal with what happens in case of finding the anomalous value.

    
answered by 12.12.2016 в 09:26
0

look at the dimensions when declaring, turn the indexes and you could do this:

for (i = 0; i

   if( m[i][j] > 1){
    printf("Número mayor a 1 en la primera columna"); 
   }
   else
     if(m[i][j] > 0.2){
    printf("Número mayor a 0.2 en la Segunda columna"); 
   }
   else 
    printf("Número mayor a 0.02 en la tercera columna"); 
   }

correct errors and improve the syntax

    
answered by 12.12.2016 в 13:38