Hi, I'm only in my second term of college and still grasping everything about C++ programming. This week in week 5 of my term we have this assignment.
Functions
Modify your week 4 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.
The thing I'm confused on is I don't understand how I'm supposed to change my code into a function. I've talked to my teacher and she says I have case statements in my code. Any advice, help on this would be awesome. Here is my code from last week...
#include <iostream>
usingnamespace std;
constint SENTINEL = -99; //Sentinel Value set at -99 that will cause the loop to end
int main()
{ //Variables
int numbers;
int smallest;
int counter;
int largest;
int next;
char response;
bool quit = false;
while(!quit) //While statement that never ends unless user inputs right commands
{
cout << "\nWelcome and please use the following menu.\n";
cout << "\nEnter A -- Find the largest # with a set quantity of numbers. \n";
cout << "Enter B -- Find the smallest # with an unknown quantity of numbers. \n";
cout << "Enter C -- Quit Program. \n";
cout << "\nPlease enter your choice: ";
cin >> response; //User Input for the menu
switch (response) //Switch Statement allows transitions between Inputs
{
case'A': //Switch Statement allows for either Uppercase or lowercase letters to be entered
case'a':
cout << "\nIn this section you will enter numbers and compare them to figure out the \nlargest of the numbers you entered.\n";
cout << "\nEnter the amount of numbers to be compared. \n"; //User enters numbers to be compared and find the largest of them
cin >> numbers;
cout << "\nEnter the numbers.\n";
cin >> largest;
for (counter = 0; counter < numbers-1; counter++) //For loop figures out largetst number entered by the user
{
cin >> next;
if (largest < next)
{
largest = next;
}
}
cout << "\nThe largest number entered was. \n";
cout << largest << endl; //Output of largest number entered
break;
case'B':
case'b':
cout << "\nEnter any amount of numbers.\n"; //User Inputs numbers
cout << "After that enter -99 to display the smallest number and exit.\n"; //User enters Sentinel Value -99 to stop
cin >> smallest;
next = smallest;
while (next != -99) //While statement sorts out all the numbers to figure out smallest
{
cin >> next;
if (smallest > next && next != -99)
{
smallest = next;
}
}
cout << "\nThe smallest number entered was. \n";
cout << smallest << endl; //Output of smallest number entered
break;
case'C':
case'c':
quit = true; //Ends the Program
break;
} // Ends Switch
} // End While
return 0;
}
#include <iostream>
usingnamespace std;
constint SENTINEL = -99;//Sentinel Value set at -99 that will cause the loop to end
int larger(int x, int y);
int main()
{ //Variables
int numbers;
int smallest;
int counter;
int largest;
int next;
char response;
bool quit = false;
while(!quit)//While statement that never ends unless user inputs right commands
{
cout << "\nWelcome and please use the following menu.\n";
cout << "\nEnter A -- Find the largest # with a set quantity of numbers. \n";
cout << "Enter B -- Find the smallest # with an unknown quantity of numbers. \n";
cout << "Enter C -- Quit Program. \n";
cout << "\nPlease enter your choice: ";
cin >> response;//User Input for the menu
switch (response)//Switch Statement allows transitions between Inputs
{
case'A'://Switch Statement allows for either Uppercase or lowercase letters to be entered
case'a':
cout << "\nIn this section you will enter numbers and compare them to figure out the \nlargest of the numbers you entered.\n";
cout << "\nEnter the amount of numbers to be compared. \n";//User enters numbers to be compared and find the largest of them
cin >> numbers;
cout << "\nEnter the numbers.\n";
cin >> largest;
for (counter = 0; counter < numbers-1; counter++)//For loop figures out largetst number entered by the user
{
cin >> next;
largest = larger(largest, next);
}
cout << "\nThe largest number entered was. \n";
cout << largest << endl;//Output of largest number entered
break;
case'B':
case'b':
cout << "\nEnter any amount of numbers.\n";//User Inputs numbers
cout << "After that enter -99 to display the smallest number and exit.\n";//User enters Sentinel Value -99 to stop
cin >> smallest;
next = smallest;
while (next != -99)//While statement sorts out all the numbers to figure out smallest
{
cin >> next;
if (smallest > next && next != -99)
{
smallest = next;
}
}
cout << "\nThe smallest number entered was. \n";
cout << smallest << endl;//Output of smallest number entered
break;
case'C':
case'c':
quit = true;//Ends the Program
break;
}// Ends Switch
}// End While
return 0;
}
int larger(int x, int y)// Larger Function
{
if (x >= y)
return x;
elsereturn y;
}
I think A now has a function like my assignment asks for.
Now I just think I need help with part B which I'm at a lost with.