Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- struct student
- {
- string name;
- string surname;
- int grade[6];
- float avg;
- struct student *next;
- struct student *prev;
- };
- typedef struct student StNode;
- typedef struct student *Stpt;
- Stpt findmax(Stpt first);
- void addNode(Stpt *first,Stpt *last,StNode s);
- int main()
- {
- Stpt first = NULL , last=NULL, stMax;
- StNode s;
- int n,sum,cnt,gr;
- cin>>n;
- for(int i=0; i<n; i++)
- {
- cin>>s.name>>s.surname;
- sum=0,cnt=0;
- while(1)
- {
- cin>>gr;
- if(gr==-1) break;
- else s.grade[cnt]=gr;
- sum+=gr;
- cnt++;
- }
- if(cnt<4)
- cnt=4;
- s.avg=(float)sum/cnt;
- addNode(&first,&last,s);
- }
- stMax=findmax(first);
- cout<<stMax->name<<" "<<stMax->surname<<" "<<stMax->avg;
- return 0;
- }
- Stpt findmax(Stpt first)
- {
- Stpt Max= first;
- while(first!=NULL)
- {
- if(first->avg>Max->avg)
- Max=first;
- first=first->next;
- }
- return Max;
- }
- void addNode(Stpt *first,Stpt *last,StNode s)
- {
- Stpt node =(Stpt)malloc(sizeof(StNode));
- (node->name=s.name);
- (node->surname=s.surname);
- node->avg=s.avg;
- node->next=NULL;
- node->prev=NULL;
- if((*first)==NULL)
- {
- (*first)=node;
- (*last)=node;
- }
- else
- {
- (*last)->next=node;
- node->prev=(*last);
- (*last)=node;
- }
- }
- void printList(Stpt first)
- {
- while(first!=NULL)
- {
- cout<<first->name<<" "<<first->surname<<" "<<first->avg;
- first=first->next;
- }
- }
Add Comment
Please, Sign In to add comment