Suppose, if you have an array consisting of scores of a game, you might want to know which individual has secured the highest or the lowest position and what are the highest and the lowest scores.

Algorithm:

1. Assume the first element as max/min.

2. Compare each element with the max/min.

3. If, the element is greater than max or smaller then min, then, we change the value of max/min respectively.

4. Then, output the value of max and/or min.

There are four methods using which we can write a program to find max or min element. They are as follows:

  1. Without using functions
  2. Using functions
  3. Using pointer
  4. Using in-built method

Technique 1: Basic without any functions

Logic:

The main logic for the first three techniques is the same, just the execution part is different. Here, we directly use the logic of maximum and minimum in main().


#include <iostream>

using namespace std;

#define SIZE 50 //Defining max size of array

int main()

{
    int array[SIZE];
    int i, max, min, size;
    // Input size of the array
    cout<<"Enter size of the array: ";
    cin>>size;
    // Input array elements
    cout<<"\n Enter "<<size <<" elements in the array: ";
    for(i=0; i<size; i++)
        cin>>array[i];
    // Assume first element as maximum and minimum
    max = array[0];
    min = array[0];
    //Find maximum and minimum in all array elements.
    for(i=1; i<size; i++)
    {
    // If current element is greater than max
        if(array[i] > max)
            max = array[i];
        // If current element is smaller than min
        if(array[i] < min)
            min = array[i];
    }
    // Print maximum and minimum element
    cout<<"\nMaximum element =" << max << "\n";
    cout<<"Minimum element =" << min;
    return 0;

}

Output:

Enter the size of the array:
Enter 3 elements in the array:
Maximum element =63
Minimum element =12

Technique 2: Using Functions

Here, we use two functions, one for finding the maximum number and the other for the minimum. We pass the array and the size of the array to functions as parameters. The main logic of the function is same as the previous one.

Code:

#include <iostream>
using namespace std;
int FindMax(int a[],int n) //function to find largest element
{
	int i, max;
	max = a[0];//assume that first element is max
	for(i=1;i<n;i++)
	{
		if(a[i]>max) //if currentelement is greater than max
			max =a[i]; //assign that number as max now
	}
	return max; //returns the largest number to main function
}

int FindMin(int a[],int n) //function to find smallest element
{	
	int i, min;
	min = a[0];// assuming first element as minimum
	for(i=1;i<n;i++)
	{
		if(a[i]<min)// If current element is smaller than min
			min =a[i];//assigning the smaller number to min
	}
	return min; //returns the smallest number to main function
}
int main()
{
	int i, array[50], size, max, min;

	cout<<"Input number of elements in array\n";
	cin>>size;

	cout<<"Enter "<< size << " integers\n";
	for(i=0;i<size;i++)
	cin>>array[i];

	max = FindMax(array,size); //calls the max function
	min = FindMin(array,size); //calls the min function

	// Print maximum and minimum element
	cout<<"Maximum element in the array is:" << max << "\n";
	cout<<"Minimum element in the array is:" << min << "\n";

return 0;
}

Output:

Input number of elements in array 3
Enter 3 integers
Maximum element in the array is:63
Minimum element in the array is:12

Technique 3: Using Pointers

A pointer is just a name, that points to a certain position in the memory. The name of the array points to the address of its first element. Thus arrays and pointers use the same concept.

Algorithm:

  1. Declare two pointers max and min.
  2. max and min are made to point to the first element of the array.
  3. Now we use “*” operator to access the value, where the pointer points.
  4. Now, *max and *min, work similar to a normal variable containing max/min value of an array.
  5. Next, we execute the same algorithm as used earlier.

Code:

#include <iostream>
using namespace std;
int main()
{
    int array[50], *max, *min, size, i; //pointer variables declared for max and min
    cout<<"Enter the number of elements in array\n";
    cin>>size;
    cout<<"Enter array elements\n";
    for ( i = 0 ; i < size ; i++ )
    cin>>array[i];
    max = array; //assigning max pointer to the address of the first element
    min = array; //assigning min pointer to the address of the first element
    for (i = 0; i < size; i++)
    {
    //Finding the largest element in the array
    if (*(array+i) > *max)//check if the value stored at array+i is greater than value stored at max
        *max = *(array+i);
    }
    cout<<"Maximum element in the array is "<< *max << "\n" ;
    for (i = 0; i < size; i++)
    {
    //Finding the smallest element in the array
    if (*(array+i) < *min)//check if the value stored at array+i is lesser than value stored at min
        *min = *(array+i);
    }
    cout<<"Minimum element in the array is"<< *min <<"\n";
    return 0;
}

Output:

Enter the number of elements in array 5
Enter array elements 54 12 63 78 13
Maximum element in the array is 78
Minimum element in the array is12

Technique 4: Using Inbuilt method

The syntax of the inbuilt method is:

ForwardIterator min_element (ForwardIterator first, ForwardIterator last);

where,

First ForwardIterator pointing to the beginning of the range.

Last ForwardIterator pointing to the end of the range.

And, it returns a pointer to the smallest element in the range, when there are more than one, it points to the first one. It points to the last element in case if the array is empty.

Code:

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int array[50], *maximum, *minimum, size, i; //pointer variables declared for max and min
    cout<<"Enter the number of elements in array\n";
    cin>>size;
    //input for array elemets
    cout<<"Enter array elements\n";
    for ( i = 0 ; i < size ; i++ )
        cin>>array[i];
    //minimum pointer points in the range from first element of array to last
    // Returns the first one if more than one numbers
    // are same
    minimum= std::min_element(array,array+size);
    cout<<"Minimum element in the array is "<< *minimum <<"\n";
    //maximum pointer points in the range from first element of array to last
    maximum= std::max_element(array,array+size);
    cout<<"Maximum element in the array is "<< *maximum <<"\n";

return 0;
}

Output:

Enter the number of elements in array 4
Enter array elements 354 2642 245 754
Minimum element in the array is 245
Maximum element in the array is 2642

Report Error/ Suggestion

Related Posts:

[yuzo_views]











CopyRight © 2019

CopyRight © 2019