Need to Modify my codes 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. What i have so far is this.

#include <iostream>

using namespace std;

double larger(double x, double y);

int main()
{
	char response;
	int num = 0;
	int newNumber = 0;
	int max = -9999;
	int min = 9999;

	do{
		max = -9999;
		min = 9999;
		cout << "A - Find the largest number with a known quantity of numbers." << endl;
		cout << "B - Find the smallest number with an unknown quantity of numbers." << endl;
		cout << "C - Quit" << endl;

		cin >> response;
		
		switch (response){
			case 'a':
			case 'A':
				cout << "How many numbers do you want to enter?" << endl;
				cin >> num;
				cout << "Start entering numbers." << endl;
				for (int count = 0; count < num; count++){
					cin >> newNumber;
					if (newNumber > max)
						max = newNumber;
				}
				cout << "The largest number is: " << max << endl;
				break;
			case 'b':
			case 'B':
				cout << "Start entering numbers. -99 to quit" << endl;
				do{
					cin >> newNumber;
					if ((newNumber < min) && (newNumber != -99))
						min = newNumber;
				}while (newNumber != -99);
				cout << "The smallest number is: " << min << endl;
				break;
		}
	} while ((response != 'C') && (response != 'c'));

	return 0;
}

now the steps i took to write this code was:

1. do{
2. Present Menu
3. Get User Choice
4. Switch statement -> If user choice matches case A, go to 5, if it matches case B, go to 11, if C go to 18
5. Ask how many numbers they want to enter
6. Run for loop. If number of times is less than or equal to user entry, go to step 7, else go to step 10
7. Ask user for input
8. Calculate
9. Go back to 6
10. break to line 17
11. Run while loop
12. Get user entry
13. If user entry is not -99, calculate
14. If user entry is -99, go to line 16
15. Go to step 11
16. break to line 17
17. Loop back to line 1
18. end program

And the next step I need to take steps 6-9 above, create a function for what it is doing and pass the data gathered in step 5 above as a parameter. But honestly lost in exactly how to start the process.... I am not asking for all the answers but some help to get started.

Recommended Answers

All 11 Replies

Do you know arrays?

just now starting to learn them.. dont understand them to well

Need to Modify my codes 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.

An array would be the easiest way to handle this. Load the numbers in the array, pass the array and the number of values to the function.

The function for B should have no parameters and return the smallest number.

Maybe for this one you have to read the values in the function.

unfortuantly we are not allowed to use arrays in your codes just yet... is there another way to do this?

something like this perhaps?

#include <iostream>

inline int larger( int a, int b ) { return a>b ? a : b ; }
inline int smaller( int a, int b ) { return a<b ? a : b ; }

int main()
{
  int number ;
  std::cin >> number ;
  int largest = number, smallest = number ;
  while( (std::cin>>number) && (number!=-99) )
  {
    largest = larger( largest, number ) ;
    smallest = smaller( smallest, number ) ;
  }
  std::cout << "largest: " << largest
      << "  smallest: " << smallest << '\n' ;
}

I am not exactly sure how that would fit into the code i already have started...

// ...
switch (response){
  case 'a':
  case 'A':
    cout << "How many numbers do you want to enter?" << endl;
    cin >> num;
    cout << "Start entering numbers." << endl;
    cin >> newNumber ;
    max = newNumber ; 
    for (int count = 1; count < num; count++){
      cin >> newNumber;
      max = larger( max, newNumber ) ;
    }
    cout << "The largest number is: " << max << endl;
    break;
  case 'b':
  case 'B':
    cout << "Start entering numbers. -99 to quit" << endl;
    cin >> newNumber ;
    min = newNumber ; 
    while ( ( cin >> newNumber) && (newNumber != -99)){
        max = smaller( min, newNumber ) ; 			   
    };
    cout << "The smallest number is: " << min << endl;
    break;
// ...

so in this code you have displayed....

#include <iostream>

inline int larger( int a, int b ) { return a>b ? a : b ; }
inline int smaller( int a, int b ) { return a<b ? a : b ; }

int main()
{
  int number ;
  std::cin >> number ;
  int largest = number, smallest = number ;
  while( (std::cin>>number) && (number!=-99) )
  {
    largest = larger( largest, number ) ;
    smallest = smaller( smallest, number ) ;
}

The first part of it would have to be up near the top of my old code...

int main()
{
  int number ;
  std::cin >> number ;
  int largest = number, smallest = number ;
  while( (std::cin>>number) && (number!=-99) )

and towards the middle would be the rest of the code....

{
    largest = larger( largest, number ) ;
    smallest = smaller( smallest, number ) ;
  }
  std::cout << "largest: " << largest
      << "  smallest: " << smallest << '\n' ;

Is that about right? if not then i am still lost

so in this code you have displayed....

...

The first part of it would have to be up near the top of my old code...

...

Is that about right? if not then i am still lost

This is why you should not post code without explaining what you're doing. The OP rarely has an idea how to cut and paste your code into his project and have it work.

> The OP rarely has an idea how to cut and paste your code into his project and have it work.
my idea was not to have the OP cut and paste my code. merely that it should give him a pointer in the right direction.

actually i was not copy and pastuing the code... was merely asking to get an idea on formation and break down of the code. I know with the addition of function the program will not look diffrent to the user but just orginzes the code of the program its self so only person to really see the function is the programmer and not the user that is using the program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.