Hi i cant seem to get this program to work, i think the problem might be with the arrays but i cant seem to figure it out :/
This is the hw problem
Write a program that stores lists of names (the last name first) and ages in parallel arrays and sorts the names into alphabetical order keeping the ages with the correct names. The original arrays of names and ages should remain no changes. Therefore, you need to create an array of character pointers to store the addresses of the names in the name array initially. Apply the selection sort to this array of pointers so that the corresponding names are in alphabetical order. See the sample program of Fig. 9.14 from pages 474 to 476 in the textbook. You should use another array of pointers to age array to make sure the age is corresponding to the correct name.
Code:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define CLASS_SIZE 3
#define NAME_LEN 15
int strncmp(const char *s1, const char *s2, size_t n);
int main (void) {
char lname[CLASS_SIZE][NAME_LEN];
char fname[CLASS_SIZE][NAME_LEN];
int age[CLASS_SIZE];
char *temp;
for(int i=1; i<=CLASS_SIZE; i++){
printf("Enter first name, last name and age please:\n");
scanf("%s %s %d", fname[i], lname[i], &age[i]);
}
for (int x=0; x<CLASS_SIZE; x++) {
for (int y=1; y<NAME_LEN; y++){
if (strncmp(lname[y-1], lname[y],NAME_LEN)>0){
temp = lname[y];
lname[y] = lname[y-1];
lname[y-1] = temp;
}
}
}
for(int a=1; a<=CLASS_SIZE; a++)
printf("%s\n", lname[a] );
system(pause);
return 0;
}