Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Also we haven’t learned anything past structs. Mini Project covers Chapter 8 of

ID: 3701179 • Letter: A

Question

Also we haven’t learned anything past structs. Mini Project covers Chapter 8 of Hanly and Koffman. This is an individual assignment. Please use only the covered material for working on this exercise. Your solution should demonstrate the learning from the book/class. Problem Statement Write a C program that reads from a file and stores lists of names (the last name first) and ages in parallel arrays and sorts using quicksort the names into alphabetical order keeping the ages with the correct names, then writes the output to a file.

Explanation / Answer

// File Name: FileQuickSort.c
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100

// Function to read file and display information
int readFile(char lastName[][20], char firstName[][30], char age[][4])
{
char temp[MAX];
// Counter for number of records
int counter = 0;
// File pointer declared
FILE *rFile;
// Opens the file for reading and checks whether it can be opened or not
if ((rFile = fopen("QuickSortData.txt", "r")) == NULL)
{
// If unable to open shows error message
printf("Error! in opening file for reading");
// Program exits if the file pointer returns NULL.
exit(1);
}// End of if condition

// Extracts the heading
fscanf(rFile, "%[^ ]s", firstName);
printf("%s", firstName);

// Loops till end of file
// Extracts last name
while(fscanf(rFile,"%s", lastName[counter]) != EOF)
{
// Extracts first name
fscanf(rFile, "%s", firstName[counter]);
// Extracts name if first name contains another word
fscanf(rFile, "%s", temp);
// Checks the first character of temp if it is alphabet
// Then two words available for first name
if(isalpha(temp[0]))
{
// Adds space at the end of first name
strcat(firstName[counter], " ");
// Adds the temp data at the end of the first name
strcat(firstName[counter], temp);
// Extracts age
fscanf(rFile, "%s", age[counter]);
}// End of if condition
// Otherwise temp contains age
else
// Copy the contents of the temp to age
strcpy(age[counter], temp);
// Increase the record counter by one
counter++;
}// End of while loop
// Returns number of records
return counter;
}// End of function

// Function to sort the names
void quickSort(char lastName[][20], char firstName[][30], char age[][4], int left, int right)
{
int i, j;
char *x;
// Temporary array for swapping
char temp[50];
// Stores the left position
i = left;
// Stores the right position
j = right;
// stores the left index position last name as povit
x = lastName[left];
// Loops till i value is less than or equals to j value
do
{
// Loops till i value is less than right and
// Compares i index position last name with last name stored in x as povit
while((strcmp(lastName[i],x) < 0) && (i < right))
// Increase the value of i
i++;
// Loops till j value is greater than left value and
// Compares j index position last name with last name stored in x as povit
while((strcmp(lastName[j],x) > 0) && (j > left))
// Increase the value of j
j--;
// Checks if i value is less than or equals to j
if(i <= j)
{
// Swap last name
strcpy(temp, lastName[i]);
strcpy(lastName[i], lastName[j]);
strcpy(lastName[j], temp);

// Swap first name
strcpy(temp, firstName[i]);
strcpy(firstName[i], firstName[j]);
strcpy(firstName[j], temp);

// Swap age
strcpy(temp, age[i]);
strcpy(age[i], age[j]);
strcpy(age[j], temp);
// Increase the i value and decrease the j value
i++;
j--;
}// End of if condition
} while(i <= j); // End of do - while loop
// Checks if left is less than j value
if(left < j)
// Recursively call the function
quickSort(lastName, firstName, age, left, j);
// Checks if i value is less than right value
if(i < right)
// Recursively call the function
quickSort(lastName, firstName, age, i, right);
}// End of function

// main function definition
int main()
{
// To store last name, first name and age
char lastName[MAX][20];
char firstName[MAX][30];
char age[MAX][4];
// To store number of records
int numberOfRecords, c;
// Calls the function to read file contents
numberOfRecords = readFile(lastName, firstName, age);

// Displays records before sorting
printf(" Before sorting: ");
for(c = 0; c < numberOfRecords; c++)
printf(" Last name = %s first name: %s Age = %s", lastName[c], firstName[c], age[c]);

// Calls the function to sort
quickSort(lastName, firstName, age, 0, numberOfRecords-1);

// Displays records after sorting
printf(" After sorting: ");
for(c = 0; c < numberOfRecords; c++)
printf(" Last name = %s first name: %s Age = %s", lastName[c], firstName[c], age[c]);
}// End of main function

Sample Output:

Last Name First Name Age
Before sorting:
Last name = Karki first name: Bimal Bikram Age = 42
Last name = Ramirez first name: Aaron Age = 36
Last name = Carroll first name: Ti'area Age = 54
Last name = Huynh first name: Khang Age = 68
Last name = Lagares first name: Hunter Age = 72
Last name = West first name: Cody Age = 59
Last name = Owens first name: Hunter Age = 90
Last name = Cleveland first name: Demetrius Age = 5
Last name = Urech first name: Thomas Age = 59
Last name = Justus first name: Boston Age = 28
Last name = Boone first name: Evan Age = 80
Last name = Williams first name: Phillips Age = 18
Last name = Wilkinson first name: Drew Age = 93
Last name = Flores first name: Ryan Age = 12
Last name = Bartley first name: Anna Age = 36
Last name = Lynskey first name: Ethan Age = 84
Last name = Anaghara first name: Ebubechukwu Age = 37
Last name = Caon first name: Cristiano Eleutherio Age = 50
Last name = Bakliwal first name: Gautam Age = 27
Last name = Kiros first name: Michael Age = 8
Last name = Quintanilla first name: Emily Age = 7
Last name = Chanthavisouk first name: Armon Age = 27
Last name = Goonie first name: Rajeev Age = 19
Last name = Michell first name: Brain Age = 25
Last name = Anand first name: Aishwarys Dhirendrakumar Age = 0
Last name = McFarlane first name: Scott Age = 94
Last name = Palting first name: Faus Age = 73
Last name = Vermillion first name: Jason Age = 74
Last name = McDougal first name: Luke Age = 43

After sorting:
Last name = Anaghara first name: Ebubechukwu Age = 37
Last name = Anand first name: Aishwarys Dhirendrakumar Age = 0
Last name = Bakliwal first name: Gautam Age = 27
Last name = Bartley first name: Anna Age = 36
Last name = Boone first name: Evan Age = 80
Last name = Caon first name: Cristiano Eleutherio Age = 50
Last name = Carroll first name: Ti'area Age = 54
Last name = Chanthavisouk first name: Armon Age = 27
Last name = Cleveland first name: Demetrius Age = 5
Last name = Flores first name: Ryan Age = 12
Last name = Goonie first name: Rajeev Age = 19
Last name = Huynh first name: Khang Age = 68
Last name = Justus first name: Boston Age = 28
Last name = Karki first name: Bimal Bikram Age = 42
Last name = Kiros first name: Michael Age = 8
Last name = Lagares first name: Hunter Age = 72
Last name = Lynskey first name: Ethan Age = 84
Last name = McDougal first name: Luke Age = 43
Last name = McFarlane first name: Scott Age = 94
Last name = Michell first name: Brain Age = 25
Last name = Owens first name: Hunter Age = 90
Last name = Palting first name: Faus Age = 73
Last name = Quintanilla first name: Emily Age = 7
Last name = Ramirez first name: Aaron Age = 36
Last name = Urech first name: Thomas Age = 59
Last name = Vermillion first name: Jason Age = 74
Last name = West first name: Cody Age = 59
Last name = Wilkinson first name: Drew Age = 93
Last name = Williams first name: Phillips Age = 18

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote