0

I am trying to use a sorting array in C programming. I have three arrays, arr1, arr2, arr3, that are used together to make this:

arr1: arr2: arr3:

4534  97.5  m4W
4554  97.4  m5W
4574  97.6  m6W
3934  97.1  m1W
4054  97.2  m2W
4174  97.3  m3W

I want to sort these arrays so that they are in order from least to greatest based on the first array, arr1.

So far, I have a function that sorts the first two columns correctly. However, I'm not sure how to go about sorting the third column of strings as well. Here is my code so far:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void sortArray(float a[], float b[], char c[], int size){
    int i, swap;
    float temp1, temp2;
    char temp3;
    do{
        swap = 0;
        for (i = 0; i < size - 1; i++){//basic sorting for loop
            if (a[i]>a[i + 1]){
                swap = 1;
                temp1 = a[i];    //temporarily stores value of array cell
                temp2 = b[i];
                temp3 = c[i];
                a[i] = a[i + 1]; //swaps the cells
                b[i] = b[i + 1];
                c[i] = c[i + 1];
                a[i + 1] = temp1;//stores value in swapped cell
                b[i + 1] = temp2;
                c[i + 1] = temp3;
            }
        }
    } while (swap);

}

int main()
{
    float arr1[6] = { 4534, 4554, 4574, 3934, 4054, 4174 };
    float arr2[6] = { 97.5, 97.4, 97.6, 97.1, 97.2, 97.3 };
    char arr3[6][4] = { "m4w", "m5w", "m6w", "m1w", "m2w", "m3w" };

    printf("Arrays before sorting:\n");
    for (int i = 0; i != 6; i++)
    {
        printf("%f ", arr1[i]);
        printf("%f ", arr2[i]);
        printf("%s\n", arr3[i]);
    }

    sortArray(arr1, arr2, *arr3, 6); ///this is where the sorting function is used

    printf("\n\nArrays after sorting:\n");

    for (int i = 0; i != 6; i++)
    {
        printf("%f ", arr1[i]);
        printf("%f ", arr2[i]);
        printf("%s\n", arr3[i]);
    }


    system("pause");
    return 0;
}

This is the output:

Arrays before sorting:
4534.0  97.5    m4w
4554.0  97.4    m5w
4574.0  97.6    m6w
3934.0  97.1    m1w
4054.0  97.2    m2w
4174.0  97.3    m3w


Arrays after sorting:
3934.0  97.1
4054.0  97.2    4ww
4174.0  97.3    m6w
4534.0  97.5    m1w
4554.0  97.4    m2w
4574.0  97.6    m3w

Clearly the third column is done wrong. I'm really not sure how to pass the array of strings into the function and have the function sort it like it did with the first two columns. Any help would be appreciated

1
  • sortArray() accesses array of characters incorrectly. You probably want c[i*4+0], c[i*4+1], c[i*4+2] Commented Dec 13, 2014 at 7:18

4 Answers 4

1

This will solve your problem. Change function definition argument and code like below.

void sortArray(float a[], float b[], char c[6][4], int size){
    int i, swap;
    float temp1, temp2;
    char temp3[4];
    int k = 0;
    do{
        swap = 0;
        for (i = 0; i < size - 1; i++){//basic sorting for loop
            if (a[i]>a[i + 1]){
                swap = 1;
                temp1 = a[i];    //temporarily stores value of array cell
                temp2 = b[i];
                for ( k=0; k < 4 ; k++ ) { //Copying the c[i] to temp
                   temp3[k] = c[i][k];
                }
                a[i] = a[i + 1]; //swaps the cells
                b[i] = b[i + 1];
                for ( k=0; k < 4 ; k++ ) { //Copying the c[i+1] to c[i]
                    c[i][k] = c[i+1][k];
                }
                a[i + 1] = temp1;//stores value in swapped cell
                b[i + 1] = temp2;
                for ( k=0; k< 4 ; k++ ) { //Copying the temp to c[i+1]
                   c[i+1][k] = (char)temp3[k];
                }
            }
        }
    } while (swap);
}

You can check the running example at Sorting Multiple Arrays

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

1 Comment

@Basilevs:: char arr3[6][4] is similar to two dimensional array right? What wrong in it. Can you also please let me know the reason for down vote?
0

I think you are basically swapping char instead of swapping string.

sortArrayfunction should like this: void sortArray(float a[], float b[], char c[][], int size), you need swap char one by one in sortArray function in order to swap the whole string.

1 Comment

Try char c[][4]? Current declaration won't compile.
0

This

    sortArray(arr1, arr2, *arr3, 6); ///this is where the sorting function is used

passes the 1st element of arr3, which is char[4] and being passed decays to a char*.

So the sorting is done on the elements of the 1st element of arr3 (which is obvious from temp3 being a char not char[4]):

void sortArray(float a[], float b[], char c[], int size){
  int i, swap;
  float temp1, temp2;
  char temp3; 
  do{
    swap = 0;
    for (i = 0; i < size - 1; i++){//basic sorting for loop
        if (a[i]>a[i + 1]){
            [...]
            temp3 = c[i];
            [...]
            c[i] = c[i + 1];
            [...]
            c[i + 1] = temp3;

This only work by (bad) luck as the sizes of the three arrays are near the size of arr3's elements.

To fix this pass arr3 (not its 1st element), and correctly type the temporay variable temp3 for swapping arr3's elements (char[4] instead of char) . And finally choose the correct method to "load" and "save" temp3.

Comments

0

It is better to use a structure on this one. Because if you operate it as a struct, arr2 and arr3 will follow/attached to the arr1 as an union, therefore easier to code.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct data
{
    int arr1;
    float arr2;
    char arr3[3];
};

struct data list[4];
struct data temp;



int main()
{
    list[0].arr1=4534;
    list[0].arr2=97.5;
    strcpy(list[0].arr3,"m4W");
    list[1].arr1=4554;
    list[1].arr2=97.4;
    strcpy(list[1].arr3,"m5W");
    list[2].arr1=4574;
    list[2].arr2=97.6;
    strcpy(list[2].arr3,"m6W");
    list[3].arr1=3934;
    list[3].arr2=97.1;
    strcpy(list[3].arr3,"m1W");

    //sorting
    int i=0;
    int j=0;
    while(i<3)
    {
        j=0;
        while(j<3-i)
        {
            if(list[j].arr1>list[j+1].arr1)
            {
                temp=list[j];
                list[j]=list[j+1];
                list[j+1]=temp;
            }
            j++;
        }
        i++;
    }

    //for printing the struct
    i=0;
    while(i<4)
    {
        printf("%i %.2f %s\n",list[i].arr1,list[i].arr2,list[i].arr3);
        i++;
    }
    return 0;
}

1 Comment

Thanks, I was thinking about using structs as well. However, this little excerpt was part of a much larger program I was working on and I had already done most of it using arrays so it would take some work to modify it using structs. I might end up doing it if I have the time.

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.