in this program i am dynamically allocating space for an array and then sorting the numbers entered by the user this program run successfully when array size is small as i try to take larger array size it some how take some garbage value plz help me with this problem...
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define NULL 0
void main()
{
clrscr();
int *ptr,*i,j,n,t;
printf("enter the number\n :" );
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int));//dynamic memory allocation for a block of memory of size n to be entered by user
printf("input %d values\n",n);
for(i=ptr;i<ptr+n;i++)
{
scanf("%d",i);//inputting the values entered by the user into the block of dynamic memory of size n
}
if(ptr==NULL)//checking for availability of memory for dynamic allocation
{
printf("memory not sufficient");
exit(1);
}
else
{
for(j=1;j<=n-1;j++)//applying bubble sort algorithm
{
for(i=ptr;i<=ptr+(n-j);i++)
{
if(*i<=*(i+1))
{
t=*i;
*i=*(i+1);
*(i+1)=t;
}
else
{
continue;
}
}
}}
for(i=ptr;i<ptr+n;i++)// printing the sorted list
{
printf("%d\t",*i);
}
getch();
}