Answered step by step
Verified Expert Solution
Question
1 Approved Answer
************URGENT********** Can someone help me with this code? The program opens and runs, but when I try to print out the sorted arrays, none of
************URGENT********** Can someone help me with this code? The program opens and runs, but when I try to print out the sorted arrays, none of the menu options function properly. It prints out random numbers. #include#include struct array { char model[50]; int numI; float numF; char color[50]; }; struct array list[100]; int count = 0; void readData() { FILE *file; int ch; int position = 0; file = fopen("hw2.data","r"); if(file != NULL){ ch = fgetc(file); while(fscanf(file, "%s %f %d %s", list[position].model, list[position].numF, list[position].numI, list[position].color) == 4){ position++; count++; } } else { printf("Error Reading File "); exit(0); } } void sortfloat()//float list[].numF, int count { int c,g; for (c = 0; c < count-1; c++) for (g =0; g< count - c - 1; g++) if(list[g].numF > list[g+1].numF ) { int temp = list[c].numF; list[c].numF = list[g].numF; list[g].numF = temp; } } void sortint()//int list[].numI, int count) { int c,g; for (c = 0; c < count-1; c++) for (g =0; g< count - c - 1; g++) if(list[g].numI > list[g+1].numI ) { int temp = list[c].numI; list[c].numI = list[g].numI; list[g].numI = temp; } } void printhightolow() { int i; for (int i = 100; i > 0; i--) { printf("%s %f %d %s", list[i].model, &list[i].numF, &list[i].numI, &list[i].color); } } void printlowtohigh() { int i; for (int i = 0; i < 100; i++) { printf("%s %f %d %s", list[i].model, &list[i].numF, &list[i].numI, &list[i].color); } } int main() { int i; int choice; //Loop for the menu options do { printf(" 1. Sort data by the float value & print high to low "); printf(" 2. Sort data by the float value & print low to high "); printf(" 3. Sort data by the int value & print high to low "); printf(" 4. Sort data by the int value & print low to high "); printf(" 5. Exit "); printf(" Please Enter a number corresponding to one the options: "); scanf("%d", &choice); switch (choice) { case 1: sortfloat(); printhightolow(); break; case 2: sortfloat(); printlowtohigh(); break; case 3: sortint(); printhightolow(); break; case 4: sortint(); printlowtohigh(); break; case 5: exit(0); break; default: printf("Sorry! Entered a wrong value. Try again."); } } while(1); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started