Beruflich Dokumente
Kultur Dokumente
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
int array[10], i=0, j=0, max, min, num=0, a=0, b=0, temp, found=0;
for(i=0;;)
{
printf("Enter any number (0-9999): ");
scanf("%d", &num);
if((num>9999)||(num<0))
{
printf("\n\n\n\tINVALID INPUT");
getch();
clrscr();
j=0;
}
else
{
array[j]=num;
j++;
}
if(j==10)
break;
}
printf("\nInput Complete.");
for(i=0; i<=9; i++)
printf("\narray[%d] = %d", i, array[i]);
printf("\nPress any key to sort the array.");
getch();
clrscr();
for(a=0; a<9; a++)
for(b=a; b<=9;b++)
if(array[b]<array[a])
{
temp=array[b];
array[b]=array[a];
array[a]=temp;
}
printf("\nSorting Complete.");
printf("\nPress any key to see the array.");
getch();
for(i=0; i<=9; i++)
printf("\narray[%d] = %d", i, array[i]);
getch();
printf("\nEnter number to search: ");
scanf("%d",&num);
for(i=0; i<=9; i++)
if(array[i]==num)
{printf("\nFOUND: %d at index array[%d]", num, i);
found=1;
break;}
if(found==0)
printf("\nNOT FOUND...");
printf("\nPress any key to see Maximum&Minimum numbers...");
getch();
printf("\nMaximum=%d, Minimum=%d", array[9],array[0]);
getch();
}
ALGORITHM
1: Define an integer array of size 10 (i.e. 0 – 9)
2: Take input from user one by one in each the index of the array using:
for(i=0;;)
{
printf("Enter any number (0-9999): ");
scanf("%d", &num);
if((num>9999)||(num<0))
{
printf("\n\n\n\tINVALID INPUT");
getch();
clrscr();
j=0;
}
else
{
array[j]=num;
j++;
}
if(j==10)
break;
}
printf("\nInput Complete.");
for(i=0; i<=9; i++)
printf("\narray[%d] = %d", i, array[i]);
3: Use bubble/jump sort algorithm to sort out the values of the array in
ascending order and display a message using following code:
4: Now take user input for the number to search in the array and display the
number and location of the number if found or else show the NOT FOUND
message using following procedure:
5: Now display Minimum and the Maximum numbers in the array by simply
displaying the values of 0th and the 9th index of the array respectively,
as the array would be sorted in ascending order.
6: Terminate the program
NOTE: You can also add user interaction in the program as it is shown in the
program code.