Can you help me find the error? repetitive cycle for C ++

1

I have to print the average of the approved grades and the average of the approved grades but I get it wrong.

Does anyone have any suggestions?

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main () {

    int proma=0,promd=0,n,sumap=0,suman=0,contador=0,contadorp=0,contadorn=0;
    printf("Ingrese las 10 notas:\n");

    for(contador=1;contador<=10;contador++){

        scanf("%d",&n);
        if(n>=6) {
          contadorp++;
          sumap=sumap+n;
          proma=sumap/contadorp;
        }
        else if(n<6)
           contadorn++;
           suman=suman+n;
          promd=suman/contadorn;
    }

    printf("\nEl promedio de las notas aprobadas es de: %d ",proma);
    printf("\nEl promedio de las notas desaprobadas es de: %d",promd);
}
    
asked by Gustavo Torres 30.06.2017 в 00:00
source

3 answers

3

specify what error it is?

Fixing me by the code the error would be that you are missing two {}

else if(n<6){

contadorn++;

suman=suman+n;

promd=suman/contadorn;}
    
answered by 30.06.2017 в 00:05
3

The error is syntax at a glance. Since a structure carries more than one line of internal code, the { ...} must be added, which is what it lacks in else if

I will propose a solution with some modifications:

  • The average variable type should not be an integer, but a float so that it takes decimals into account
  • Another logical error of the additional comparison in the second if , where it could have been a else thus avoiding the additional comparison since the only other option of if is that be < 6
  • If you have cumulative variables such as the sum and count variables of the notes, it is not necessary to perform the average operation in each iteration. if not rather at the end of the cycle.
  • It would be logical to use a variable for the number of notes entered.an additional variable nottotal ;
  • The printing of the variable float is done with %.2f that represents two decimals.

The Code would be:

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


int main () {
    int n,sumap=0,suman=0,contadorp=0,contadorn=0, nottotal;
    float proma=0,promd=0;
    printf("Ingrese cantidad de notas:\n");
    scanf("%d",&nottotal);
    printf("Ingrese Notas :\n");
    for(int contador=0;contador<nottotal;contador++){
        scanf("%d",&n);
        if(n>=6) {
           contadorp++;
           sumap=sumap+n;
        }
        else{
          contadorn++;
          suman=suman+n;
      }
    }
  proma = (float)sumap/contadorp;
  promd = (float)suman/contadorn;
  printf("\nEl promedio de las notas aprobadas es de: %.2f ",proma);
  printf("\nEl promedio de las notas desaprobadas es de: %.2f",promd);
}
    
answered by 30.06.2017 в 00:17
1

For it to work, you simply have to take these two lines:

proma = sumap/contadorp;
promd = suman/contadorn;

take them out of the for loop and put them just before the final printf . This is because the averages should be calculated once you have the total number of notes and the number of notes, both approved and suspended.

Also, if you allow the average to be a decimal number, the average variables must be a float and do the division so that if the sum and the counter are integers, convert them to float (casting or * 1.0 ).

    
answered by 30.06.2017 в 00:17