I written a sorting to an ascending order program using pointers in C.Logically this program works an ascending order,but output is descending order.What happened to this program.
This is my code :
#include<stdio.h>
#include<stdlib.h>
main()
{
int n,i,j,t,*ptr,sum=0;
printf("Enter number of elements to sort : ");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));
if(ptr==NULL)
{ printf("Error occurred memory not allocated \n"); exit(0); }
printf("Enter the elements of Array \n");
for(i=0;i<n;i++)
{
scanf("%d",ptr+i);
}
for(i=0;i<n;i++){
for(j=0;j<n;j++)
{
if( *(ptr+i) > *(ptr+j))
{
t = *(ptr+i);
*(ptr+i) = *(ptr+j);
*(ptr+j) = t;
}
}
}
for(i=0;i<n;i++)
{
printf("\n%d\n",*(ptr+i));
}
free(ptr);
}
*(ptr+i)is idiomatically writtenptr[i].