?
Solved

need help on sorting assignment

Posted on 2003-02-27
5
Medium Priority
?
171 Views
Last Modified: 2010-04-15
program assignment:

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.

 
 
 
Like the program before, there are structural requirements. This part also
requires incremental compiling.

names.c   contains main() and is given to you.
names_func.h  function prototypes for names.c
names_func.c  functions used in main.
 
Functions used in main:
 
getnames        It fills the array with the proper number of names.
 
sort_names      It sorts the array based on the sorting type.
 
print_names     prints the names in the array.


I need help on how to sort the first name, and age...which are after commas...and still print out with the format last,first,age




My code below:

#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]);

}

}

// sort...save for later
void bubble(void *p, int width, int N)
{
    int i, j, k;
    unsigned char buf[MAX_BUF];
    unsigned char *bp = p;

    for (i = N-1; i >= 0; i--)
    {
        for (j = 1; j <= i; j++)
        {
          k = compare((void *)(bp + width*(j-1)), (void *)(bp + j*width));
          if (k > 0)
            {
             memcpy(buf, bp + width*(j-1), width);
             memcpy(bp + width*(j-1), bp + j*width , width);
             memcpy(bp + j*width, buf, width);
            }
        }
    }
}
 
// compare for sort
int compare(void *m, void *n)
{
    char *m1 = m;
    char *n1 = n;
    return (strcmp(m1,n1));
}

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

for (i = 0; i < 5; i++)            /* show the sorted strings */
    {
        printf("%s\n", arr2[i]);
    }
    return 0;
}



}

// so the arrays will will sort at first name and age...NEEDS HELP!
if((s1[i])==(s2[j]))
                {
                        if(s1[i]==',')
                        {
                                return 0;
                        }
                        i++;
                        j++;
                        continue;
                }
                else if(s1[i]==',')
                        return -1;
                else if(s2[j]==',')
                        return 1;
                else if(s1[i]>s2[j])
                        return 1;
                else if(s2[j]>s1[i])
                        return -1;
0
Comment
Question by:stuipd_guy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 5

Expert Comment

by:Kocil
ID: 8039369
Well, your code is too complicated for me.
Pardon me to suggest my own code.
Assume that your name array is in this format;

#define MAX_ARRAY 100
#define MAX_NAME  40
typedef char NameArray[MAX_ARRAY][MAX_NAME];
enum {SORT_FIRST, SORT_LAST, SORT_AGE};

/* prototypes to get First/Last/Age from names string */
char *GetFirst(char *f, char *name);
char *GetLast(char *l, char *name);
int GetAge(char *names);

/* prototypes for 3 compares */
CompareFirst(char* a, char *b);
CompareLast(char* a, char *b);
CompareAge(char* a, char *b);

/* sort_names
 * na = the names array, for example
 *   na[0] = "Beckham,David,26"
 *   na[1] = "Shearer,Alan,36"
 * num = number of names in the array, for example 2
 * na will be sorted (changed)
 */
sort_names(NameArray* na, int num, int sort_type)
{
   switch sort_type {
      case SORT_FIRST : qsort(na, num, MAX_NAME, SortFirst); break;
      case SORT_FIRST : qsort(na, num, MAX_NAME, SortLast); break;
      case SORT_FIRST : qsort(na, num, MAX_NAME, SortAge); break;
   }
}

/* GetFirst
 * Return first name from names
 */
char *GetFirst(char *f, char *names)
{
   while(*names != ',') {
      *f = *names; f++; names++;
   }
   *f = 0;
   return f;
}

/* Write your own GetLast and GetAge */


/* CompareFirst
 * Compare FirstName, if equal then compare LastName
 * return 0 -> a == b
 *       -1 -> a < b
 *        1 -> a > b
 */
CompareFirst(char* a, char *b)
{
   int compare;
   char fa, fb;
   GetFirst(fa, a); GetFirst(fb,b);
   compare = strcmp(fa, fb);
   if (compare == 0) {
      GetLast(fa, a); GetLast(fb,b);
      compare = strcmp(fa, fb);
   }
   return compare;
}

/* Write your own CompareLast and CompareAge */


Put that code in names_func.c.
And call this from main like

main()
{
   NameArray names;
   ini num;
 
   ....
   getnames(names, num);
   ...
   sort_names(names, num, SORT_FIRST);
   ...

   print_names(names, num);
}

------
Cheers
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8039372
Ooops sorry, some correction.

===========
sort_names(NameArray* na, int num, int sort_type)
{
  switch sort_type {
     case SORT_FIRST : qsort(na, num, MAX_NAME, CompareFirst); break;
     case SORT_FIRST : qsort(na, num, MAX_NAME, CompareLast); break;
     case SORT_FIRST : qsort(na, num, MAX_NAME, CompareAge); break;
  }
}
============
My code is not checked. Expect syntax error etc :)
0
 

Author Comment

by:stuipd_guy
ID: 8039638
/* GetLast
* Return last name from names
*/
int count;
char *GetLast(char *l, char *names)
{
count=0;
  while(*names != ',' && count != 2) {
     *l = *names; l++; names++; count++;
  }
  *l = 0;
  return l;
}

Is that good for get last?  also...i see that in the function prototypes, age has only char *names ... is there a special way i should use to getage?  Also for Last compare...do i just change getfirst to the getlast function i wrote?  is that all that needs to be done? (assuming similar for age also)....sorry for the dumb questions..very new at this
0
 
LVL 5

Expert Comment

by:Kocil
ID: 8040420
/* GetLast
 * Return last name from names
 */
char *GetLast(char *l, char *names)
{
 // skip the first name
 while(*names != ',') names++;

 // catch the last names
 while(*names != ',') {
    *l = *names; l++; names++;
 }
 *l = 0;
 return l;
}

For the GetAge, no second parameter because
we can return the integer directly
int GetAge(char *names)
{
 char age[20];
 
 // skip the first name
 while(*names != ',') names++;
 // skip the last name
 while(*names != ',') names++;

 // catch the age
 while(*names != ',') {
    *age = *names; age++; names++;
 }
 *age = 0;
 return atoi(age);
}


Then yes, use that the same way in the CompareLast and CompareAge.
But of course in the CompareAge you dont need strcmp but

if (age1 == age2) return 0;
if (age1 < age2) return -1;
return 1;

Cheers
0
 
LVL 5

Accepted Solution

by:
Kocil earned 300 total points
ID: 8040447

Ooops ... sorry, a liitle mistake

/* GetLast
* Return last name from names
*/
char *GetLast(char *l, char *names)
{
  // skip the first name
  // while(*names != ',') names++; This is wrong
  while(*names++ != ','); // this is OK
  // litle thing but make a difference :)

  ...
}

And for the age
// catch the age
// while(*names != ',') { WRONG, because not comma anymore
  while (*names != '\0') {
   *age = *names; age++; names++;
  }
  ...

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question