Ok well im desperate this problem is due by 12 am today and i have sent the teacher messages with my problem but no reply so basically can u help, im having problem with the (strcmp) etc.
This is what i have
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]; /*Declare array of char*/
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] );
return 0;
}
i saw this in another forum post but i dont understand it what it means , im obviously a amateur but please help me
Code:
char temp[namelen];
int itemp;
for (int x=0; x<CLASS_SIZE; x++) {
for (int y=1; y < x; y++){
if (strncmp(lname[x], lname[y],NAME_LEN)>0){
strcpy (temp, lname[y]);
strcpy (lname[y], lname[x]);
strcpy (lname[x], temp);
strcpy (temp, fname[y]);
strcpy (fname[y], fname[x]);
strcpy (fname[x], temp);
itemp = age[y];
age[y] = age[x];
age[x] = itemp;
}
}
}
Problem Description
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.