Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

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.
0
solid_hyprid
Asked:
solid_hyprid
1 Solution
 
EgoreCommented:
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.
0
 
gj62Commented:
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...
0
 
solid_hypridAuthor Commented:
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]);

}

}





0
 
gj62Commented:
OK - is this homework?  If so, you probably can't use qsort() - if not, take a look at it - it will make the problem trivial.

If you've got to do your own sort, and this is homework, look at this link (yes, it does have complete source code, but it also has good explanations on sorting...).

http://www.yendor.com/programming/sort/

They are internal sorts - just what you need for this project...
0
 
jmcgOwnerCommented:
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
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now