Thread: HELP! Array sorting hw

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    1

    HELP! Array sorting hw

    I am having trouble with my program. Here is the hw problem.

    Write a program that stores lists of names (the last name first) and ages in parallel arrays and sorts the names into alphabetical order keeping the ages with the correct names. The original arrays of names and ages should remain no changes. Therefore, you need to create an array of character pointers to store the addresses of the names in the name array initially. Apply the selection sort to this array of pointers so that the corresponding names are in alphabetical order. See the sample program of Fig.8.14 from pages 479 to 481 in the textbook. You should use another array of pointers to age array to make sure the age is corresponding to the correct name.

    I am using the code from the book (because the professor said that it'll make things way easy for us) I am just not understanding on how I can add a list for age while keeping it aligned with the names. I also can't seem to print the original list again after the alphabetical list without the program crashing or not including it at all. Please throw me some tips!
    Attached Files Attached Files

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Well a parallel array would be
    int age[MAXAPP];

    Fill the array with data, then add an extra array parameter to your sort functions.

    > See the sample program of Fig.8.14 from pages 479 to 481 in the textbook
    This book needs to be thrown away if it's still recommending this by page 500.
    gets(applicants[i]);

    gets() is NOT safe
    gets() can never be made safe.
    The latest C standard has made gets() obsolete.

    Read up on how to use fgets() as a replacement means of reading a line of input.
    fgets() wins additionally, because the same code can read from stdin or a file.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-04-2014, 09:51 AM
  2. Replies: 2
    Last Post: 04-18-2013, 02:21 PM
  3. Sorting an array
    By dxgalaxy in forum C Programming
    Replies: 11
    Last Post: 06-09-2011, 06:31 AM
  4. Adding nodes to an array at the top & array sorting
    By Wolf` in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2010, 12:48 PM
  5. Replies: 9
    Last Post: 04-07-2010, 10:03 PM