I found an example code that is doing similar functions that i am supposed to do, I just dont really know how to change the code so it applies to my code.
Any ideas??
Code:
//included libraries
#include <stdio.h>
#include <string.h>
//constants
#define NUMSTUDENTS 5
//Structures
//Structures are used to create new data types
struct record {
char fname[20];
char lname[20];
int PID;
};
//Function Prototypes
void print_students (struct record students[]);
void sort (struct record students[]);
void swap (struct record students[], int a, int b);
int namecmp (struct record sA, struct record sB);
//Main Function
int main() {
FILE *ifp = fopen("students.txt", "r");
int i;
//In order to declare a variable of the structure type
//you need "struct <struct name> <variable name>.
//The [NUMSTUDENTS] creates an array of structures.
//The size is dictated by NUMSTUDENTS.
struct record students[NUMSTUDENTS];
//This loop reads in all the student information
//from the file.
for(i=0; i<NUMSTUDENTS; i++) {
fscanf(ifp, "%s", students[i].lname);
fscanf(ifp, "%s", students[i].fname);
fscanf(ifp, "%d", &students[i].PID);
}
//Call the function "sort" to sort the array of students
sort(students);
//Call the function "print_students" to print the list of students
print_students(students);
return 0;
}
/* Pre-Conditions: This function takes in an array of
* structures of type record.
* Post-Conditions: This function prints the students information.
* The constant NUMSTUDENTS is used to control the loop.
*/
void print_students (struct record students[]) {
int i;
for(i=0; i<NUMSTUDENTS; i++)
printf("%s %s: %d\n", students[i].fname, students[i].lname,
students[i].PID);
return;
}
/* Pre-Conditions: This function takes in two structures of type record.
* These are labeled sA and sB for student A and student B, respectively.
* Post-Conditions: This function compares the names of the two
* students to see which should come first lexicographically.
* The function returns -1 if sA comes first, -1 if sB comes first,
* and 0 if the students have the same name.
*/
int namecmp (struct record sA, struct record sB) {
if (strcmp(sA.lname, sB.lname) < 0)
return -1;
else if (strcmp(sA.lname, sB.lname) > 0)
return 1;
else {
if (strcmp(sA.fname, sB.fname) < 0)
return -1;
else if (strcmp(sA.fname, sB.fname) > 0)
return 1;
else
return 0;
}
return 3;
}
/* Pre-Conditions: This function takes in an array of structures
* of type record and two integers a and b. a and b represent two
* indices of the array - these are the two structs in the array
* that are to be swapped.
* Post-Conditions: This function swaps the two structs in the
* array at the indicated indices.
*/
void swap (struct record students[], int a, int b) {
struct record temp;
temp = students[a];
students[a] = students[b];
students[b] = temp;
return;
}
/* Pre-Conditions: This function takes in an array of structures
* of type record.
* Post-Conditions: This function sorts the array.
* Specifically, this is an implementation of the Bubble Sort Algorithm.
* See http://en.wikipedia.org/wiki/Bubble_sort for more information.
* This function will call both namecmp and swap.
*/
void sort (struct record students[]) {
int flag = 0, i;
while(flag == 0) {
flag = 1;
for (i=1; i<NUMSTUDENTS; i++) {
if (namecmp(students[i-1], students[i]) > 0) {
swap(students, i-1, i);
flag = 0;
}
}
}
return;
}