Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* **************************************************************
**************************************************************
The following functions/symbolic constants/arrays are used
by generate random student
**************************************************************
************************************************************** */
#define UNOCCUPIED 0
#define OCCUPIED 1
#define SIZE 25
#define NUMBER_NAME 26
#define NUMBER_ID 26
#define NUMBER_GPA 26
char *name_source[]={"Crane", "Daniels", "Lewis","Crabb", "Keaton", "Harris", "Williamson", "Lane", "Richards", "Morris", "Nevins", "John", "Jane", "Wednesday", "Annette", "Noah", "Christopher", "George", "Washington", "Curie", "Rhonda", "Susan", "Mary", "Langely","Thompson", "Reynolds"};
unsigned id_source[]={ 1131, 1134, 1433,1516, 1788, 1899, 1902, 1908, 1923, 1978,2018, 2034, 2077, 2099, 2100,2133,4533,5634,5781, 5892, 5965, 6038, 7891, 7900, 8203, 8244};
double gpa_source[] ={1.15, 1.65, 1.88, 1.91, 1.987, 2.00, 2.14, 2.28 , 2.38, 2.45, 2.51, 2.55, 2.66, 2.81, 2.93, 2.99,3.0, 3.33, 3.45, 3.55, 3.65, 3.66, 3.78, 3.88,3.99, 4.00};
int name_position[NUMBER_NAME]={UNOCCUPIED};
int id_position[NUMBER_ID]={UNOCCUPIED};
int gpa_position[NUMBER_GPA]={UNOCCUPIED};
/* **************************************************************
**************************************************************
The following are for your programming assignment
**************************************************************
************************************************************** */
struct student {
char NAME[35];
unsigned ID;
double GPA;
struct student * nextptr;
};
void alphabetize( struct student *); //alphabetize linklist
struct student * list_initialize( int listsize); // student is to complete
double generate_avg_gpa(struct student *T); //student is to complete
void free_list(struct student *T);
void generate_random_student(struct student * S);
void print_list(struct student *L);
void space_print(int num);
int main()
{
struct student *toplistptr=NULL;
int listsize;
char buffer[10];
double avg_gpa;
printf("enter a value between 0 and 25\n");
gets(buffer);
listsize=atoi(buffer);
listsize%=SIZE;
// UNCOMMENT
toplistptr=list_initialize( listsize);
print_list(toplistptr);
alphabetize(toplistptr);
avg_gpa=generate_avg_gpa(toplistptr);
printf("\n\n\n*****************************\n*****************************\n");
printf("average gpa = %5.4f\n", avg_gpa);
printf("\n*****************************\n*****************************\n");
free_list(toplistptr);
//------------------------
}
struct student * list_initialize( int listsize)
{
struct student *L=NULL;
//start modification
struct student *ptr;
int i;
L = (struct student *)malloc(sizeof(struct student)); //initialize first link
generate_random_student(L);
ptr = L; //use ptr as reference to first link
for( i=0; i<listsize-1; i++) //create listsize # of links
{
ptr->nextptr = (struct student *)malloc(sizeof(struct student)); //set up nextptr of link
generate_random_student(ptr->nextptr);
ptr = ptr->nextptr; //move ptr to end | prepare for next looping
}
//end modifcation
return L;
}
void alphabetize( struct student *ptr)
{
struct student *sucptr = NULL, *preptr=NULL, *temp = ptr;
printf("______________________________________________\n");
while(temp != NULL)
{
//alphabetize link list
for( sucptr = ptr; sucptr != NULL; sucptr = sucptr->nextptr) //check list
{
if(strcmp(sucptr->NAME, temp->NAME) == 1) //find succeeding name
{
temp->nextptr = sucptr; //assign nextptr of new ptr to sucptr
break; //break from for loop
}
for( preptr = ptr; preptr != NULL; preptr=preptr->nextptr)
{
if(preptr->nextptr == sucptr) //when preptr's nextptr is sucptr...
{
preptr->nextptr = temp; //assign preptr's nextptr to ptr of new link
break;
}
}
}
print_list(sucptr);
temp = temp->nextptr;
}
getchar();
}
double generate_avg_gpa(struct student *T)
{
double answer=0.0;
int counter = 0;
struct student *ptr;
for(ptr = T; ptr != NULL; ptr=ptr->nextptr)
{
answer = ptr->GPA + answer;
counter++;
}
return answer/counter;
}
void print_list(struct student *L)
{
printf("NAME");
space_print(40-strlen("NAME"));
printf("ID");
space_print(8);
printf("GPA\n");
while(L!=NULL)
{
printf("%s", L->NAME);
space_print(40-strlen(L->NAME));
printf("%4d", L->ID);
space_print(5);
printf(" %4.3f\n", L->GPA);
L=L->nextptr;
}
}
void space_print(int num)
{
while(num-->0)
putchar(' ');
}
void generate_random_student(struct student * S)
{
int i;
/* generate random name */
i=rand()%SIZE;
while(name_position[i]!=UNOCCUPIED)
i=(i+1)%SIZE;
strcpy(S->NAME, name_source[i]);
name_position[i]=OCCUPIED;
/*generate random id */
i=rand()%SIZE;
while(id_position[i]!=UNOCCUPIED)
i=(i+1)%SIZE;
S->ID=id_source[i];
id_position[i]=OCCUPIED;
/* generate random gpa */
i=rand()%SIZE;
while(gpa_position[i]!=UNOCCUPIED)
i=(i+1)%SIZE;
S->GPA=gpa_source[i];
gpa_position[i]=OCCUPIED;
/* set nextptr to NULL */
S->nextptr = NULL;
}
void free_list(struct student *T)
{
struct student *tmp1;
while ( T!=NULL)
{
tmp1=T;
T=T->nextptr;
free(tmp1);
}
}