用于从已排序数组中删除元素的函数

时间:2013-04-28 07:17:23

标签: c arrays sorting shuffle

从排序数组中删除元素时遇到一些问题(必须删除元素的所有实例)。当我运行程序时,我遇到了分段错误。我不知道为什么会发生这种情况,因为函数remElem(int *,int *,int)其中1st arg是一个数组,2nd arg是一个数组的长度(当元素被删除时会改变)而3rd arg是一个元素要删除 - 在使用switch语句扩展程序之前正常工作,以及其他函数,如shuffle(int *,int)(用于控制数组的元素)和insElem(int *,int *,int)(用于插入元素)我不知道出了什么问题,请帮忙。我会提供注释的代码。评论中还提供了一些其他的小问题。提前谢谢:)

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

#define MAX_LEN 1000


void initArray(int*, int);
void printArray(int*, int);
void swap(int*, int*);
void insSort(int*, int);
void insElem(int*, int*, int);
void remElem(int*, int*, int);
void shuffle(int*, int);


int main() {
    int array[MAX_LEN];
    int len, elem, comm;

    srandom(time(NULL));

    printf("Number of elements? >> ");
    scanf("%d", &len);

    initArray(array, len);

    do {
        printf("Command? (6 for help) >> ");
        scanf("%d", &comm);
        switch (comm) {
            case 1:
                printArray(array, len);
                break;
            case 2:
                shuffle(array, len);
                break;
            case 3:
                insSort(array, len);
                break;
            case 4:
                printf("Insert element? >> ");
                scanf("%d", &elem);
                insElem(array, &len, elem);
                break;
            case 5:
                printf("Remove element? >> ");
                scanf("%d", &elem);
                remElem(array, &len, elem);
                break;
            case 6:
                printf("Help:\n");
                printf("1 - Print\n");
                printf("2 - Shuffle\n");
                printf("3 - Sort\n");
                printf("4 - Insert element\n");
                printf("5 - Remove element\n");
                printf("6 - Help (this screen)\n");
                printf("0 - Quit\n");
                break;
            case 0:
                break;
            default :
                printf("Wrong input! Repeat!\n");
        }
    } while (comm);

    return EXIT_SUCCESS;
}


/*Initializes array with n random numbers from 0 to 9 (including)*/
void initArray(int a[], int n) {
    int i;

    for (i = 0; i < n; i++)
        a[i] = random() % 10;
}


/*Prints n elements of an array*/
void printArray(int a[], int n) {
    int i;

    for (i = 0; i < n; i++)
        printf("%d ", a[i]);
    putchar('\n');
}


/*Swaps the values of two variables*/
void swap(int *i, int *j) {
    *i = *i + *j;
    *j = *i - *j;
    *i = *i - *j;
    /*I saved up some memory yaaaaaaay :3 */
    /*I spent some processing time nooooo :| */
    /*Which is better... with or without tmp variable???*/
    /*I suppose it depends on application... clearly, this
      method doesnt work with structures, for example*/
}


/*Sorts the elements of an array using insertion sort algorythm*/
void insSort(int a[], int n) {
    int i, j;

    for (i = 1; i < n; i ++)
        for (j = i; j > 0 && a[j] < a[j-1]; j--)
            swap(&a[j], &a[j-1]);
}


/*Inserts an element into a sorted array*/
/*Wassn meant to be working with unsorted arrays
  in that case unpreddictable results*/
void insElem(int a[], int *n, int e) {
    int i, j;

    (*n)++;
    for (i = 0; a[i] < e && a[i+1] < e; i++);

    for (j = *n; j > i + 1; j--)
        a[j] = a[j-1];

    a[i+1] = e;
}


/*Removes an element from a sorted array*/
/*Wassn meant to be working with unsorted arrays
  in that case unpreddictable results*/
void remElem(int a[], int *n, int e) {
    int i, j;

    for (i = 0; i < *n; i++) {
        while (a[i] == e) {
            for (j = i; j < *n; j++)
                a[j] = a[j+1];
        (*n)--;
        }
    }
}


/*Shuffles the elements of an array*/
/*I just did this on the fly...
  are there any known algorythms for doing this*/
void shuffle(int a[], int n) {
    int i;

    for (i = 0; i < n; i++)
        swap(&a[rand()%n], &a[rand()%n]);
}

2 个答案:

答案 0 :(得分:1)

以下内容可导致越界访问,从而导致未定义的行为:

        for (j = i; j < *n; j++)
            a[j] = a[j+1];

考虑a[j+1]j == (*n)-1会发生什么。

对于swap()函数,您看起来很聪明的实现存在整数溢出和未定义行为的风险。只需使用临时变量,让编译器担心效率。

答案 1 :(得分:1)

在这个循环中:

for (j = i; j < *n; j++)
  a[j] = a[j+1];

j = *n - 1时,您将访问数组边界之外的内存。这会调用未定义的行为,结果可能是任何内容,因此我相信它会解释您的崩溃。至于程序之前没有崩溃 - 这可以做任何事情也可能没有负面影响。