I can not make it appear how many students have been approved

0

Algorithm to read 2 notes of a student, calculate and print the final average. Right after writing the message "Calculate the average of another student 1.Yes 2.No?" and request a response. If the answer is 1, the algorithm must be run again, otherwise it should be terminated by printing the number of approved students. To be approved the student must have an average of seven or more

Follow my code:

#include<stdio.h>
int main(void)
{
  float nota1,nota2,media1,media2,nota3,nota4,mediaf=1;
  int resp;
  char aluno1[10],aluno2[10];
    while(resp==1)
    {
        printf("Informe o nome do aluno:");
        gets(aluno1);
            fflush(stdin);
        printf("\nDigite a primeira nota do aluno: ");
        scanf("%f",&nota1);
        printf("\nDigite a segunda nota: ");
        scanf("%f",&nota2);
            fflush(stdin);
        media1 = (nota1 + nota2)/2;
            fflush(stdin);
        printf("\na media de %ce : %0.0f\n",aluno1, media1);
        printf("\nDigite 1 para continuar ou 2 para sair: ");
        scanf("%d", &resp);

    }
  do
    {
            fflush(stdin);
        printf("Informe o nome do aluno:");
        gets(aluno2);
            fflush(stdin);
        printf("\nDigite a primeira nota: ");
        scanf("%f",&nota3);
        printf("\nDigite a segunda nota: ");
        scanf("%f",&nota4);
             fflush(stdin);
        media2 = (nota3 + nota4)/2;
        printf("\nMedia de %s e: %0.0f\n",aluno2,media2);
            fflush(stdin);
        printf("\nDigite 1 para continuar ou 2 para sair\n");
        scanf("%d", &resp);
    }   
  while (resp==1);

  return 0;
}


#include<stdio.h>

int main(void)

{
  float nota1,nota2,media1,media2,nota3,nota4,mediaf=1;
  int resp;

char aluno1[10],aluno2[10];

    while(resp==1)
    {

        printf("Informe o nome do aluno:");

        gets(aluno1);

            fflush(stdin);

        printf("\nDigite a primeira nota do aluno: ");

        scanf("%f",&nota1);

        printf("\nDigite a segunda nota: ");

        scanf("%f",&nota2);

            fflush(stdin);

        media1 = (nota1 + nota2)/2;

            fflush(stdin);

        printf("\na media de %ce : %0.0f\n",aluno1, media1);

        printf("\nDigite 1 para continuar ou 2 para sair: ");

        scanf("%d", &resp);

    }
  do
    {

            fflush(stdin);

        printf("Informe o nome do aluno:");

        gets(aluno2);

            fflush(stdin);

        printf("\nDigite a primeira nota: ");

        scanf("%f",&nota3);

        printf("\nDigite a segunda nota: ");

        scanf("%f",&nota4);

             fflush(stdin);

        media2 = (nota3 + nota4)/2;

        printf("\nMedia de %s e: %0.0f\n",aluno2,media2);

            fflush(stdin);

        printf("\nDigite 1 para continuar ou 2 para sair\n");

        scanf("%d", &resp);

    }   

  while (resp==1);

  return 0;
}
    
asked by anonymous 08.11.2018 / 00:31

2 answers

1

Hello, how are you?

I'll suggest some changes to your code to make it look better:

  • You need to be more careful with how you write your code and try to keep it as clean and simple as possible by avoiding repetitions of lines of code, for example.

  • Second, you do not need to declare as many variables to solve this problem. The memory management used is something quite important and should pay special attention to it.

  • Also, remember the do..while repeat structure that will help you a lot not to commit as many repetitions.

  • Finally, pay attention to how char variables that store more characters are handled. To display them correctly you must use the reading format '% s' instead of '% c'.

  • Now I'll show you my proposed algorithm resolution. It's fairly simple but keep in mind: there may be even more effective resolutions.

    #include<stdio.h>
    int main(void)
    {
      float nota1,nota2,media;
      int resp, aprovados;
      char aluno[10];
      aprovados = 0;
      do {
          printf("\nInforme o nome do aluno: ");
          scanf("%s", aluno);
      printf("\nDigite a primeira nota do aluno: ");
      scanf(" %f",&nota1);
      printf("\nDigite a segunda nota: ");
      scanf(" %f",&nota2);
      media = (nota1 + nota2)/2;
      printf("\nA media de %s e : %0.0f\n",aluno, media);
      if(media>=7){
        aprovados++;
      }
      printf("\nDigite 1 para continuar ou 2 para sair: ");
      scanf(" %d",&resp);
       }while( resp == 1 );
      printf("\nForam aprovados %d alunos.\n",aprovados);
     return 0;
    }
    

    I hope I have been useful!

    Note: Everyone has their code and indentation style, so please write the code as you like.

        
    08.11.2018 / 01:58
    1

    Hello. I've refined your code in a simpler way. If you have questions, just ask.

    insira o código aqui
    
    
    #include <stdio.h>
    
    
    int mediaParcial(){
    float notaUm,notaDois,media;
    int x,cont=0,escolha;
    
    do{
    printf("Calcular a média de um aluno\n");
    printf("1.Sim 2.Não?\n");
    scanf("%d",&escolha);
    if(escolha==1){
    fflush(stdin);
    printf("Nota 1:");
    scanf("%f",&notaUm);
    printf("Nota 2:");
    scanf("%f",&notaDois);
    media=(notaUm+notaDois)/2;
    if(media >= 7){
    cont+=1;
    }
    }else{
    break;
    }
    }while(escolha !=2);
    
     return cont;
    
     }
    
     int main(void) {
     int aprovados;
     aprovados=mediaParcial();
     printf("Foram Aprovados %d",aprovados);
     }
    
        
    08.11.2018 / 02:12