A C program with a function that returns the minimum and the maximum value in an array of integers

By | October 15, 2013
void minmax(int array[],int length,int *min,int *max); //prototype

Let’s identify variables needed for this program.

In this program, we need several variables to store array of decimal numbers and count the length of array and store minimum and maximum of the array.

First variable will be the one which will save the list of integers in an array and it will be array[]. Next is will be min and max which will hold the minimum and maximum of the list. Next we will need a length variable to store Length of a array initialized so in all Four variables.
The identified variables are array[],min,max,length.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

 void minmax(int array[],int length,int *min,int *max);
  int array[]={12,4,8,14,3,21,45,18,1,6,9,2};
  int length,min,max;

Now, Selection of data type will be int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient for array[],min,max,length.

In this program, Variable length is used for storing Length is used to count the array size which will be integer value. Call the function by passing array[], length, address of min and max variable. Finally print the minimum and maximum i.e. min and max variable returned by the function at the end of the program.

#include<stdio.h>
void main()
{
  void minmax(int array[],int length,int *min,int *max);
  int array[]={12,4,8,14,3,21,45,18,1,6,9,2};
  int length,min,max;
  clrscr();
  length=0;
  while(array[length]!=NULL)
  {
     length++;
  };
  minmax(array,length,&min,&max);
  printf("\nminimum=%d",min);
  printf("\nmaximum=%d",max);
  getch();
}

Let’s identify variables needed for function which is part of program outside main function.
Now the only variable i which will be used for FOR LOOP. The identified variable is i.

Now, Selection of data type is int data type due to the values expected are decimals and they will be holding smaller values so int data type is sufficient.

void minmax(int array[],int length,int *min,int *max)
{
    int i;
    *min=*max=array[0];
 
    for(i=1;i<length;i++)
    {
       if(*min>array[i])
        *min=array[i];
       else if(*max<array[i])
        *max=array[i];
    }
}

In this program, pointer *min and *max which will be used for moving the minimum and maximum value to the variable within the main function loop. Both the value of pointers are assigned to the first in the list. and Inside the LOOP the value of the array is compared and replaced till the LOOP ends and finds minimum and maximum values in the array and return to main function.

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.   void minmax(int array[],int length,int *min,int *max);
  5.   int array[]={12,4,8,14,3,21,45,18,1,6,9,2};
  6.   int length,min,max;
  7.   clrscr();
  8.   length=0;
  9.   while(array[length]!=NULL)
  10.   {
  11.      length++;
  12.   };
  13.   minmax(array,length,&min,&max);
  14.   printf("\nminimum=%d",min);
  15.   printf("\nmaximum=%d",max);
  16.   getch();
  17. }
  18.  
  19. void minmax(int array[],int length,int *min,int *max)
  20. {
  21.     int i;
  22.     *min=*max=array[0];
  23.  
  24.     for(i=1;i<length;i++)
  25.     {
  26.        if(*min>array[i])
  27.         *min=array[i];
  28.        else if(*max<array[i])
  29.         *max=array[i];
  30.     }
  31. }

SCREEN SHOTS:-

C_program_Min_Max_Function

C_program_Min_Max_Function_Output

Leave a Reply