Finding the maximum and minimum in an array

Program Description

The program demonstrates, how to find the maximum and minimum number in an integer array. The array contents are taken from the user.The array of appropriate size is created in the beginning by taking the size from the user.

Program
/**********************************************
*   code.cheraus.com
* 
* Finding the maximum and minimum in an array
* 
**********************************************/
#include<stdio.h>

//function prototypes
int max(int arr[],int n);
int min(int arr[],int n);

int main()
{
   int n,i,ans;
   
   //taking array size from user
   printf("\nEnter the number of elements : ");
   scanf("%d",&n);
   
   //allocating space of appropriate size for integer array
   int array[n];
   
   //filling in the array elements from the user   
   for(i=0;i<n;i++)
    {
       printf("\nEnter the %d element : ",i+1);
       scanf("%d",&array[i]);
    }
	
	
	//displaying the array contents
    printf("\nThe array you entered is : { ");
   for(i=0;i<n;i++)
    {
      printf("%d \t",array[i]);
    }
    printf("}");
   
   
   //calling max() function to find maximum value in array and storing the result in "ans"
   ans = max(array,n);
   
   //displaying the max value
   printf("\nThe max no. in the array is --> %d",ans);
   
   
   //calling min() function to find minimum value in array and storing the result in "ans"
   ans = min(array,n);
   
   //displaying the min value
   printf("\nThe min no. in the array is --> %d\n",ans);
   
   
   return 0;
}


/*
Function definition for max()

The function finds the maximum value in the integer array and returns the maximum value
element to the calling function.

Input: The function takes the following arguments:
       1) Integer array
	   2) size of the integer array
	   
Return: The function returns the maximum value element from the given array.	   
*/

int max(int arr[],int n)
{
  int i,temp;
  
  temp=arr[0];
  
  
  for(i=1;i<n;i++)
   {
  //if i'th element is greater than the temp,make temp=i'th element
   if(temp<arr[i])
      temp = arr[i];  //current highest value
   }
   
   //return result
  return temp;
}



/*
Function definition for min()

The function finds the minimum value in the integer array and returns the minimum value
element to the calling function.

Input: The function takes the following arguments:
       1) Integer array
	   2) size of the integer array
	   
Return: The function returns the minimum value element from the given array.	   
*/
int min(int arr[],int n)
{ 
  int i,temp;
  temp=arr[0];
  
  for(i=1;i<n;i++)
  { 
   //if i'th element is smaller than the temp,make temp=i'th element
    if(temp>arr[i])
      temp = arr[i];   //current smallest element
  }
  
  //return result
  return temp;
}

Output
Test Case 1:


Enter the number of elements : 8
Enter the 1 element : 1
Enter the 2 element : 2
Enter the 3 element : 3
Enter the 4 element : 7
Enter the 5 element : 9
Enter the 6 element : 121
Enter the 7 element : 343
Enter the 8 element : 21
The array you entered is : { 1 2 3 7 9 121 343 21 }
The max no. in the array is --> 343
The min no. in the array is --> 1

Test Case 2:

Enter the number of elements : 10
Enter the 1 element : 234
Enter the 2 element : 34
Enter the 3 element : 34
Enter the 4 element : 3
Enter the 5 element : 2
Enter the 6 element : 3
Enter the 7 element : 23
Enter the 8 element : 2
Enter the 9 element : 434
Enter the 10 element : 3
The array you entered is : { 234 34 34 3 2 3 23 2 434 3 }
The max no. in the array is --> 434
The min no. in the array is --> 2

Comments
comments powered by Disqus