C Program to find the biggest and smallest number and its location

Given an array of integer numbers, we sometimes need to find the largest and the smallest number from the array. This is not a problem if the array is already sorted in ascending or descending order.

Advertisements

This program finds the biggest and the smallest number from a given array of integers and also tell the index value (location) of those values.

We have used Dev-C++ 4.9.9.2 compiler installed on a windows 7 64-bit computer to run the program. You can do the same or use any other ANSI standard C compiler to run the program. The program is intended for beginners of C programming language.

Advertisements

Problem Definition

you are given an array of unsorted integers and you want to solve two problems.

  1. Find biggest number
  2. Find smallest number

To solve this problem very easily by

  1. Assigning the first number from the array as the smallest number. You do not know yet, if this is the smallest number, we are just assuming it is.
  2. Compare this smallest assumed value with other elements of the array and if you encounter another smaller value than the current smallest value, the new value becomes the smallest.
  3. Then the process repeats itself, until search procedure is exhausted.

To find the biggest number do the same thing except the first value is assumed to be the largest value in the array.

Flowchart – Find Biggest and Smallest Number

Flowchart - Find Biggest and Smallest Number
Flowchart – Find Biggest and Smallest Number

Program Code

/* program to find the biggest and smallest number in an array of numbers. */

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100 

main() {

/* declare variables array, smallest, biggest,
location_smallest,location_biggest,array_size,i*/

    int array_of_number[SIZE];
    int big_num,small_num,loc_small,loc_big;
    int i,n;

/* Get the size of the array input */

    printf("ENTER THE SIZE OF THE ARRAY:");
    scanf("%d",&n);

/* Get the Array elements */

    printf("GET THE ARRAY ELEMENTS:");

for(i=1;i<=n;i++) {

    scanf("%d",&array_of_number[i]); 

    }

/* Search for the biggest Number and it's location */

    big_num = array_of_number[1];

for(i=1;i<=n;i++) {

    if(big_num <= array_of_number[i]) {

        big_num = array_of_number[i];
        loc_big = i;
    }
}

printf("_________________________________________\n");

printf("The Biggest Number is %d at location = %d\n",big_num,loc_big);

printf("_________________________________________\n");

/* Find smallest number and it's location */

small_num = array_of_number[1];

for(i=1;i<=n;i++) { 

    if(small_num >= array_of_number[i]) {
        small_num = array_of_number[i];
        loc_small = i;
    }
}

printf("___________________________________________\n");

printf("The smallest Number is %d at location = %d\n",small_num,loc_small);

printf("___________________________________________\n");

    getch();
    return 0;
}


Output

ENTER THE SIZE OF THE ARRAY:5
GET THE ARRAY ELEMENTS:53
66
3
6
23
_________________________________________
The Biggest Number is 66 at location = 2
_________________________________________
_________________________________________
The smallest Number is 3 at location = 3
_________________________________________

Advertisements

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.