Insert elements in an array in sorted order.
#include <stdio.h>
#include <conio.h>
void main ( )
{
int arr[50], n, i = 0, k = 0, j;
char c = 'y';
clrscr ( );
while ( c == 'y' )
{
printf("\n\n\t Enter a number..:"); scanf("%d",&n);
for ( j = 0; j < k; j++ )
{
if ( arr[j] > n )
{
break;
}
}
for ( i = k-1; i >= j; i-- )
{
arr[i+1] = arr[i];
}
arr[j] = n;
k++;
printf("\n\t Elements are..:");
for (i = 0; i < k; i++ )
{
printf("%d,",arr[i]);
}
printf("\n\n\t Do you want to add more elements?(y/n):");
c = getche ( );
}
}
Click here to contact us
#include <stdio.h>
#include <conio.h>
void main ( )
{
int arr[50], n, i = 0, k = 0, j;
char c = 'y';
clrscr ( );
while ( c == 'y' )
{
printf("\n\n\t Enter a number..:"); scanf("%d",&n);
for ( j = 0; j < k; j++ )
{
if ( arr[j] > n )
{
break;
}
}
for ( i = k-1; i >= j; i-- )
{
arr[i+1] = arr[i];
}
arr[j] = n;
k++;
printf("\n\t Elements are..:");
for (i = 0; i < k; i++ )
{
printf("%d,",arr[i]);
}
printf("\n\n\t Do you want to add more elements?(y/n):");
c = getche ( );
}
}
Click here to contact us
No comments:
Post a Comment