Mga Bro, Good Day!
![grin](images/smilies/grin.gif)
Naa nasad ko diri karon to ask for help from your most gracious wisdom! Anyways, I have another C programming language problem. This time, it includes structures, pointer to pointer and linked lists. I'm posting my code first:
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAX 100
typedef struct {
char fname[40];
char lname[40];
char mi;
}NAMETYPE;
typedef struct {
NAMETYPE name;
long int id;
char course[20];
int year;
float GPA;
}STUDREC;
struct node {
STUDREC x;
struct node *next;
};
typedef struct node nd;
int GetStudRec(STUDREC *, nd *);
void Search(nd *);
void DisplayStudRec(nd *);
void DisplayAllStudentRec(nd *);
void DisplayTopStudent(nd *, int);
int main(void)
{
STUDREC s[MAX];
nd *head;
int last;
clrscr();
head=(nd *)malloc(sizeof(nd));
last = GetStudRec(s, head);
Search(head);
DisplayAllStudentRec(head);
DisplayTopStudent(head, last);
return 0;
}
int GetStudRec(STUDREC *s, nd *head)
{
nd *p;
int i=0, j, last;
char ch;
float a;
do
{
printf("First name of student %d: ", i+1);
scanf("%s", s[i].name.fname);
printf("Last name of student %d: ", i+1);
scanf("%s", s[i].name.lname);
printf("Middle initial of student %d: ", i+1);
fflush(stdin);
scanf("%c", &s[i].name.mi);
printf("ID number of student %d: ", i+1);
scanf("%d", &s[i].id);
printf("Course of student %d: ", i+1);
scanf("%s", s[i].course);
printf("Year of student %d: ", i+1);
scanf("%d", &s[i].year);
printf("GPA of student %d: ", i+1);
scanf("%f", &a);
s[i].GPA=a;
printf("\n");
printf("Do you wish to continue? Y/N ");
fflush(stdin);
scanf("%c", &ch);
if (ch=='Y' || ch=='y')
{
j=1;
i++;
printf("\n");
}
else if (ch=='N' || ch=='n')
{
j=0;
last=i;
}
}while (j!=0 && i<MAX);
printf("\n");
head->x=s[0];
p=head;
for (i=1;i<=last;i++)
{
p->next=(nd *)malloc(sizeof(nd));
p->next->x=s[i];
p=p->next;
}
p->next=NULL;
return (last);
}
void Search(nd *head)
{
nd *p;
long int iden;
p=head;
printf("Student ID to search: ");
scanf("%d", &iden);
while (p->x.id!=iden && p!=NULL)
p=p->next;
if (iden==p->x.id)
DisplayStudRec(p);
else if (p==NULL)
{
printf("\n");
printf("STUDENT DOES NOT EXIST\n");
}
}
void DisplayStudRec(nd *p)
{
printf("The student you are looking for:\n");
printf("Name: %s %c %s\tID number: %d\n", p->x.name.fname, p->x.name.mi, p->x.name.lname, p->x.id);
printf("Course: %s\t\t\tYear: %d\n", p->x.course, p->x.year);
printf("GPA: %.1f\n", p->x.GPA);
printf("\n");
}
void DisplayAllStudentRec(nd *head)
{
nd *p;
p=head;
printf("\nRecord of all students:\n");
while (p!=NULL)
{
printf("Name: %s %c %s\tID number: %d\n", p->x.name.fname, p->x.name.mi, p->x.name.lname, p->x.id);
printf("Course: %s\t\t\tYear: %d\n", p->x.course, p->x.year);
printf("GPA: %.1f\n", p->x.GPA);
printf("\n");
p=p->next;
}
}
void DisplayTopStudent(nd *head, int last)
{
nd *p, *q;
float temp;
int count=0;
q=head;
if (last>0)
while (q!=NULL && count!=last)
{
p=q->next;
if (q->x.GPA > p->x.GPA)
temp=q->x.GPA;
q=p;
p=p->next;
count++;
}
else if (last==0)
temp=p->x.GPA;
printf("Student with the Highest GPA: %.1f\n", temp);
}
Explain nako briefly ang expected operation each function: (These functions are required though we can add more functions if we have to.)
-GetStudRec: Mukuha siya ug information about a student. Then transfers the data into a linked list.
-Search: Asks an input (student ID) from the user, then searches the linked list for that specific student of that distinct ID.
-DisplayStudRec: Mao ni ang tawagon ni Search() function if naay match nga student ID.
-DisplayAllStudentRec: Displays all student records.
-DisplayTopStudent: I-display niya ang student nga naay pinaka dako ug GPA from the linked list.
Ang problems nako ani nga code kay kani:
-Sa Search() function: pirmi NULL ang labas sa pag trace nako.
-Sa pag print sa student with the highest GPA: zero man ang iyang i-print. Nag provide ko ug duha ka pointers so that ma follow nila ang liked list and para mas dali nila ma compare ang sulod.
Mga bro, naa sad koy mga questions that I want to ask in continuation ani:
Is it possible to call function main()? Included man gud sa problem namo nga maghimo ug user interface nga mu ask sa user unsa iyang gusto buhaton after inputting the student records. Naghuna-huna ko nga magbutang ug call ni main() in order to provide an effective program.
Mga Bro, Thank You kaayo sa inyong tabang kanako! I know "noobs" ko, that's why ning ask ko ninyo ug tabang. I acknowledge my limitations but I don not accept defeat. I hope matabangan ko ninyo mga bro!
Thank You!