Answer to Question #342818 in C++ for herry

Question #342818

Write a user defined function that will ask the user to input 5

array integers and performed the following menu functions:

1. Maximum

2. Minimum

3. Count Odd

4. Count Even

5. Exit

If the user press the following number (s), do the corresponding

functions

1. Print the largest array value

2. Print the lowest array value

3. Count the number of even number(s)

4. Count the number of odd number(s)

5. Exit

Sample Input/Output:

==========Main Menu==========


[1] – Maximum

[2] – Minimum

[3] – CountEven

[4] – CountOdd

[5] – Exit

==============================

Enter your choice: 2

Enter 5 integer number(s):

20

30

8

500

800

The lowest value is: 8


1
Expert's answer
2022-05-19T16:41:10-0400
#include <iostream>

using namespace std;

void Menu()
{
	cout << "\n==========Main Menu==========\n"
		<< "[1] - Maximum\n"
		<< "[2] - Minimum\n"
		<< "[3] - Count Odd\n"
		<< "[4] - Count Even\n"
		<< "[5] - Exit\n";
	cout << "=============================\n";
}

int Largest(int arr[5])
{
	int larg = arr[0];
	for (int i = 0; i < 5; i++)
	{
		if (arr[i] > larg)
		{
			larg = arr[i];
		}
	}
	return larg;
}


int Smallest(int arr[5])
{
	int smallest = arr[0];
	for (int i = 0; i < 5; i++)
	{
		if (arr[i] < smallest)
		{
			smallest = arr[i];
		}
	}
	return smallest;
}


int CountOdd(int arr[5])
{
	int odd = 0;
	for (int i = 0; i < 5; i++)
	{
		if (arr[i]%2==1)
		{
			odd++;
		}
	}
	return odd;
}


int CountEven(int arr[5])
{
	int even = 0;
	for (int i = 0; i < 5; i++)
	{
		if (arr[i] % 2 == 0)
		{
			even++;
		}
	}
	return even;
}


int main() 
{
	char ch;
	do
	{
		Menu();
		cin >> ch;
		if (ch == '5')break;
		int arr[5];
		cout << "Please, enter 5 integer values: ";
		for (int i = 0; i < 5; i++)
		{
			cin >> arr[i];
		}
		switch (ch)
		{
			case '1':
			{
				cout<<"The largest value is "<<Largest(arr);
				break;
			}
			case '2':
			{
				cout << "The lowest value is " << Smallest(arr);
				break;
			}
			case '3':
			{
				cout << "The number of odd values is " << CountOdd(arr);
				break;
			}
			case '4':
			{
				cout << "The number of even values is " << CountEven(arr);
				break;
			}
		}
	} while (true);
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS