Thread: Sorting Arrays

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Sorting Arrays

    You all probably have no trouble with this but I do. I am sorting two dimensional arrays. Don't have any idea how to do this. Never done it before. But this is what I have.
    double temp2[][];
    long temp1;
    for(i = 0; i<emp_counter; i++)
    {
    for(j = 0; j<emp_counter; j++)
    {
    if(emp_id[j - 1] > emp_id[j])
    {
    temp1 = emp_id [j - 1];
    emp_id [j - 1] = emp_id [j];
    emp_id [j] = temp1;

    strcpy(temp2[j-1], last[j]);
    strcpy(last[j - 1], last[j]);
    strcpy(last[j], temp2[j-1]);
    }
    }
    Is this at all right. It's close. But I don't know what to declare temp2 as.
    Sorry if this is considered one of those "Annoying Questions."

  2. #2
    Just one more wrong move. -KEN-'s Avatar
    Join Date
    Aug 2001
    Posts
    3,227
    First of all, that's code for normal arrays, and second of all it's not entirely correct...well, it is but instead of strcpy you should just use an equals sign. ex: array[1] = array[2]. For multidimensional arrays just add another loop, our you could run an entire sort on array[j][] and another one on array[][j]. Either one works just fine.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I couldn't figure out whether this was a 2D array of doubles, a 2D array of chars, or a 1D array of strings.

    Besides, I'd use qsort (see previous discussions)
    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.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    sorting

    I guess I didn't explain myself very well. I am dealing with a 2D array for double, 2D array for char, char, long and intergers. I am not suppose to use qsort for some reason. I wouldn't be able to have to seperate codes for array[][j] and array[j][] because my teacher is totally into speed and short codes. Here is a part of the code again. Hopefully I can get a little help.
    int i, j;
    long temp1;
    double temp2[][];
    char temp3[][];
    char temp4;
    for(i = 0; i<emp_counter; i++)
    {
    for(j = 0; j<emp_counter; j++)
    {
    if(emp_id[j - 1] > emp_id[j])
    {
    temp1 = emp_id [j - 1];
    emp_id [j - 1] = emp_id [j];
    emp_id [j] = temp1;


    strcpy(temp3[j-1], last[j]);
    strcpy(last[j - 1], last[j]);
    strcpy(last[j], temp3[j-1]);


    strcpy(temp3[j-1], first[j]);
    strcpy(first[j - 1], first[j]);
    strcpy(first[j], temp3[j-1]);

    temp2 = wg_hr_py [j - 1][0];
    wg_hr_py [j - 1][0] = wg_hr_py [j][0];
    wg_hr_py [j][0] = temp2;

    temp2 = wg_hr_py [j - 1][1];
    wg_hr_py [j - 1][1] = wg_hr_py [j][1];
    wg_hr_py [j][1] = temp2;

    temp2 = wg_hr_py [j - 1][2];
    wg_hr_py [j - 1][2] = wg_hr_py [j][2];
    wg_hr_py [j][2] = temp2;

    strcpy(temp4[j-1], section[j]);
    strcpy(section[j - 1], section[j]);
    strcpy(section[j], temp4[j-1]);

    Would I be able to get away with my variables being declared like that? Confused and frustrated. I can't seem to get past this point.

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