Given a user defined array of marks of students the task is to sort them in descending order using pointers.
Example:
Consider an array of size 5 containing marks of 5 students.
| 70 | 90 | 85 | 65 | 75 |
Arranged array.
| 90 | 85 | 75 | 70 | 65 |
C
#include <stdio.h>
int main()
{
int n=0,temp=0;
printf ("Enter the size of the array: ");
scanf ("%d",&n);
int marks[n];
int *p=marks;
printf ("Enter the marks of the students:-\n");
for (int i=0;i<n;i++)
{
scanf("%d",p);
p++;
}
for(int i=0;i<n-1;i++)
{
p=marks;
for(int j=0;j<n-1-i;j++)
{
if(*p<*(p+1))
{
temp=*p;
*p=*(p+1);
*(p+1)=temp;
}
p++;
}
}
p=marks;
printf("Marks\t\tRank\n");
for(int i=0;i<n;i++)
{
printf("%d\t\t%d\n",*p,(i+1));
p++;
}
return 0;
}C