1

I'm writing a program to alphabetize inputted names and ages. The ages are inputted separately, so I know I need to use an array of pointers to tie the ages to the array of names but I can't quite figure out how to go about doing it. Any ideas?

So far, my program only alphabetizes the names.

/* program to alphabetize a list of inputted names and ages */

#include <stdio.h>
#define MAXPEOPLE 50
#define STRSIZE 

int alpha_first(char *list[], int min_sub, int max_sub);
void sort_str(char *list[], int n);

int main(void)
{
    char people[MAXPEOPLE][STRSIZE];
    char *alpha[MAXPEOPLE];
    int num_people, i;
    char one_char;

    printf("Enter number of people (0...%d)\n> ", MAXPEOPLE);
    scanf("%d", &num_people);

    do
        scanf("%c", &one_char);
    while (one_char != '\n');

    printf("Enter name %d (lastname, firstname): ", );
    printf("Enter age %d: ", );
    for (i = 0; i < num_people; ++i)
        gets(people[i]);

    for (i = 0; i < num_people; ++i)
        alpha[i] = people[i];
    sort_str(alpha, num_people);

    printf("\n\n%-30s5c%-30s\n\n", "Original List", ' ', "Alphabetized List");

    for (i = 0; i < num_people; ++i)
        printf("-30s%5c%-30s\n", people[i], ' ', alpha[i]);

    return(0);
}

int alpha_first(char *list[], int min_sub, int max_sub)
{
    int first, i;

    first = min_sub;
    for (i = min_sub + 1; i <= max_sub; ++i)
        if (strcmp(list[i], list[first]) < 0)
            first = i;

    return (first);
}

void sort_str(char *list[], int n)
{
    int fill, index_of_min;
    char *temp;

    for (fill = 0; fill < n - 1; ++fill){
        index_of_min = alpha_first(list, fill, n - 1);

        if(index_of_min != fill){
            temp = list[index_of_min];
            list[index_of_min] = list[fill];
            list[fill] = temp;
        }
    }
}
Mariano
  • 1,357
  • 3
  • 17
  • 30
  • 1
    Are you looking for the [hash table](http://en.wikipedia.org/wiki/Hash_table) data structure? –  Jul 17 '13 at 19:41
  • 2
    Hey, this is a lot of code, is there any pieces of this that can be omitted? I'm much more willing to help when I don't have to try to read 50 lines of uncommented C. – daniel gratzer Jul 17 '13 at 19:44
  • @H2CO3 That looks like it would work, but this is for a class and we have not learned about hash tables yet. My professor instructed us to "create an array of character pointers to store the addresses of the names in the name array initially" and then "use another array of pointers to age array to make sure the age is corresponding to the correct name". – wings of maybe Jul 17 '13 at 19:49
  • 3
    Have you learned about structures yet? If so, you should probably using them. – Jonathan Leffler Jul 17 '13 at 19:50
  • 1
    The other (poorer) alternative to structs is to just sort your list of ages at the same time, and in the same way, that you sort your list of names. Please learn how to indent your code, too. – Crowman Jul 17 '13 at 19:51
  • @wingsofmaybe Your prof is wrong and he should feel bad - listen to the advice of Jonathan Leffler. –  Jul 17 '13 at 19:55
  • 1
    your empty `#define STRSIZE` looks weird. – qehgt Jul 17 '13 at 19:57
  • @Jonathan Leffler We have not learned about structures yet. I'm really unsure how to move forward on this program right now. The only resource available to us is our textbook and it really doesn't explain things very well. – wings of maybe Jul 17 '13 at 20:05
  • @wingsofmaybe: But you said he's already told you how to do it: "use another array of pointers to age array to make sure the age is corresponding to the correct name". – Crowman Jul 17 '13 at 20:11
  • OK. Your input loop is using `gets()`; don't! (Use `fgets()` instead.) Your input loop is currently not separating names from ages, so you should be seeing ages sorted at the same time as names (though if John Smith 33 and John Smith 9 — his son — are in the data, you won't see the names sorted by name then age). You aren't error checking (EOF checking) your inputs (`scanf()`, `fgets()`; that could lead to problems if you miscount. Making humans count is not nice; computers are good at it. You shouldn't really prompt for 'number of people'; just read until out of file or out of space. – Jonathan Leffler Jul 17 '13 at 20:27
  • Or, if you think you're inputting names only, you aren't inputting the ages at all...this is where sample data (5 lines of data, for example) would help. (Note that `gets()` strips newlines but `fgets()` does not. See [Safe alternative to `gets()`](http://stackoverflow.com/questions/4309746/safe-alternative-to-gets/) for more information.) – Jonathan Leffler Jul 17 '13 at 20:37

2 Answers2

1

Most of your printfs are syntax errors, as in

printf("Enter name %d (lastname, firstname): ", );
printf("Enter age %d: ", );

or bomb right away, since you pass an int (' ') as a pointer:

printf("\n\n%-30s5c%-30s\n\n", "Original List", ' ', "Alphabetized List");

As a first step, get all your %s right and show us what you really compiled, not some random garbage. And crank your compiler's warning level to the maximum, you need it! What is

#define STRSIZE

supposed to mean when STRSIZE is used as an arary dimension? You have a serious cut&paste problem, it would appear.

Jens
  • 69,818
  • 15
  • 125
  • 179
0

Creating a struct would probably be easier: i.e

struct person {
   char name[STRSIZE];
   int age;
}

Otherwise, if you must do it the way you're trying, just create an extra array of the indexes. When you move a name, you also move the index on the array... when you're done sorting names, just sort the ages to match the indexes.

aiguofer
  • 1,887
  • 20
  • 34