通过排序函数传递字符串数组

时间:2014-12-13 07:11:38

标签: c arrays sorting

我正在尝试在C编程中使用排序数组。我有三个数组,arr1,arr2,arr3,它们一起用来做这个:

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

我想对这些数组进行排序,使它们从最小到最大基于第一个数组,arr1。

到目前为止,我有一个正确排序前两列的函数。但是,我不知道如何对第三列字符串进行排序。到目前为止,这是我的代码:

#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;
}

这是输出:

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

显然第三栏做错了。我真的不确定如何将字符串数组传递给函数,并让函数对它进行排序,就像它对前两列一样。任何帮助将不胜感激

4 个答案:

答案 0 :(得分:1)

这将解决您的问题。更改函数定义参数和代码,如下所示。

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);
}

您可以在Sorting Multiple Arrays

查看正在运行的示例

答案 1 :(得分:0)

我认为你基本上是交换字符而不是交换字符串。

sortArray函数应该是这样的:void sortArray(float a[], float b[], char c[][], int size),你需要在sortArray函数中逐个交换char才能交换整个字符串。

答案 2 :(得分:0)

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

传递arr3的第一个元素,即char[4]并将衰减传递给char*

因此,对arr3的第一个元素的元素进行了排序(从temp3显而易见char而不是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;

这只能通过(坏)运气,因为三个数组的大小接近arr3元素的大小。

修复此传递arr3(不是其第一个元素),并正确键入临时变量temp3以交换arr3的元素(char[4]而不是{{ 1}})。最后选择正确的方法来“加载”和“保存”char

答案 3 :(得分:0)

最好在这个上使用一个结构。因为如果你把它作为结构来操作,arr2和arr3将跟随/附加到arr1作为联合,因此更容易编码。

#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;
}