Treść zadania:
Zdefiniuj strukturę opisującą studenta ( imię, nazwisko, rok, kierunek), a następnie utwórz tablicę, której elementami są zdefiniowane wcześniej struktury. Napisz funkcje, która: wypisze wszystkich studentów danego roku.
kod:
#include <stdio.h>
#include <stdlib.h>
struct student
{
char imie[10];
char nazwisko[30];
int rok;
char kierunek[20];
};
void wypelnij(struct student *s)
{
printf("Podaj imie: ");
scanf("%s", &s->imie);
printf("Podaj nazwisko: ");
scanf("%s", &s->nazwisko);
printf("Podaj rok: ");
scanf("%d", &s->rok);
printf("Podaj kierunek: ");
scanf("%s", &s->kierunek);
}
char studenci_z_roku(struct student *s,int n, int rok)
{
int i;
for(i=0; i<n; i++)
{
if(((s+i)->rok)==rok)
{
printf("\n%s %s\n", (s+i)->imie, (s+i)->nazwisko);
}
}
}
int main()
{
int n,i,student_rok;
printf("Ile studentow: ");
scanf("%d", &n);
struct student tab[n];
for(i=0; i<n;i++)
{
printf("\nPodaj dane srudenta %d\n", i+1);
wypelnij(tab);
}
printf("Wypisz studentow z roku: ");
scanf("%d", &student_rok);
studenci_z_roku(tab,n, student_rok);
return 0;
}
Program nie wypisuje wszystkich studentów z podanego roku tylko ostatniego.