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

[codesyntax lang=”c”]

 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;

[/codesyntax]

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.

[codesyntax lang=”c”]

#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();
}

[/codesyntax]

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.

[codesyntax lang=”c”]

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];
    }
}

[/codesyntax]

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 :

[codesyntax lang=”c” lines=”normal”]

#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();
}

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];
    }
}

[/codesyntax]

SCREEN SHOTS:-

C_program_Min_Max_Function

C_program_Min_Max_Function_Output

 

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply