Thread: Help with sorting arrays

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    3

    Unhappy Help with sorting arrays

    Hey, i have written a prgram with output as a summary/menu type thing. i need to sort the summary by room numbers. But i dont understand how to.

    Heres some of the code:

    Code:
     /*
    #include <stdio.h>     VARIABLES HERE
    #include <conio.h>
    #include <stdlib.h>
    
    #define SIZE 10
    
    void determine_vehicle_requirements (void);
    void vehicles (void);	//declare function prototypes
    void summary (void);
    void calculations (void);
    void final_summary (void);
    
    char name[SIZE][20];
    char destination[SIZE];
    int room_number[SIZE];	//declare input arrays
    int num_people[SIZE];
    int i=0;
    int choice;
    
    ------------------------------------------------------------------------------------
    
    final summary
    
    	printf("Room Number\tName\tBookings\tDestination\n");
    	printf("*********************************************************************\n");
    	for (i=0;i<=SIZE-1;i++){
    	printf("%d%17s%7d\t%11c\n", room_number[i], name[i], num_people[i], destination[i]);
    	}*/
    in the final summary, the for loop prints out all the contents of the arrays as i can understand.what i need to do is e.g. the room numbers entered were 9, 7, 5, 7, 2, 1, 5, 3, i need to sort then in ascending order. Not only that but, i need to be able to get all the other information about that room number to move as well. e.g. name, number of people, destination etc.

    Can anyone help?

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Search the boards or the web for sorting routines. You should come up with a bajillion hits. Use pointers to move corresponding data around, or use a struct room.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The first thing to do is define a structure to hold your data

    char name[SIZE][20];
    char destination[SIZE];
    int room_number[SIZE]; //declare input arrays
    int num_people[SIZE];

    Eg
    Code:
    struct data_st {
        char name[20];
        char destination;
        int room_number;
        int num_people;
    };
    
    struct data_st data[SIZE];
    This does two things
    1. it makes it a lot easier to keep all of the data together - when you copy room_number, then num_people etc get copied at the same time. With arrays, you have to do this all by yourself.

    2. Quick (to implement) sorts are bubble sorts (search the web). If you're up to it, there's a qsort() function already available in stdlib.h. qsort() can work with the data[SIZE] array above

  4. #4
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    just a stupid question, could a vector be used here.

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > could a vector be used here
    Only if the question was posted on the C++ board

  6. #6
    In The Light
    Join Date
    Oct 2001
    Posts
    598
    howdy,
    yea i was afraid you would say that, just goes to show my ignorance. sorry.

    M.R.
    I don't like you very much. Please post a lot less.
    Cheez
    *and then*
    No, I know you were joking. My point still stands.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Sorting Arrays
    By DaniiChris in forum C Programming
    Replies: 11
    Last Post: 08-03-2008, 08:23 PM
  3. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  4. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  5. sorting arrays
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-13-2001, 05:39 PM