Monday, 7 January 2013

Write a program that declares an integer array of 10 size, but the array can at most hold only 8 elements. User enters a number and you have to insert it in the array in such a way that the array remains in ascending order. The program should run until user terminates it.


#include<stdio.h>
#include<conio.h>
void main()
{
int ar[10],j,n,i,tmp;
printf(" Enter size of array \t");
 scanf("%d",&n);
printf("Now enter values in the array \t");
for(i=0;i<n;i++)
 {
 scanf("%d",&ar[i]);


}
printf("\n Array is ");

for(i=0;i<n;i++)
{
 printf("\t %d",ar[i]);}

     for(i=0;i<n;i++)

     {
 for(j=0;j<n-i;j++)
 {

if(ar[j]>ar[j+1])
{
tmp=ar[j];
ar[j]=ar[j+1];
ar[j+1]=tmp;
}
}

}
 printf("\n\n Array in the ascending order is \n");
for(i=0;i<n;i++)
{
printf("\t %d",ar[i]);
}
getch();

}

No comments:

Post a Comment