我需要从用户数组中删除重复的数字。
我想使用像RemoveDup这样的功能。
我犯了一个错误:“表达式必须具有指向对象类型的指针”
我查看了较早的版本,并没有成功解决问题。你能帮我一下吗?

#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable: 4996)

void sortAsending(int arr[], int size);
void swap(int* a, int* b);
void RemoveDup(int, int);

void main()
{

    int arr[10];
    int i, size ;

    printf("please enter size of array: ");
    scanf("%d", &size);

    printf("please enter %d numbers: ", size);
    for (i = 0; i < size; i++)
        scanf("%d", &arr[i]);
    printf("the original array is: ");
    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");

    sortAsending(arr, size);
    printf("the sorted array is: ");

    for (i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
void swap(int* a, int* b)
{
    int temp = *a;
        *a = *b;
        *b = temp;
}

void sortAsending(int arr[], int size)
{
    int i, j;
    for (i = size - 1; i > 0; i--)
    {
        for (j = 0; j < i; j++)
        {
            if (arr[j]>arr[j + 1])
                swap(&arr[j], &arr[j + 1]);
        }
    }
}
void RemoveDup(int arr, int size)
{
    int arr[size];
    int i, j, k;
    for (i = 0; i < size; i++)
    {
        for (j = i + 1; j < size;)
        {
            if (arr[i] == arr[j])
            {
                for (k = j; k < size; k++)
                    arr[k] = arr[k++];
                size--;
            }
            else
                j++;
        }
    }
}

最佳答案

//prototype
void RemoveDup(int arr[], int *size);

//Assumes that it is sorted
//call RemoveDup(arr, &size);//size is updated.
void RemoveDup(int arr[], int *size){
    int *d, *s, *endp = arr + *size;
    if(*size == 1)
        return;
    d = s = arr;
    while(++s != endp){
        if(*d != *s){
            *++d = *s;
        }
    }
    *size = d - arr + 1;
}

关于c - 如何删除重复的数字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27838539/

10-11 20:54