In: Computer Science
Write a program calls each of the three methods described below and prints out the result of the two methods which return value
reverse- a void method that reverses the elements of the array.
Print the array before you call the method and after call the
method.
getMin- an int method that returns the smallest value in the
array
Sample output
array: 22,34,21,35,12,4,2,3,99,81
array in reverse: 81,99,3,2,4,12,35,21,34,22
the smallest number is 2
PROBLEM :
To reverse the array and find the minimum element in the array.
----------------------------------------------------------------------------------------------------------------
EXPLANATION / ALGORITHM :
2. Find the smallest element-
Code is written in C++.
-----------------------------------------------------------------------------------------------
CODE:
#include <bits/stdc++.h>
using namespace std;
void reverse(int array[], int left, int right)
{
    while (left < right) //run while loop till
lef is less than right
    {
        int var =
array[left];   // Next 3 lines swaps the elements
        array[left] =
array[right];
        array[right] =
var;
        left++; //increment the
left
        right--; //decrement the
right
    }
}    
void displayElements(int array[], int size) //function to print
the elements of the array
{
   for (int i = 0; i < size; i++)
   {
       cout << array[i]
<<" ";
   }
   cout << endl;
}
int getMin(int array[],int size)
{
    int smallest = array[0]; //initialise first
element as the smallest element
    for (int i = 1; i < size; i++)
   {
       if(array[i] < smallest)
//if current element is less the smallest update the smallest
           
smallest = array[i];
   }
   return smallest;
}
int main()
{
    int array[] =
{22,34,21,35,12,4,2,3,99,81};
    int size = sizeof(array) /
sizeof(array[0]);
    cout<<"array:";
    displayElements(array, size); //print the
array
    cout<<"array in reverse:";
    reverse(array, 0, size-1); // reverses the
elements of the array
    displayElements(array, size); //After reverse
print
    cout<<endl;
    cout<<"The smallest number is :
"<<getMin(array,size-1); //prints the smallest number
    return 0;
}
OUTPUT:
