Link to home
Start Free TrialLog in
Avatar of solid_hyprid
solid_hyprid

asked on

sorting help

Hi, Can someone give me some advice for this program?  Especially on getting the user input and sorting it accordingly!

First your program will ask the user how manys names will be inputed. Number of names will not exceed 100. Then ask the user for the names. Names will be in the format of "Last,First,age" with nospaces and there will always be
three fields. Assume the user will always use this format. The names string will be at most 40 characters. After the user has inputed the names, ask how are they to be sorted. They are sorted by either last, first, or age. Then 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.
Avatar of Egore
Egore

I'm thinking that you would want to read in the values using scanf() or gets() or equivalent.  Then copy the data into a doubly-linked list in sorted order:

typedef struct name_list
{
  char *first_name;
  char *last_name;
  int age;

  name_list *prev;
  name_list *next;
};
struct name_list *first;

You would sit there reading in names with scanf() or gets() or something along those lines.  Then you'd loop over the input searching for commas.  At each comma you'd copy the data into a temporary variable.  Once you have parsed out the first name, last name, and age you would loop through the linked list looking for the proper location for this entry (using strcmp() or stricmp() to test for alphabetical ordering).  This way you sort as you read in the values and don't have to worry about having a gather data phase and a sort phase, you just have a smart gather data phase.

This assumes that you're familiar with malloc() and strcpy() to make room for the data in a string / create new entries in the list and to copy that data to the proper location.

I appologize for being rusty on C struct syntax as I've been a C++ junkie instead of a C junkie for a couple years now.

Good luck with your assignment.
First, we can't give you code because as a homework assignment it is against the board policies.

Second, egore's approach will only work with a single sort order as written - you could add pointers to keep track of the other sort orders, but you really have to want to do it that way.

Third, egore's approach for getting the data from the user is the way to go.

If you don't care about memory (with a max of 100 structs, it is no big deal), you can just declare an array of 100 structs.

You can use qsort with different compare functions for name vs age - I will leave that as a task for you to try first, then you can post your efforts and we will help you if you need it...
Avatar of solid_hyprid

ASKER

Heres what i have so far....Can someone help me on the sorting...and tell me if i'm going into the right direction?  Thanks~!

#include <stdio.h>
#include "names_func.h"

void getnames(char names[][LEN], int num){

int i;
for(i=0;i<num;i++)
printf("Enter name %d:", i+1);
scanf("%s", names[i]);
i++;

}

void sort_names(char names[][LEN], int num, int type){
  int i=0,j=0,x,iagetemp=0;
   char first[MAX][FIRST+LAST],last[MAX][LAST+FIRST];
  char age[MAX][AGE+LAST+FIRST];
  char agetemp[FIRST+LAST+AGE];
 



// last name





for(x=0;x<num;x++){
  i=0;
  j=0;

  while(names[x][i]!=','){
    i++;
    j++;
  }
  strncpy(last[x], &names[x][0], i);       // last is the array of last names
  last[x][i] = '\0';

  while(names[x][j+1]!=','){
    j++;
  }
 
  strncpy(first[x][LEN], &names[x][i+1], j); //   first is the array of first names
  first[x][j-i] = '\0';
  strcat(first[x],last[x]);   // i think this connects first and last name
 

  strcpy(agetemp, &names[x][j+2]);
    iagetemp = atoi(agetemp);
    sprintf(age[x],"%03d",iagetemp);
    strcat(age[x],last[x]);
    strcat(age[x],first[x]);

}

}





ASKER CERTIFIED SOLUTION
Avatar of gj62
gj62

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Nothing has happened on this question in over 10 months. It's time for cleanup!

My recommendation, which I will post in the Cleanup topic area, is to
accept answer by gj62.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

jmcg
EE Cleanup Volunteer