C Programming

Min and Max in C

Finding the minimum and maximum values from an array of elements in the C programming language is a common practice and is used in several programs. Thus, it’s necessary for C programming beginners to learn the use of this practice. If you are the one searching for help related to min and max in C, follow this article’s guidelines.

Min and Max in C

In C programming language, there are two most used functions to find min and max values, which are as follows:

1: User-Defined Functions

The user-defined functions in C language are the ones created by the user itself. These functions include defining the variable data type, array, floats and more. These types of functions include the for loop and the main process to compare each element of array one by one. After completing the final iteration, the function will then print out the min and max values from an array using the “printf” function.

An example of such type of user-defined functions is given below:

#include <stdio.h>

 int calcarray(int x[],int n)
 {
    int min,max,y;
    min=max=x[0];
    for(y=1; y<n; y++)
    {
         if(min>x[y])
          min=x[y];
           if(max<x[y])
            max=x[y];
    }
    printf("minimum of array is : %d",min);
    printf("\nmaximum of array is : %d",max);
 }
int main()
{
    int x[1000],y,n,sum;
    printf("Enter size of the array : ");
    scanf("%d", &n);

    printf("Enter elements in array : \n");
    for(y=0; y<n; y++)
    {
        scanf("%d",&x[y]);
    }
    calcarray(x,n);
}

 

The calcarray() function in this code determines an array’s minimum and maximum values. The array, size of the array, and a value are passed as parameters to the calcarray() function in the main() function. The calcarray() function compares the min and max values with array items and outputs the values of the minimum and maximum array values.

Output

2: Built-in Functions

The user-defined function may be ideal for beginners since they will learn a step-by-step process to calculate the min and max value from an array. However, if a user doesn’t want to go into a detailed process, they can go with using the built-in functions in C called ‘fmin()’ and ‘fmax()’. These functions fetch the min or max element from a specific subset or range of the array. To use the ‘fmin’ and ‘fmax’ functions, first an array is created with the values to be checked. Then the ‘fmin’ and ‘fmax’ functions are called and the range or subset of the array is passed to them. The ‘fmin’ and ‘fmax’ functions return the minimum or maximum elements respectively. An example of such a type of function is shown below:

#include<stdio.h>
#include<math.h>

int main() {

  printf("fmax(223, 422) = %f\n", fmax(223, 422));
  printf("fmin(9.9, 2.8) = %f\n", fmin(9.9, 2.8));

  return 0;
}

 

In this code, built-in functions, fmax() and fmin() are used with printf statements to calculate maximum values from 223 and 422, and calculate minimum values from 9.9 and 2.8.

Output

Conclusion

The users can calculate min and max values from an array using the user-defined function or built-in function in C. The user-defined function takes more steps compared to built-in function thus, are helpful for beginners. On the other hand, the built-in functions are more promising for the users since they execute at the faster rate.

About the author

Hiba Shafqat

I am a Computer Science student and a committed technical writer by choice. It is a great pleasure to share my knowledge with the world in which I have academic expertise.