# user defined functions

Page 1 of 1

## 2 Replies - 936 Views - Last Post: 15 February 2009 - 03:58 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=86861&amp;s=5c8f8bcae01a25ed4485c6ba78f921b1&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 qazwsxed22

Reputation: 0
• Posts: 11
• Joined: 04-February 09

# user defined functions

Posted 15 February 2009 - 03:41 PM

```#include <iostream>

using namespace std;

const int SENTINEL = -99;
int greatest(int i);
int smallest();

int main ()
{
// Declaring variables
char letter;	//letter chosen from the menu
int i;			//quantity of numbers user will enter
int counter;	//loop control variables
int num;	 // variable to store new number
int largest;	 // variable to store largest number
int least;

// Initializing variables
largest = -9999999;
least = 9999999;
// Big loop here for the condition of the Application running
do
{
cout << "Please Select One of the Following Choices" << endl;
cout << "A: Find the largest number with a known quantity of number " << endl;
cout << "B: Find the smallest number with an unknown quantity of number " << endl;
cout << "C: Quit" << endl;
cin >> letter;
cout << endl;

switch (letter)
{
case 'A':
case 'a':
cout << "How many numbers would you like to enter"<< " ";
cin >> i;
cout << endl;
cin >> num;
cout << "The largest number you entered is" << " " << greatest(i);
cout << " ";
cout << endl;
break;

case 'B':
case 'b':
cout << "Please enter your numbers with your last number being" << " " << SENTINEL << endl;
cin >> num;
while (num != SENTINEL)
{
cout << smallest();
}
cout << "The smallest number you entered is" << " " << smallest() << endl;
}
}
while (letter != 'C' && letter != 'c');

cout << endl;

return 0;
}
int Greatest(int i)

{
int large;
int num;
int count;
large = -999999;	  // Initialize final result variable to largest number possible
for (count = 0, count <= i, count++;)	  // start a loop, increment from 0 to count
{
cin << num;//	   Read next number
if
num > large  //	   Compare number to final result variable
large = num;   //	   if number is greater than final result variable, then
//	   store in final result variable

}	 // end of loop
return large;//   return final result
}// end of function greatest

smallest()
{
least = 9999999   // Initialize final result variable to smallest number possible

int num, int least; // Declare all variables
while (number != SENTINEL)
{

if (num < least)   //code for smallest goes here
least = num;
cin >> num;
}

return  least;

}

```

This post has been edited by qazwsxed22: 15 February 2009 - 05:19 PM

Is This A Good Question/Topic? 0

## Replies To: user defined functions

### #2 KYA

• Wubba lubba dub dub!

Reputation: 3207
• Posts: 19,239
• Joined: 14-September 07

## Re: user defined functions

Posted 15 February 2009 - 03:51 PM

Two I can see with a quick glance:

```if
num > large

//needs to be

if( num > large )

```

Another:

```int smallest() //function need return identifier
{
least = 9999999   // Initialize final result variable to smallest number possible

int num, least; //only need 'int' once
while (number != SENTINEL)
{

if (num < least)   //code for smallest goes here
least = num;
cin >> num;
}
return  least;
}

```

This post has been edited by KYA: 15 February 2009 - 03:51 PM

### #3 bszmyd

Reputation: 15
• Posts: 104
• Joined: 09-February 09

## Re: user defined functions

Posted 15 February 2009 - 03:58 PM

qazwsxed22, on 15 Feb, 2009 - 02:41 PM, said:

```case 'A':
case 'a':
cout << "How many numbers would you like to enter"<< " ";
cin >> i;
cout << endl;
cin >> num;
cout << "The largest number you entered is" << " " << greatest(i);
```

Here your only reading in one number into num...you need to do a for loop from 0 to i-1 each time reading into an array of numbers from cin. Declare num like: int num[MAX_NUMBERS] Where MAX_NUMBERS is some constant. Then in your case statement:
```   cout << "How many numbers would you like to enter"<< " ";
cin >> i;
cout << endl;
for (int x = 0; i < x; ++x )
{
cout << "Please enter a number" << endl;
cin >> num[x];
}
```

You also are passing only the number of entries they have to greatest, you probably also need to pass a pointer to the array of numbers they entered. This is also true for smallest. In that case statement you need cin >> num; in the while loop and the call to smallest just outside it. Otherwise, again, you're only reading in one number and printing out the smallest essentially forever.

Take some of these suggestions, fix it up and re-post and we'll continue on.