I been away from C programming for awhile now, and it's difficult for me to figure this out. The program itself works, however I am supposed to turn this code into an array of structures instead of having 3 parallel arrays.
I heard it doesn't take long if you already have a working set of code.
Code:#include <stdio.h> // preprocessor directive to allow use of printf()/sca$ #include <string.h> //preprocessor directive to allow use of strings //typedef below used to create a structure tag for structure definitions typedef struct person { //Algorithm step 2 char first_name[20]; //array "first_name"element //Algorithm step 3 char last_name[20]; //array "last_name"element //Algorithm step 4 char age[4]; //array "age" element }ppl ; //tag name void populate (ppl []); //PCF populate prototype void last_name_sort (ppl []); //PCF last_name_sort prototype int main (void ) //Main function- returns and integer/ accepts no argumen$ { //Marks beginning of the main function's statement int i; //integer i created and initialized //Algorithm step 1 completed in next four lines printf("Hello! \nThe purpose of this program is to use parallel arrays $ printf("order the names aphabetically (by last name) without mixing the$ printf("names and ages. The newly sorted information will be printed to$ printf("screen. \n\n\n\n"); ppl person[7]; //defined structure "person" populate (person); //defined structure passed to PCF populate last_name_sort (person); //defined structure passed to PCF last_name_s$ printf("\n\n<-------------------------~SORTED LIST~-------------------$ for (i=0; i<= 6; i++) { //Algorithm step 8 in for loop printf("%s %s %s\n", person[i].first_name , person[i].last_name, pe$ } printf("\n\n\n"); printf("Thank you for using my program. I hope it was helpful!\n\n"); return (0) ; } void populate (ppl person[]) { int i; //creation of integer variable i printf("Please enter a first name, last name, and age with a space in b$ printf("and the first letter of the first and last name capitalized: \n$ printf("\n\n<-----------------------~UNSORTED LIST~--------------------$ for (i=0; i<= 6; i++) { scanf("%s %s %s",person[i].first_name , person[i].last_name, person[i].$ } return; } void last_name_sort( ppl person[]) { int i, j; char temp[20]; //creation of first temporary array for 3way switch of las$ char temp_1[20]; //creation of first temporary array for 3way switch of f$ char temp_2[4]; //creation of first temporary array for 3way switchof age for (i=0; i<=5; i++) //outer for loop { for(j=0; j<=5; j++) //inner "nested" for loop { if (strncmp(person[j].last_name, person[j+1].last_name, 7) >0) { //Algorithm for last name sorting strcpy (temp, person[j].last_name); strcpy (person[j].last_name, person[j+1].last_name); strcpy (person[j+1].last_name, temp); //Algorithm so first name follows where last name is sorted strcpy (temp_1, person[j].first_name); strcpy (person[j].first_name, person[j+1].first_name); strcpy (person[j+1].first_name, temp_1); //Algorithm so age follows where last name is sorted strcpy (temp_2, person[j].age); strcpy (person[j].age, person[j+1].age); strcpy (person[j+1].age, temp_2); } } return; } }
Any tip, help or comment would be great!
Thank you.



4Likes
LinkBack URL
About LinkBacks



