sortowanie struktury studentów

0

Witam,

Jest w stanie ktoś mi powiedzieć co robię źle, bo szczerze nie mam pojęcia co tu jest nie tak.
Muszę wprowadzić oceny dla studentów, policzyć średnie i posortować quicksortem.

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct student{
	int nrindeksu;
	double oceny[5];
	double srednia;
};
void studenty(int,struct student[]);
void quicksort(int,int,struct student[]);
int main(int argc, char *argv[]) {
	int n,i;
	printf("Podaj ilosc studentow: ");
	scanf("%d",&n);	
	struct student student[n];
	studenty(n,student);
	printf("--------------QUICKSORT-----------------");
	quicksort(1,n,student);

	return 0;
}
void studenty(int n,struct student student[]){
	int i =1;
	double sr;
		while(i<=n){
		sr = 0;
		int j=1;
		printf("Podaj nr indeksu %d studenta: ",i);
		scanf("%d",&student[i].nrindeksu);
		while(j<=5){
			printf("Podaj %d ocene z egz: ",j);
			scanf("%lf",&student[i].oceny[j]);
			sr = sr + student[i].oceny[j];
			j++;
		}
		student[i].srednia = sr/5;
		printf("Srednia %d studenta to: %lf\n",i,student[i].srednia);
		i++;
	}
}
void quicksort(int l, int p, struct student student[]){
	int i,j;
	double piwot,pomoc;
	
	i = (l + p) / 2;
	piwot = student[i].srednia;
	student[i].srednia = student[p].srednia;
	for(j = i = l; i < p; i++){
		if(student[i].srednia < piwot){
			pomoc=student[i].srednia;
			student[i].srednia=student[j].srednia;
			student[j].srednia=pomoc;
			j++;
			}
	}
  	student[p].srednia = student[j].srednia;
  	student[j].srednia = piwot;
  	if(l < j - 1)  quicksort(l, j - 1,student);
  	if(j + 1 < p) quicksort(j + 1, p,student);
}
0

A czy ta Twoja funkcja quicksort ma szanse kiedyś się skończyć?

1

Chyba było dobrze ale poziom nieczytelności przekroczył granice dobrego smaku.


void students( struct student *const student, const size_t n ) {

  for( int i = 0; i < n; i++ )  {
        
    printf( "Podaj nr indeksu %d studenta: ", i );
    scanf( "%d",&student[i].nrindeksu );

    double av = 0.0;
    for( int j = 0; j < 5; j++ )  {

      printf("Podaj %d ocene z egz: ", j );
      scanf( "%lf", &( student[i].oceny[j] ) );
      av = av + student[i].oceny[j];
      
    }
    
    student[i].srednia = av / 5;
    printf("Srednia %d studenta to: %lf\n",i,student[i].srednia);
    
  }
  
}

Zapoznaj się z qsort. ( man qsort )

1 użytkowników online, w tym zalogowanych: 0, gości: 1