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;
        }
    }
}
11
  • 1
    Are you looking for the hash table data structure? Commented Jul 17, 2013 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. Commented Jul 17, 2013 at 19:44
  • 3
    Have you learned about structures yet? If so, you should probably using them. Commented Jul 17, 2013 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. Commented Jul 17, 2013 at 19:51
  • 1
    your empty #define STRSIZE looks weird. Commented Jul 17, 2013 at 19:57

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.