I have an assignment for my class that asks to modify my previous assignment to include a function for both A and B. The function for A should have the quantity of numbers passed in as a parameter and needs to return the largest number. The function for B should have no parameters and return the smallest number.
Here is some pseudo-code for your greatest function:
int Greatest(int count)
{
// Initialize final result variable to largest number possible
//
// start a loop, increment from 0 to count
// Read next number
// Compare number to final result variable
// if number is greater than final result variable, then
// store in final result variable
// end of loop
// return final result
}// end of function greatest
//Here is what I have so far. The application seems to run well without the Void function. I am new to programming, and I am finding it difficult to handle this assignment. I am looking for a clear path of explanation in order to learn.
#include <iostream>
using namespace std;
void smallest();
int main ()
{
char choice;
int quantity;
int num1;
int num2;
int largest;
int smallest;
double larger(double x, double y);
double smaller(double x, double y);
do
{
cout<<"Please choose one of the following options"<<endl;
cout<<"A - Find the largest number within a known quatity of numbers"<<endl;
cout<<"B - Find the smallest number within an unknown quantity of numbers"<<endl;
cout<<"C - Quit"<<endl;
cout<<"Please enter your choice: ";
cin>>choice;
cout<<endl;
switch (choice)
{
case'a':
case 'A':
//gave both lower case and capital 'a' to allow for user freedom of choice
cout<<"A - find the largest number within a known quatity of numbers"<<endl;
cout<<"How many numbers would you like to use? ";
cin>>quantity;
for(quantity;quantity > 0; quantity--)
{
cout<<"Please enter a number: ";
cin>>largest;
cout<<endl;
for(quantity;quantity > 1; quantity--)
{
cout<<"Please enter a number: ";
cin>>num1;
cout<<endl;
if(largest<=num1)
largest=num1;
}
}
cout<<"The largest number entered is ("<<largest<<")"<<endl;
break;
case 'b':
case 'B':
//gave both lower case and capital 'b' to allow for user freedom of choice
cout<<"B - Find the smallest number within an unknown quantity of numbers" <<endl;
cout<<"Enter your first number: ";
cin>>num2;
cout<<endl;
smallest=num2;
cout<<"To get result and return to main menu enter (-999) for your number" <<endl;
//used sentinel to allow user to initiate close of process
while (num2 !=-999)
{
cout<<"Please enter another number: ";
cin>>num2;
cout<<endl;
if (num2 != -999)
if (num2<smallest)
smallest=num2;
}//keeps prompting until sentinel is input
cout<<"The smallest number you entered is: (" <<smallest<<")"<<endl;
break;
case'c':
case'C':
//gave both lower case and capital 'c' to allow for user freedom of choice
exit('c');
default:
cout<<"The letter you entered is not valid"<<endl;
//this works well in case of user input error during the menu.
//Does not help with user input error during the different parts of application.
}
if (choice == 'b')
{
cout << "Enter quantity of numbers: ";
cin >> quantity;
count = quantity;
larger (quantity);
} if (choice == 'b')
{
cout << "Enter quantity of numbers: ";
cin >> qty;
count = qty;
larger (qty);
}
}
while(choice !='c');
//end of do while loop
return 0;
}
double largest (double x, double y)
{
double max = x;
if ( y > max)
max = y;
return max;
}
void smallest()
{
int smallest(int quantity);
for (count = 1; count > qty; count++)
{
cout << "Enter " << qty << " numbers: ";
cin >> num2;
max = smallet(min, num2);
done = false;
}
cout << "The largest number is: " << min;
}void smallest()