Search for item appearances in fix

2

The program must ask for the notes of the number of students entered by the user, which can be only between 0 and 10. It must be said how many 0, 5 and 10 have appeared. The entered notes that are not 0, 5 or 10 must also be said.

The program compiles but the repeated numbers appear more than once, when they would have to appear only once.

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

//Programa EJ003
int main(){
    int notas[10];
    int index, alumno, veces0, veces5, veces10, n, t;

    printf("Cuántos alumnos hay en clase?: ");
    scanf("%d",&n);
    veces0 = 0; 
    veces5 = 0; 
    veces10 = 0;
    /*Se inicializan a cero los contadores que guardarán las veces que 
se obtuvieron puntuaciones de 0, 5, y 10 */

   for (index = 0; index <= n;index++){ 
    //N es el número de alumnos.
    //Repetir N veces el siguiente proceso:

      printf("Nota de alumno(%i): ",index);
      scanf("%d",&alumno);   
      /*Leer la nota del alumno. Si la nota no esta en el conjunto, 
      entonces se añade:*/
       if (!(alumno == notas[index])) notas[index] = notas[index] + alumno;
       switch (alumno){
       /*si la nota es cero, cinco o diez, se incrementa en uno el contador
       correspondiente: veces0, veces5 o veces10*/
         case 0: veces0 = veces0 + 1;
           break; 
         case 5: veces5 = veces5 + 1; 
           break;
         case 10: veces10 = veces10 + 1;
           break;
       }
     }
     printf("Número de alumnos con un cero: %d\n",veces0); //Se muestran los 
     printf("Número de alumnos con un cinco: %d\n",veces5);      //resultados 
     printf("Número de alumnos con un diez: %d\n",veces10);   
     printf("Ningún alumno ha obtenido ninguna de las siguientes puntuaciones:");
     /*Se muestran las notas que no están en el conjunto, que no estar n, porque
ningún alumno habrá obtenido esa calificación.*/

       for (index = 0; index <= 10;index++){
         for (t = 0; t < 10;t++){
           if (!(index == notas[t])) printf("%d ",index);
         }
       }
       getch();
       return 0;
}
    
asked by Alejandro Caro 01.10.2017 в 12:56
source

1 answer

2

We go in parts:

int notas[10];

That's wrong; the possible values are { 0, 1, 2, ... 10 } . That is, 11 possible values. And we take advantage of it to initialize it, and that it does not have rare values: int notas[11] = { 0 };

for (index = 0; index <= n;index++){ 

That counts 1 more student. If you enter 3 alumnos , your for loop asks for 4 note entries. We change it to for (index = 0; index < n;index++){ .

 if (!alumno) notas[0] = notas[0] + alumno;

I do not know what you want there. The correct thing to do is to remove the if and leave it as ++notas[alumno];

  for (index = 0; index <= 10;index++){
     for (t = 0; t < 10;t++){
       if (!(index == notas[t])) printf("%d ",index);
     }
   }

Another thing that does not end with cuadrar . The easiest thing to do is to

for( index = 0; index < 11; index++ ) {
  if( !notas[index] )
    printf( "No hay ningun %d\n", index );
  }

With those changes, everything will work correctly.

Edit

That I am distracted .

Applying the changes I indicated, you can delete a few things:

  • Variables veces0, veces5, veces10 , which are already useless.
  • Your%% complete% loop. Neither is necessary.

And some minor changes in your switch( ) :

printf("Número de alumnos con un cero: %d\n",notas[0]); //Se muestran los 
printf("Número de alumnos con un cinco: %d\n",notas[5]);      //resultados 
printf("Número de alumnos con un diez: %d\n",notas[10]);

Now yes: -)

    
answered by 01.10.2017 в 13:09