# Finding a larger number using a dynamic memory allocation

Writing a program to find largest number using dynamic memory allocation. I have
written the following code, there is some problem in
sorting part I am doing selectin sorting by pointers . but sorting is not not done properly .....can we do selection sort by pointers am I applying right approach?

``````         #include<stdio.h>
#include<stdlib.h>
main()
{

int i,j,n,temp;
int *data;
printf("enter total no of elements:");
scanf("%d",&n);
data=(int*)calloc(n,sizeof(int));
printf("\n");
for(i=0;i<n;i++)
{
printf("enter number %d:",i+1);
scanf("%d",data+i);
}
for(i=0;i<n;i++)
{
printf("%d\t",*(data+i));
}
printf("\n");
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(*(data)>*(data+j))
{
temp=*(data);
*(data)=*(data+j);
*(data+j)=temp;
}
}

}
for(i=0;i<n;i++)
{
printf("%d\t",*(data+i));
}
printf("largest element is %d\t",*(data+n-1));
}
```
```

output:

``````         enter total no of elements:5

enter number 1:2
enter number 2:10
enter number 3:12
enter number 4:1
enter number 5:0
2  10  12  1   0
0  10  12  2
```
```

You need little modification in your code

``````   for(i=0;i<n-1;i++) //change here
{
for(j=i+1;j<n;j++)
{
if(*(data+i)>*(data+j)) //here
{
temp=*(data+i);      //here
*(data+i)=*(data+j); //here
*(data+j)=temp;
}
}
```
```

You are always comparing first element(`*data`) with `*(data+j)`. You need to compare `*(data+i)` with `*(data+j)`