Modify your week 5 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 pseudocode for your Greatest function:
int Greatest(int count)
{
// Initialize final result variable to largest number possible
//

// start a loop, increment from 0 to count
// 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

This is what i was the information i was givin to change my code.. but i don't understand what exactly i am suppose to be doing...

this is my code so far..(before changes)

``````#include <iostream>

using namespace std;

const int SENTINEL = -99; //Code for exiting loop

int main()
{
int numbers; //variables
int large;
int small;
int smallest = 0;
int counter;
int largest = 0;
int next;
char letter;
bool quit = false;

while(!quit)
{
cout << "A - Find the largest # with a set quantity of numbers. \n"; //this is the program initiating
cout << "B - Find the smallest # with an unknown quantity of numbers. \n";
cout << "C - Quit Program. \n";
cin >> letter; //choice is inputted

switch (letter)
{
case 'A':
cout << "Enter the number of numbers to be compared. \n";
cin >> numbers;
cout << "Enter the numbers please:";
cin >> large;
largest = large;
for (counter = 0; counter < numbers-1; counter++)
{
cin >> next;
if (largest < next)
{
largest = next;
}
}
cout << "The largest number you entered was. \n";
cout << largest << endl; //code for the largest number inputted
break;
case 'B':
cout << "Enter numbers, the smallest number entered of all the numbers will be displayed. \n To exit and receive answer, enter -99" << endl;
cin >> small;
smallest = small;
while (next != -99)
{
cin >> next;
if (smallest > next && next != -99)
{
smallest = next;
}
}
cout << "The smallest number you entered was. \n";
cout << smallest << endl; //code for the smallest number inputted
break;
case 'C':
quit = true;
break;
}
cout << endl;
}

return 0;
}``````
4
Contributors
7
Replies
8
Views
9 Years
Discussion Span
Last Post by shasha821110

you need to create two functions that do what your instructions told you to do. Then move the code you have on lines 38-44 into function A and lines 50-59 into function B. Lastly, change what you now have on line 38 to call function A, and change what you have on line 50 to call function B.

Replace the terms "function A" and "function B" with function names that have more meaning to your program.

I guess part of my problem is that i don't quite understand the instructions i don't know what he's wanting or what he's looking for... if i understood the instructions a bit more i could atleast have an idea of what i'm suppose to be doing... i mean i'm new to this so the function thing i'm not sure what/how to do that.. but i can learn but i'm not sure what function i'm suppose to be inputing and not sure what exactly function A and function B are suppose to do.. i understand it says it in his assignment.. but i don't understand the way it's worded.

I thought AncientDragon explained it pretty well.

Your assignment is to create a couple of functions which implement the menu options 'A' and 'B'. When you're done, the person running the program shouldn't be able to tell that you have made any changes, the program should still behave as it does.

The instructor also pretty clearly stipulated the interface to both functions. The first function must take the quantity of numbers as an argument and return the largest number entered. (So the main code will need to prompt for the quantity of numbers and display the largest.)

The second function is to take no arguments and return the smallest number entered. (So the main code will need to display the smallest number.)

He wants the switch to look something like this:

``````switch(letter)
{
case 'A':
// prompt for the quantity of numbers here
// display the largest number
break;
case 'B':
// display the smallest number
break;
}``````

(Though those are bad function names, you can do better. I also didn't declare the largestnumber or smallestnumber variables, feel free to change their names as well.)

Most of the code that used to be in the switch statement will be 'inside' the new functions.

Yeah once i started understanding it a bit better than relooked at what ancient dragon had posted i realized that he did explain it very well as did everyone else.. now i can got to work on this lol 0.o thanx alot you guys i'll post if i ever complete the dang things :D

ugh i'm so stupid... i don't get this... what am i doing wrong?

``````#include <iostream>

using namespace std;

const int SENTINEL = -99; //Code for exiting loop

int main()
{
int numbers; //variables
int small;
int smallest = 0;
int largest = 0;
int next;
char letter;
bool quit = false;
while(!quit)
{
cout << "A - Find the largest # with a set quantity of numbers. \n"; //this is the program initiating
cout << "B - Find the smallest # with an unknown quantity of numbers. \n";
cout << "C - Quit Program. \n";
cin >> letter; //choice is inputted

switch (letter)
{
case 'A':
cout << "Enter the number of numbers to be compared. \n";
cin >> numbers;
cin >> next;
{
largest = functiona(numbers);
}
break;
case 'B':
cout << "Enter numbers, the smallest number entered of all the numbers will be displayed. \n To exit and receive answer, enter -99" << endl;
cin >> small;
smallest = small;
{
smallest = functionb();
}
break;
case 'C':
quit = true;
break;
}
cout << endl;
}

return 0;
}

int functiona(numbers)
{
int largest = 0, next = 0;

if (largest < next)
{
largest = next;
}
}
cout << "The largest number you entered was. \n";
cout << largest << endl; //code for the largest number inputted
}

int functionb(void)
while (next != -99)
{
cin >> next;
if (smallest > next && next != -99)
{
smallest = next;
}
}
cout << "The smallest number you entered was. \n";
cout << smallest << endl; //code for the smallest number inputted
}``````

You should be getting compile errors with that code.

functionb is not declared properly

functiona needs a loop (for loop?) and the input statement.

The prints for largest and smallest should be from the main code and not from in functiona or functionb

both functions need to 'return' what they found.

Hi, i test your code and i was thinking(don't know weather its correct or not) maybe for the greatest function you should do in this way:

``````if(case 'A')
{
cout << "Enter the number of numbers to be compared. \n";
cin >> numbers;
for(int i=0; i< numbers; i++)
{
cout<<"Enter the next number\n";
cin >> next;

largest=next;

if (largest < next)
{largest = next;}
}``````

and another thing don't use break or continue in the switch, it may cause problems.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.