0
I wanted to sort the grades of the students, only I’m not able to do because at the time of receiving the grades I’m using the index j, I think that’s the problem only I don’t know how to solve.
#include<stdio.h>
#include<stdlib.h>
double ordena(double *a, double *b);
struct notas
{
double notas[3];
char nome[100];
};
int main(void)
{
int i, j;
struct notas aluno[2];
for(i = 0; i < 2; i++)
{
printf("Informe os nomes dos alunos :");
scanf("%s", aluno[i].nome);
system("cls");
for(j = 0; j < 3; j++)
{
printf("Informe as notas do aluno %d:", j + 1);
scanf("%lf", &aluno[i].notas[j]);
system("cls");
}
system("cls");
}
for(i = 0; i < 2; i++)
{
for(j = i + 1; j < 3; j++)
{
ordena(&aluno[i].notas[j], &aluno[j].notas[j]);
}
}
for(i = 0; i < 2; i++)
{
printf("Nome dos alunos %s:", aluno[i].nome);
for(i = 0; i < 3; i++)
{
printf("%.2lf\n", aluno[i].notas[j]);
}
}
system("pause");
return 0;
}
double ordena(double *a, double *b)
{
double o;
if(*a > *b)
{
o = *a;
*a = *b;
*b = o;
}
return 0;
}
The function that has called
ordena
actually does not order and simply exchange values of variables, what is usually calledswap
. Start by reading about bubblesort for example, which is one of the simplest sorting algorithms.– Isac