2

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). Any tips?

8
  • why do you subtract one from the requested array size? Commented Jun 11, 2020 at 18:10
  • printf("%d", *(arr++)); You increment the pointer first, then read the value. Commented Jun 11, 2020 at 18:11
  • For some reason, the program allows more than requested input size. So if i enter 5, it allows me to input 6. Commented Jun 11, 2020 at 18:11
  • Your program would be simplified using indexing instead of pointer arithmetic. Ex: arr[i] instead of *(arr+i) Commented Jun 11, 2020 at 18:12
  • Your loops might be clearer and easier to handle if you use a 'for' loop from zero, < size Commented Jun 11, 2020 at 18:13

1 Answer 1

3

I see that after taking the array_size as input, you are decrementing it by 1, which is not necessary. This is because inside all your functions, you are doing arrend = arr + size - 1 i.e., you are doing arrend = arr[size-1]. This works when size is actual size of array. Hence you need not decrement array_size inside the main function. 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;
            }
        }
    }       
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it works but for some reason it still allows size + 1 input even if the output is of specified size. It bugs me but yes the program works fine
Finally, I found it. You are doing: scanf("%d\n",arr++) . The \n in this line is the reason for extra input. Just do scanf("%d",arr++). The scanf("%d\n",arr++) is expecting a non empty input (non newline input ) at the end. It means that read the number till non-whitespace character appear(ignore all whitespace and '\n' after the number). So removing it fixes the issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.