Logic: The entered array is traced through the dimension. Initially the program assumes the first member is the smallest and the greatest till now. In each iteration, the stored min and max number is checked if the present number is smaller than the min, or larger than max stored. If so, the present number is overwritten on the old value. Finally the lowest and highest members will be stored in min and max respectively.
We demonstrated the algorithm, by taking the integer array. It is also possible to work with the array of other data types also.
#include
void main()
{
int A[25], max, min, maxpos, minpos, n, i;
clrscr();
printf(“\n\n\t ENTER THE SIZE OF THE ARRAY…: “);
scanf(“%d”, &n);
printf(“\n\n\t ENTER THE ELEMENTS OF THE ARRAY…: “);
for(i=1; i<=n; i++) { gotoxy(25, 15+i); scanf(“%d”, &A[i]); } max = A[1]; maxpos = 1; for(i=1; i<=n; i++) { if(max<=n; i++) { if(min>A[i])
{
min = A[i];
minpos = i;
}
}
printf(“\n\n\t THE LARGEST ELEMENT IS…: %d”, max);
printf(“\n\n\t AND ITS POSSITION IS…: %d”, maxpos);
printf(“\n\n\tTHE SMALlEST ELEMENT IS…: %d”, min);
printf(“\n\n\t AND ITS POSSITION IS…: %d”, minpos);
getch();
}
Related Links :
No comments:
Post a Comment