Gday All,
I'm currently writing a program using structures to accept input from the user including student number, student name and phone number.
THe program needs to accept this input, sort the arrays alphabetically and then print out the results to the user.
This is all fine. Then the user needs to be able to search for a name, and the details are returned to the user. I have the alternative of linear search or binary search.
Considering that the records will be sorted, i figure that binary search should be used as it is far more efficient.
I have everything working fine, except for the binary search of which i have no idea about. There is no information in any text book i have, and any examples or tutorials involve linked lists and files etc. which i have not come across yet. (e.g. fopen, node )
If anybody could lend a hand in helping me with a function to search the records i would be highly appreciative.
Sorry that i cannot post any code of the binary search function, but i can't even get my head around it.
Thanks.
Code:#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STUDENTS 10 typedef struct { char studentNum[10]; char lastName[ 15 ]; char firstName[ 10 ]; char phoneNum[ 15 ]; } studentData; void read_studentRecord( studentData [] ); void print_studentRecord( studentData [] ); void sort_studentRecord( studentData [] ); //void search_studentRecord( studentData [] ); //****************************************************************************************** int main() { studentData details [MAX_STUDENTS]; //define a structure array of size MAX_STUDENTS read_studentRecord( details ); sort_studentRecord( details ); print_studentRecord( details ); //search_studentRecord( details ); return 0; } //****************************************************************************************** void read_studentRecord ( studentData details[] ) { int i = 0; printf( "Enter Student Number (e.g. S00054321, 0 to end input)\n" ); scanf( "%s", details[i].studentNum ); while ( details[i].studentNum[0] != '0' ) { printf (" Enter Last Name:\n?"); scanf ( "%s", details[i].lastName ); printf (" Enter First Name:\n?"); scanf ( "%s", details[i].firstName ); printf (" Enter Phone Number:\n?"); scanf ( "%s", &details[i].phoneNum ); i++; printf ( "Enter Student Number\n?"); scanf ( "%s", details[i].studentNum); } } //***************************************************************************************** void sort_studentRecord ( studentData details[] ) { studentData hold; int i, pass; for ( pass = 1; pass <= 3 - 1; pass++ )//number of passes for ( i = 0; i <= 3 - pass -1; i++ )//single pass if ( strcmp( details[i].lastName, details[i+1].lastName ) == 1) {//sort in order of last name hold = details[i]; details[i] = details[i+1];//swap array elements if statement is true details[i+1] = hold; } } //***************************************************************************************** void print_studentRecord( studentData details[] ) { int i=0; if (details[0].studentNum != 0) printf ("\n%-10s%-16s%-11s%10s\n\n", "Student #", "Last Name", "First Name", "Phone #"); while ( details[i].studentNum[0] != '0' ) { printf("%-10s%-16s%-11s%10s\n", details[i].studentNum, details[i].lastName, details[i].firstName, details[i].phoneNum); i++; } } //***************************************************************************************** /* void search_studentRecord( studentData details[] ) { I dont think this will have a void return value? } */ //*****************************************************************************************