Hey everyone,
I'm having trouble with a program I'm working on. In this program I'm supposed to sort an array of names. I ask the user how manys names will be inputed. Number of names will not exceed 100. Names will be in the format of "Last,First,age" with no spaces and there will always be three fields. The user will always use this format. The names string will be at most 40 characters. After the user has inputed the names, I ask how are they to be sorted. They are sorted by either last, first, or age. Then I print the names out in sorted order. For people who have the same last name, sort by first name. For people with the same age, sort by last name and if that is also the same sort by first name. For people with the same first name, sort by last name. No two people will have the same first and last name.
There are 3 files, one with main, one with the functions that are called (one function scans the array, another sorts the array, and the last prints it) and another which is the header that contains the function prototypes.
I was able to do the coding for the main program which looks like this:
#include<stdio.h>
#include<string.h>
#include"names_func.h"
int main(void){
int type,num;
char names[MAX][LEN];
/* Ask user for how many names*/
printf("How many names?: ");
scanf("%d",&num);
if(num <= 0)
exit(0);
if(num>=100){
printf("Entered too many\n");
exit(0);
}
/*Get the names for the users*/
getnames(names,num);
/*Ask user how to sort names*/
printf("How to sort names?\n");
printf("1: Last\n2: First\n3: Age\n");
scanf("%d",&type);
/*Sort the names*/
sort_names(names,num,type);
/*Print the sorted array out*/
print_names(names,num);
return(0);
}
I was also able to do a little bit of the functions:
#include <stdio.h>
#include <strings.h>
#include "names_func.h"
char getnames(char names[], int num){
int i;
for(i=0; i<num; i++){
scanf("%s", names);
}
}
char sort_names(char names, int num, int type){
if(type==1){
(This is the part which I dont know how to do. I am confused about how to sort the 2d array.)
If anyone can help me with the coding for this that would be great.
Thanks for your help.