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

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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.

#include<stdio.h>
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<A[i])
{
max = A[i];
maxpos = i;
}
}
min = A[1];
minpos = 1;
for(i=1; i<=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();
}

Download exe and source code here.

Share on FacebookTweet about this on TwitterDigg thisPin on PinterestShare on LinkedInShare on StumbleUponShare on TumblrShare on Google+Email this to someone

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

  1. 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..

    Reply

Leave a Reply