Prerequisite: Switch Statement in C/C++, Functions in C/C++, Loops in C and C++, C/C++ do-while loop with Examples
Write a menu-driven program to perform below various basic operations in the array:
Approach:
- Check if a number is odd or even: There are 2 ways to achieve this:
- By doing Bitwise AND of 1 and that digit if the result comes out to be 1 then the number is odd otherwise even.
- By its divisibility by 2. A number is said to be odd if it is not divisible by 2, otherwise, it is even.
- Find the sum and divide the sum by the total number of elements: By iterating through the whole array and adding it to a variable sum. Dividing the sum by total no. of elements to find the average.
- Find the max and min in the array: Initialize values of min and max as minimum and maximum of the first two elements respectively. Starting from 3rd, compare each element with max and min, and change max and min accordingly. If the element is smaller than min then change min, Else if the element is greater than max then change max, Else ignore the element
- Remove the duplicates from the array: Sort the array using the inbuilt sort( ) function. Create an auxiliary array temp[] to store unique elements. Traverse input array and one by one copy unique elements of arr[] to temp[]. Also, keep track of the count of unique elements. Let this count be j. Copy j elements from temp[] to arr[] and return j
- Print the array in the reverse: Reverse an array or string
Below is the implementation of the above approach:
Output:
👁 Image👁 Image👁 Image