我在这种练习中遇到麻烦。我再次..此错误分段错误(核心转储)。有什么建议吗?

问题描述 投票:0回答:1

我创建了一个(int *)表,该表从用户N个元素获取,然后借助我创建的交换和排序功能打印出排序表。下面的代码:

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

void swap(int **p_a, int **p_b);
void sort(int **table, int N);

int main(void){
    // Here your code !

    int *table;
    int elements, i;

    printf("Input the number of elements to store in the array : ");
    scanf("%d", &elements);

    table = (int *)malloc(elements * sizeof(int));
    if (table == NULL)
    {
        printf("Unable to allocate memory...");
        return -1;
    }

    printf("Input %d number of elements in the array : \n", elements);
    for (i=0; i<elements; i++)
    {
        printf("element - %d: ", i+1);
        scanf("%d", (table + i));
    }

    printf("\nThe elements in the array before sorting: \n");
    for (i=0; i<elements; i++)
        printf("element - %d : %d\n", i+1, *(table + i));


    sort(&table, elements);


    printf("\nThe elements in the array after sorting: \n");
    for (i=0; i<elements; i++)
        printf("element - %d : %d\n", i+1, table[i]);


    free(table);
} 

void swap(int **p_a, int **p_b)
{
    int temp;

    temp = **p_a;

    **p_a = **p_b;
    **p_b = temp;
}

void sort(int **table, int N)
{
    int i,j;
    int current_position;
    for (i=0; i<N; i++)
    {
        current_position = i; // current_position is the current element of the table
        for (j=i; j<N; j++)
        {
            /* 
                compares all the elements after the current position
                if the condition is true then the current position now is the next position, and compares it again.
                When the outer loop will loop again it means that the smallest element was found and then compares the other ones
            */
            if (*table[j] < *table[current_position]); 
                current_position = j;
        }

        swap(&table[current_position], &table[j]);
    }
}

控制台:

Input the number of elements to store in the array : 5
Input 5 number of elements in the array : 
element - 1: 5
element - 2: 99
element - 3: 22
element - 4: 1
element - 5: 0

The elements in the array before sorting: 
element - 1 : 5
element - 2 : 99
element - 3 : 22
element - 4 : 1
element - 5 : 0

错误:

Segmentation fault (core dumped)

任何建议?我的函数结构正确吗,函数参数正确吗?。对于排序中的代码,我100%确信是正确的。

c function sorting malloc
1个回答
0
投票

sortswap的声明中有太多的间接级别。您的声明应为:

void swap(int *p_a, int *p_b);
void sort(int *table, int N);

您的swap可以简单地是:

void swap(int *p_a, int *p_b)
{
    int temp;

    temp = *p_a;

    *p_a = *p_b;
    *p_b = temp;
}

您的sort似乎是冒泡和插入排序的混合。选择一个或另一个。稍加修改,天真的冒泡排序将是:

void sort(int *table, int N)
{
    int i,j;

    for (i=0; i<N; i++)
    {
        for (j=i; j<N; j++)
        {
            if (table[j] < table[i])
                swap(&table[i], &table[j]);
        }
    }
}

';'语句的末尾纠正了放错位置的if,然后将其调用更改为sort(table, elements);后,您会收到:

示例使用/输出

$ ./bin/sortandswap
Input the number of elements to store in the array : 5
Input 5 number of elements in the array :
element - 1: 5
element - 2: 99
element - 3: 22
element - 4: 1
element - 5: 0

The elements in the array before sorting:
element - 1 : 5
element - 2 : 99
element - 3 : 22
element - 4 : 1
element - 5 : 0

The elements in the array after sorting:
element - 1 : 0
element - 2 : 1
element - 3 : 5
element - 4 : 22
element - 5 : 99
© www.soinside.com 2019 - 2024. All rights reserved.