I am working on this code where you'll need to compare last names and first names and swap them in alphabetical order. I got that right but when I need to find the name and print that using my binary search function searching for the last name it will not print out the correct data or record. It will print a different name record. I am using pointers and calling functions. Please help. (int main()) was already provided
struct _student
{
char firstname[16];
char lastname[16];
char address[32];
char city[24];
char county[24];
char state[3];
char zip[9];
char phone[16];
char email[36];
};
typedef struct _student Student;
void swap(Student* s1, Student* s2)
{
Student Swap;
Swap = *s1;
*s1 = *s2;
*s2 = Swap;
}
int nameCmp(Student* s1, Student* s2)
{
int result1 = strcmp(s1->lastname, s2->lastname);
int result2 = strcmp(s1->firstname, s2->firstname);
if (result1 < 0)
{
return -1;
}
else if (result1 == 0)
{
if (result2 < 0)
{
return -1;
}
else if (result2 == 0)
{
return 0;
}
return 0;
}
else
{
return 1;
}
}
void exchangeSort(Student s[], int count, int (*compare)(Student*, Student*))
{
for (int i = 0; i < count - 1; i++)
{
for (int j = i + 1; j < count; j++)
{
if (compare(&s[i], &s[j]) > 0)
{
swap(&s[i], &s[j]);
}
}
}
}
int bSearch(Student data[], int count, char name[], int (*compare)(Student*, Student*))
{
int low = 0, high = count - 1;
while (low <= high)
{
int mid = (high + low) / 2;
int cmp = compare(name, &data[mid]);
if (cmp == 0)
return mid;
if (cmp > 0)
high = mid - 1;
else
low = mid + 1;
}
}
int readRowFromFile(FILE* f, Student* s)
{
memset(s, 0, sizeof(Student));
return fscanf(f, " %[^,], %[^,], %[^,], %[^,], %[^,], %[^,], %[^,], %[^,], %s\n",
s->firstname, s->lastname, s->address, s->city, s->county, s->state, s->zip, s->phone, s->email);
}
// Main routine
int main()
{
// Variable to hold the print format string
char* fmt = "%-12s%-16s%-32s%-20s%-3s%-6s%-24s\n";
// Declare an array variable to hold all the records
Student rData[1000];
// Open the file for reading, MAKE SURE THE FILE NAME MATCHES WHAT YOU HAVE
FILE* f = fopen("Samplerecords.csv", "rt");
if (!f)
{
printf("Unable to open file. \n");
keypress();
return 0;
}
// Variable to hold the number of records actually read
int count = 0;
// Skip the header row
int rc = readRowFromFile(f, &rData[0]);
// Read the rest of the file
while (!feof(f) && count < 1000)
{
rc = readRowFromFile(f, &rData[count]);
if (rc > 0)
count++;
}
// Close the file
fclose(f);
// Print the first 10 records before sorting
printf("Before sorting:\n");
for (int i = 0; i < 10; i++)
{
printf(fmt, rData[i].firstname, rData[i].lastname,
rData[i].address, rData[i].city, rData[i].state, rData[i].zip, rData[i].email);
}
// Sort the records by name
exchangeSort(rData, count, nameCmp);
// Print the first 10 records after sorting
printf("\n\nAfter name sort:\n");
for (int i = 0; i < 10; i++)
{
printf(fmt, rData[i].firstname, rData[i].lastname,
rData[i].address, rData[i].city, rData[i].state, rData[i].zip, rData[i].email);
}
// Find the index of the record of "Bernardo Figueroa" (search last name only)
int index = bSearch(rData, count, "Figeroa", nameCmp);
if (index >= 0)
{
printf("\nFound:\n");
printf(fmt, rData[index].firstname, rData[index].lastname,
rData[index].address, rData[index].city, rData[index].state, rData[index].zip, rData[index].email);
}
else
{
printf("\nRecord not found\n");
}
keypress();
return 0;
}