Thread: Array Sorting with pointers in C

  1. #1
    Registered User
    Join Date
    Oct 2021
    Posts
    1

    Array Sorting with pointers in C

    Dear all,

    Hi.

    I am new to this coding field. In this code, I am trying to do array sorting using pointers. However, I am fairly sure that I am missing something while incrementing the pointers. Any guidance would be appreciated.

    Please find the attached file for the code.
    Code:
    /*
    Write a program using pointers to sort an array in ascending and/or descending order.
    */
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #define rng 1000
    
    int sort_ascending(int a[],int size,int *loc_ptr);
    int sort_descending(int b[],int size,int *loc_ptr);
    
    int main (void)
    {
        int input_selection, sort_selection, n,i;
    
        //Program initialisation and input method selection
        printf("\n\tThis program sorts and displays the array in ascending/descending order as per user selection.");
        printf("\n\tThe array elements can either be through an RNG or be user fed.");
        printf("\n\n\tPress 1 for RNG fed array.\n\tPress 2 for user fed values.");
        printf("\n\tSelection : ");
        scanf("%d",&input_selection);
        if (((input_selection==1)||(input_selection==2))==0)
        {
            printf("\n\tPlease make a valid selection next time. Ending program execution.\n");
            exit(0);
        }
    
        //Sort Selection
        printf("\n\tPress 1 for sorting array in ascending order.");
        printf("\n\tPress 2 for sorting array in descending order.");
        printf("\n\tPress 3 for sorting array in both ascending and descending order.");
        printf("\n\tEnter the sort selection : ");
        scanf("%d",&sort_selection);
        if (((sort_selection==1)||(sort_selection==2)||(sort_selection==3))==0)
        {
            printf("\n\tPlease make a valid selection next time. Ending program execution.\n");
            exit(0);
        }
    
        //Array specs
        printf("\n\tEnter the size of the array : ");
        scanf("%d",&n);
    
        int array_1[n],array_2[n];
    
        if(input_selection==1)
        {
            printf("\n\tRandom numbers will be generated between 1 and %d.",rng);
            srand(time(NULL));
            for(i=0;i<n;i++)
            {
                array_1[i]=1+rand()%rng;
                array_2[i]=array_1[i];
            }
        }
        else
        {
            printf("\n\tPlease enter the array elements.\n");
            for(i=0;i<n;i++)
            {
                printf("\tArray[%d] : ",i);
                scanf("%d",&array_1[i]);
                array_2[i]=array_1[i];
            }
        }
    
        //Print the array
        printf("\n\tThe array is : \n");
        for(i=0;i<n;i++)
        {
            printf("\t%d",array_1[i]);
            if((i+1)%10==0)
                printf("\n");
        }
    
        //Array Sorting and printing
        printf("\n\n\tThe array after sorting is : ");
        if(sort_selection==1)
        {
            sort_ascending(array_1,n,&array_1[0]);
            printf("\n\n\tAscending order : ");
            for(i=0;i<n;i++)
            {
                printf("\t%d",array_1[i]);
                if((i+1)%10==0)
                    printf("\n");
            }
        }
        else if (sort_selection==2)
        {
            sort_descending(array_2,n,&array_2[0]);
            printf("\n\n\tDescending order : ");
            for(i=0;i<n;i++)
            {
                printf("\t%d",array_2[i]);
                if((i+1)%10==0)
                    printf("\n");
            }
        }
        else
        {
            sort_ascending(array_1,n,&array_1[0]);
            sort_descending(array_2,n,&array_2[0]);
    
            printf("\n\n\tAscending order : ");
            for(i=0;i<n;i++)
            {
                printf("\t%d",array_1[i]);
                if((i+1)%10==0)
                    printf("\n");
            }
            printf("\n\n\tDescending order : ");
            for(i=0;i<n;i++)
            {
                printf("\t%d",array_2[i]);
                if((i+1)%10==0)
                    printf("\n");
    
            }
        }
    
        printf("\n\n");
    }
    
    
    int sort_ascending(int a[],int size,int *loc_ptr)
    {
        int x,temp,pass;
    
        for(pass=0;x<size-1;pass++)
        {
            for(x=0;x<size-1;x++)
            {
                if(*(loc_ptr+pass)>*(loc_ptr+x))
                    {
                        temp=*(loc_ptr+pass);
                        *(loc_ptr+pass)=*(loc_ptr+x);
                        *(loc_ptr+x)=temp;
                    }
            }
    
        }
        return 0;
    }
    
    
    int sort_descending(int b[],int size,int *loc_ptr)
    {
        int y,temp=0,pass;
    
        for(pass=0;y<size;pass++)
        {
            for(y=0;y<size;y++)
            {
                if(*(loc_ptr+pass)<*(loc_ptr+y))
                    {
                        temp=*(loc_ptr+pass);
                        *(loc_ptr+pass)=*(loc_ptr+y);
                        *(loc_ptr+y)=temp;
                    }
            }
    
        }
        return 0;
    }
    Attached Files Attached Files
    Last edited by Salem; 10-22-2021 at 06:14 AM. Reason: Inlined the code

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    First step, add some debug print (or use a debugger) to get some visual indication of what's going on.
    Code:
    int sort_ascending(int a[],int size,int *loc_ptr)
    {
        int x,temp,pass;
     
        for(pass=0;x<size-1;pass++)
        {
            printf("DEBUG: pass %d\n",pass);
            for(x=0;x<size-1;x++)
            {
                printf("DEBUG: x %d\n",x);
                if(*(loc_ptr+pass)>*(loc_ptr+x))
                    {
                        temp=*(loc_ptr+pass);
                        *(loc_ptr+pass)=*(loc_ptr+x);
                        *(loc_ptr+x)=temp;
                        printf("DEBUG: swapped between %d and %d\n", pass, x);
                    }
            }
     
        }
        return 0;
    }
    Produces for example.
    Enter the size of the array : 5

    Random numbers will be generated between 1 and 1000.
    The array is :
    719 314 518 935 231

    The array after sorting is : DEBUG: pass 0
    DEBUG: x 0
    DEBUG: x 1
    DEBUG: swapped between 0 and 1
    DEBUG: x 2
    DEBUG: x 3



    Ascending order : 314 719 518 935 231

    Your outer loop only runs once, so that's a good place to start.

    You'll kick yourself when you see it
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 08-05-2012, 10:16 PM
  2. Replies: 3
    Last Post: 04-04-2012, 01:19 PM
  3. Replies: 5
    Last Post: 05-26-2011, 06:44 AM
  4. sorting with pointer of pointers to array
    By dunpealslyr in forum C++ Programming
    Replies: 6
    Last Post: 10-01-2007, 11:26 PM
  5. Replies: 2
    Last Post: 03-07-2002, 10:14 AM

Tags for this Thread