Question: I pretty much have this working except when I go to sort the array struct using a bubble sort something seems to mess up and
I pretty much have this working except when I go to sort the array struct using a bubble sort something seems to mess up and I don't know why. Here is my code down below
#define _CRT_SECURE_NO_DEPRECATE #define _CRT_SECURE_NO_WARNINGS #include
//Prototype void sort(struct people input[], int size);
typedef struct people{ char firstname[20]; char lastname[20]; int age; }People;
//Main Function int main(void) { FILE *unsorted = fopen("input.txt", "r"); FILE *sorted = fopen("output.txt", "w");
People toBeSorted[20] ; //People *isSorted[20]; int i = 0; char firstN[20] = { "\0" }; char lastN[20]= { "\0" }; char age[20]= { "\0" }; if (!unsorted) { printf("Error! "); return -1; } while (!feof(unsorted))//While File is scanning { fscanf(unsorted, "%s %s %s", &lastN, &firstN, &age); strcpy(toBeSorted[i].lastname, lastN); strcpy(toBeSorted[i].firstname, firstN); toBeSorted[i].age = atoi(age); printf("%s %s %d ", toBeSorted[i].lastname, toBeSorted[i].firstname, toBeSorted[i].age);//Visual Testing i = i + 1; }
sort(toBeSorted, i);//bubble sort
if (!sorted) { printf("Error! "); return -1; } for (int k = 0; k
//fclose(sorted); //fclose(unsorted); return 0; }
void sort(struct people input[], int size) { char templast[20],tempfirst[20]; int tempage,compare; int k = 0; int j = 0; for (k = 0; k
if (compare>0) {//Manually swaps each compoent in structure strcpy(templast, input[j + 1].lastname);//Copies to temps strcpy(tempfirst, input[j + 1].firstname); tempage = input[j + 1].age;
strcpy(input[j + 1].lastname,input[j].lastname);//Copies from jth integer to j+1 strcpy(input[j + 1].firstname,input[j].firstname); input[j + 1].age = input[j].age;
strcpy(input[j].lastname,templast);//Sets jth integer equal to temps strcpy(input[j].firstname, tempfirst); input[j].age = tempage; } } } }
For this assessment you are to write a C program that reads names and corresponding ages of people records from a file, and then sorts the records based on the person's last name (in descending order). Once the records have been sorted, you must output the sorted records to a different file. You do NOT have to perform error checking on the records! Please upload your solution in the appropriate dropbox (zip your solution folder). You may assume that they are stored in the input file in the appropriate format and alignment as follows (last name, first name age): Ruth, Babe Jordan, Michael Obama, Barack Potter, Harry Favre, Brett Potter, Carrie Hamm, Mia 115 46 48 16 38 40 33 After you have sorted the input records, the output records should be displayed in the output file as follows (with the age first): 115 16 40 48 46 33 38 Ruth, Babe Potter, Harry Potter, Carrie Obama, Barack Jordan, Michael Hamm, Mia Favre, Brett Note: The records were sorted based on last name, not the age
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
