# Thread: Help with sorting arrays

1. ## 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. 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.

3. 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. howdy,
just a stupid question, could a vector be used here.

M.R.

5. > could a vector be used here
Only if the question was posted on the C++ board

6. howdy,
yea i was afraid you would say that, just goes to show my ignorance. sorry.

M.R.