Tablice struktur, problem z wynikiem po skompilowaniu

0

Mam taki problem z zadaniem, że w tym programie funkcja nie znajduje maksymalnej średniej dla pierwszego obliczonego studenta(jeśli ma najwyższą średnią). Jakiś głupi błąd, którego nie mogę znaleźć.


#include <stdio.h>
#include <stdlib.h>
#define ROZMIAR 4

struct student
{char nazwisko[10];
int rok;
char kierunek[20];
int oceny[6];
float srednia;
};

void dane(struct student *s, struct student tab[]);
void wypisz(struct student *s, struct student tab[]);
void drugi_rok(struct student s, struct student tab[]);
void max_srednia(struct student s, struct student tab[]);


int main()
{
struct student s1,tabst[4];

dane(&s1, tabst);
printf("\n");
wypisz(&s1, tabst);
printf("\n");
drugi_rok(s1, tabst);
printf("\n");
max_srednia(s1, tabst);
printf("\n");
system("pause"); 

}

void dane(struct student *s, struct student tab[])
{
	int i,j;
	for(i=0;i<ROZMIAR;i++)
	{
		printf("Podaj nazwisko: ");
scanf("%s",&tab[i].nazwisko);
printf("Podaj rok studiow: ");
scanf("%d",&tab[i].rok);
printf("Podaj kierunek studiow: ");
scanf("%s",&tab[i].kierunek);
printf("Podaj oceny: ");
for(j=0;j<6;j++)
scanf("%d",&tab[i].oceny[j]);
	}
	
	
}

void wypisz(struct student *s, struct student tab[])
{
	int i, j;
	
	for(i=0; i<ROZMIAR;i++)
	{
		printf("Student %d ", i+1);
	    printf("Nazwisko: %s, Rok: %d, Kierunek: %s ", tab[i].nazwisko, tab[i].rok, tab[i].kierunek); 
		printf("Oceny: ");
	
	float suma=0;
	for(j=0;j<6;j++)
	 {
	printf("%d,", tab[i].oceny[j]);
	suma=suma+tab[i].oceny[j];
	 }
	tab[i].srednia=suma/6;
	printf(" Srednia: %.2f", tab[i].srednia);
	printf("\n");
      
   }
}
void drugi_rok(struct student s, struct student tab[])
{
	int i;
	printf("Studenci 2 roku:\n");
	for(i=0; i<ROZMIAR;i++)
	{
		if(tab[i].rok==2)
	    printf("Nazwisko: %s, Kierunek: %s \n", tab[i].nazwisko, tab[i].kierunek);
    }
	
}
void max_srednia(struct student s, struct student tab[])
{
	int i, indeks;
	float max=0;
	for(i=0; i<ROZMIAR;i++)
	{
	if(max<=tab[i].srednia)
	indeks=i;
	max=tab[i].srednia;
    }
    printf("Najwyzsza srednia: %f\n", tab[indeks].srednia);
    printf("Nazwisko: %s, Rok: %d, Kierunek: %s ", tab[indeks].nazwisko, tab[indeks].rok, tab[indeks].kierunek);
}

0

Jakim cudem ci to działa? na pewno masz błąd w tym fragmencie

        if(max<=tab[i].srednia)
        indeks=i;
        max=tab[i].srednia;

jeśli If jest prawdziwy to wykona się indeks=i;, zaś max=tab[i].srednia; za każdym razem (niezależnie od tego czy tab[i].średnia jest większa od max czy też nie)

Powinno być

        if(max<=tab[i].srednia)
       {
             indeks=i;
             max=tab[i].srednia;
        }
 

Aczkolwiek nie sprawdzałem czy zadziała jak trzeba.

0

lol, zapomniałem o klamrach... No cóż, dzięki

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