To find the biggest and smallest number and positions in the given array

Thursday, December 4th, 2008

This program deals with finding the largest and smallest magnitude in an array.  The program asks the user to enter the dimension of the integer array, which he can enter soon after that.

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.

void main()
int A[25], max, min, maxpos, minpos, n, i;
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++)
max = A[i];
maxpos = i;
min = A[1];
minpos = 1;
for(i=1; i<=n; 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);

Download exe and source code here.

Author Name :

Total : 2 Comments

2 Responses to “To find the biggest and smallest number and positions in the given array”

  1. lea says:

    can you please post a program that ask ten numbers and then sort it from highest to lowest and from lowest to largest?thank you very much..

  2. chaman patel says:

    thanks a lot

Leave a Reply

Question and Answer
C/C++ Unix & Linux Wordpress
Source codes
C C++ Java

Free email signup