I can’t get how many students approved to show up

Asked

Viewed 156 times

0

Algorithm to read 2 notes of a student, calculate and print the final average. Right after you write the message "Calculate another student’s average 1.Yes 2.No?" and request a reply. If the answer is 1, the algorithm must be run again, otherwise it must be terminated by printing the number of students approved. To be approved the student has to have an average greater than or equal to seven

Follows 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;
}
  • Have you tried adding a counter for each student insertion? example add the quantities of the variables aluno1 and aluno2, of course the best would be to unify these variables. Try working with case instead of .... while, put a case 1 (EXECUTES CODE) break case 2 Exit break. I believe it will work

2 answers

1

Hello, all right?!

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

  1. You need to be more careful about 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.

  2. Secondly, you do not need to declare so many variables to solve this problem. The memory management used is something quite important and should have a special attention with it.

  3. Also, remember the.. while repetition structure that will help you enough not to commit as many repetitions.

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

Now I’ll show you my algorithm resolution proposal. It’s quite simple but remember: 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’ve been helpful!

note: each has its own style of code and indentation, so write the code as it is most comfortable for you.

1

Hello. I redid 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);
 }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.