using codeblocks
---------------------------------------------------------------------------------------
//program to sort numbers using pointer //comment of program
#include <stdio.h> //header file
int main() //main function
{
int *p; //pointer declaration
int a[20],i,j,k; //variable declaration
int total;
printf("enter total number\n");
scanf("%d",&total); // gets total value of array.
p=a; //stores base address
for(i=0;i<total;i++) //runs from 0 to one less than total
{
scanf("%d",p+i); //gets value for given address. This input goes upto last value of address
}
printf("before sorting,values are\n");
for(i=0;i<total;i++)
{
printf("%d\n",*(p+i));//displays its value with the help of address before sorting
}
printf("after sorting,values are\n");
for(i=0;i<total;i++)
{
for(j=i+1;j<total;j++)
{
if(*(p+i)>*(p+j)) //sorting swapping taking place with its value
{
k=*(p+i);
*(p+i)=*(p+j);
*(p+j)=k;
}
}
}
for(i=0;i<total;i++) //value display in sorted form
{
printf("%d\n",*(p+i));
}
return 0;
}
---------------------------------------------------------------------------------------
//program to sort numbers using pointer //comment of program
#include <stdio.h> //header file
int main() //main function
{
int *p; //pointer declaration
int a[20],i,j,k; //variable declaration
int total;
printf("enter total number\n");
scanf("%d",&total); // gets total value of array.
p=a; //stores base address
for(i=0;i<total;i++) //runs from 0 to one less than total
{
scanf("%d",p+i); //gets value for given address. This input goes upto last value of address
}
printf("before sorting,values are\n");
for(i=0;i<total;i++)
{
printf("%d\n",*(p+i));//displays its value with the help of address before sorting
}
printf("after sorting,values are\n");
for(i=0;i<total;i++)
{
for(j=i+1;j<total;j++)
{
if(*(p+i)>*(p+j)) //sorting swapping taking place with its value
{
k=*(p+i);
*(p+i)=*(p+j);
*(p+j)=k;
}
}
}
for(i=0;i<total;i++) //value display in sorted form
{
printf("%d\n",*(p+i));
}
return 0;
}
----------------------------------------------------------------------------------------------------------
using turbo c++
------------------------------------------------------------------------------------------------------
//program to sort numbers using pointer //comment of program
#include <stdio.h> //header file
#include<conio.h>
int main() //main function
{
int *p; //pointer declaration
int a[20],i,j,k; //variable declaration
int total;
printf("enter total number\n");
scanf("%d",&total); // gets total value of array.
p=a; //stores base address
for(i=0;i<total;i++) //runs from 0 to one less than total
{
scanf("%d",p+i); //gets value for given address. This input goes upto last value of address
}
printf("before sorting,values are\n");
for(i=0;i<total;i++)
{
printf("%d\n",*(p+i));//displays its value with the help of address before sorting
}
printf("after sorting,values are\n");
for(i=0;i<total;i++)
{
for(j=i+1;j<total;j++)
{
if(*(p+i)>*(p+j)) //sorting swapping taking place with its value
{
k=*(p+i);
*(p+i)=*(p+j);
*(p+j)=k;
}
}
}
for(i=0;i<total;i++) //value display in sorted form
{
printf("%d\n",*(p+i));
}
getch();
return 0;
}
No comments:
Post a Comment