C++ Write functions to do the following. Do not change the function signature. P
ID: 3668581 • Letter: C
Question
C++ Write functions to do the following. Do not change the function signature. Please write the functions, using the given function signature and specifications. The main function must display a simple menu as follows: Welcome to my program! Please choose one of the following: (a) Find the largest of a list of positive numbers (-1 to quit). (b) Given a positive number, display the number of even, odd and zero digits in the number. (c) quit this program. Based on what the user picks, use a switch or an if statement and output the corresponding answer. This menu should be in a loop so it continues to display after each turn until the user enters ‘c’ to quit.
Explanation / Answer
//header files
#include <iostream>
#include <string>
using namespace std;
// function prototypes
int largest();
void numberCount (int number, int &oddCount, int &evenCount, int &zeroCount);
// main function
int main()
{
// declare variables
int number = 0;
int oddCount;
int evenCount;
int zeroCount;
string menu;
cout << "Welcome to my program!" << endl << endl;
do
{
// selecting choice
cout << "Please choose one of the following options." << endl;
cout << "(a) Find the largest of a list of positive numbers. (-1 to quit)." << endl;
cout << "(b) Give a positive number, display the number of even, odd, and zero digits in the number." << endl;
cout << "(c) Quit this program." << endl;
cout << "Your choice: ";
cin >> menu;
if (menu == "a" || menu == "A" )
{
// largest number
number = largest();
cout << endl;
// display largest Number
cout << "The highest number you entered was: " << number << endl;
}
else if (menu == "b" || menu == "B")
{
numberCount (number, oddCount, evenCount, zeroCount);
}
cout << endl;
}
while (menu != "c" || menu != "C");
return 0;
}
// finding largest number
int largest()
{
// declare variables
int num = 0;
int largestNumber = 0;
int sentinel = -1;
// asking user to entr positive number
cout << "Please enter a positive integer. Enter -1 to quit." << endl;
do
{
cout << "Enter number: ";
cin >> num;
// checking whether num is greater than l number
if (num > largestNumber)
{
largestNumber = num;
}
}
while (num != sentinel);
return largestNumber;
}
// counting number
void numberCount (int number, int &oddCount, int &evenCount, int &zeroCount)
{
cout << "Please enter any positive integer: ";
cin >> number;
do
{
number = number % 10;
if (number == 0)
{
zeroCount = zeroCount + 1;
}
else if (number % 2 == 0)
{
evenCount = evenCount + 1;
}
else if (number % 2 != 0)
{
oddCount = oddCount + 1;
}
number = number / 10;
}
while (number != ' ');
// display result
cout << "Number of zeros: " << zeroCount << endl;
cout << "Number of evens: " << evenCount << endl;
cout << "Number of odds: " << oddCount << endl;
}
Sample output
Enter number: 5
Enter number: -1
The highest number you entered was: 13
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.