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;
}