Write a C Program to Sort n numbers in ascending order using pointers. Here’s simple Program to Sort n numbers in ascending order using pointers in C Programming Language.
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
type *var-name;
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
The unary or monadic operator & gives the “address of a variable’”.
The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.
Below is the source code for C Program to Sort n numbers in ascending order using pointers which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
/* C Program to Sort n numbers in ascending order using pointers */
#include <stdio.h>
#include <conio.h>
int main()
{
int n,*p,i,j,temp;
printf("How many numbers u want to Sort :: ");
scanf("%d",&n);
p=(int *) malloc(sizeof(int));
if(p==NULL)
{
printf("\nMemory Allocation unsuccessful.\n");
exit(0);
}
for(i=0;i<n;i++)
{
printf("\nEnter Number %d :: ",i+1);
scanf("%d",p+i);
}
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(*(p+i)<*(p+j))
{
temp=*(p+i);
*(p+i)=*(p+j);
*(p+j)=temp;
}
}
}
printf("\nThe Sorted Numbers are ::\n");
for(i=0;i<n;i++)
{
printf(" %d ",*(p+i));
}
return 0;
}
Output : :
/* C Program to Sort n numbers in ascending order using pointers */
How many numbers u want to Sort :: 7
Enter Number 1 :: 4
Enter Number 2 :: 1
Enter Number 3 :: 2
Enter Number 4 :: 5
Enter Number 5 :: 3
Enter Number 6 :: 6
Enter Number 7 :: 7
The Sorted Numbers are ::
1 2 3 4 5 6 7
Process returned 0
Above is the source code for C Program to Sort n numbers in ascending order using pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .
What are Pointers?
A pointer is a variable whose value is the address of another variable, i.e., direct address of the memory location. Like any variable or constant, you must declare a pointer before using it to store any variable address.
The general form of a pointer variable declaration is −
Here, type is the pointer’s base type; it must be a valid C data type and var-name is the name of the pointer variable.
The asterisk * used to declare a pointer is the same asterisk used for multiplication. However, in this statement the asterisk is being used to designate a variable as a pointer.
The unary or monadic operator & gives the “address of a variable’”.
The indirection or dereference operator * gives the “contents of an object pointed to by a pointer”.
Below is the source code for C Program to Sort n numbers in ascending order using pointers which is successfully compiled and run on Windows System to produce desired output as shown below :
SOURCE CODE : :
Output : :
Above is the source code for C Program to Sort n numbers in ascending order using pointers which is successfully compiled and run on Windows System.The Output of the program is shown above .
need an explanation for this answer? contact us directly to get an explanation for this answer