简体   繁体   English

使用指针对数组进行排序 - C 编程

[英]Sorting array using pointers - C programming

I have a problem with my code.我的代码有问题。 I'm trying to sort an array using pointers.我正在尝试使用指针对数组进行排序。 The problem I'm having is that when the program is sorting the array, it doesn't process the last input element.我遇到的问题是,当程序对数组进行排序时,它不会处理最后一个输入元素。 I'm not so comfortable using pointers as of yet.到目前为止,我还不太习惯使用指针。 I'd appreciate some feedback.我会很感激一些反馈。 Here's the code这是代码

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

void inputarray(int *arr, int size);
void printarray(int *arr, int size);
void sortascending(int *arr, int size);

int main()
{
int array_size, array[MAX],choice;
    printf("Enter size of array:\n");
    scanf("%d",&array_size);
    array_size -= 1;
    printf("Enter elements:\n");
    inputarray(array, array_size);
    printf("Sorting scending:\n");
    sortascending(array, array_size);

    printarray(array, array_size);

    return 0;
}


void inputarray(int *arr, int size)
{
    int *arrayend = arr + size - 1;
    while(arr <= arrayend)
    {
        scanf("%d\n",arr++);
    }

}

void printarray(int *arr, int size)
{
    int *arrayend = arr + size-1;
    while(arr <= arrayend)
    {
        printf("%d", *(arr++));
    }
}


void sortascending(int *arr, int size)
{
    int *arrayend = arr + size - 1; 
    int i,j,t;
    for(i=0; i< size; i++)
    {
        for(j=i+1; j< size; j++)
        {
            if(*(arr+j)<*(arr+i))
            {
                t = *(arr+i);
                *(arr+i) = *(arr+j);
                *(arr+j) = t;
            }
        }
    }       
}

So basically if I enter 5 elements in the order 9,8,7,6,5, it will return 6,7,8,9, neglecting the last input element (which is 5).所以基本上如果我按 9、8、7、6、5 的顺序输入 5 个元素,它将返回 6、7、8、9,忽略最后一个输入元素(即 5)。 Any tips?有小费吗?

I see that after taking the array_size as input, you are decrementing it by 1, which is not necessary.我看到在将 array_size 作为输入之后,您将其递减 1,这是不必要的。 This is because inside all your functions, you are doing arrend = arr + size - 1 ie, you are doing arrend = arr[size-1].这是因为在您的所有函数中,您正在执行 arrend = arr + size - 1 即,您正在执行 arrend = arr[size-1]。 This works when size is actual size of array.这在 size 是数组的实际大小时有效。 Hence you need not decrement array_size inside the main function.因此,您无需减少主 function 内的 array_size。 Working code:工作代码:

#include <stdio.h>
#include <stdlib.h>
#define MAX 100

void inputarray(int *arr, int size);
void printarray(int *arr, int size);
void sortascending(int *arr, int size);

int main()
{
int array_size, array[MAX],choice;
printf("Enter size of array:\n");
scanf("%d",&array_size);
//no need to decrement size here
printf("Enter elements:\n");
inputarray(array, array_size);
printf("Sorting scending:\n");
sortascending(array, array_size);

printarray(array, array_size);

return 0;
}


void inputarray(int *arr, int size)
{
    int *arrayend = arr + size - 1;
    while(arr <= arrayend)
    {
        scanf("%d",arr++);
        //remove \n from above line

    }

}

void printarray(int *arr, int size)
{
    int *arrayend = arr + size-1;
    while(arr <= arrayend)
    {
        printf("%d", *(arr++));
    }
}


void sortascending(int *arr, int size)
{
    int *arrayend = arr + size - 1; 
    int i,j,t;
    for(i=0; i< size; i++)
    {
        for(j=i+1; j< size; j++)
        {
            if(*(arr+j)<*(arr+i))
            {
                t = *(arr+i);
                *(arr+i) = *(arr+j);
                *(arr+j) = t;
            }
        }
    }       
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM