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

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.

Editorial Team
Editorial Team

We are a group of young techies trying to provide the best study material for all Electronic and Computer science students. We are publishing Microcontroller projects, Basic Electronics, Digital Electronics, Computer projects and also c/c++, java programs.

2 thoughts on “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..

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest updates on your inbox

Be the first to receive the latest updates from Codesdoc by signing up to our email subscription.

    StudentProjects.in