Functions and Parameters

I'm lost with functions and parameters, need any help possible.

Page 1 of 1

12 Replies - 1753 Views - Last Post: 03 July 2008 - 10:00 AM Rate Topic: -----

#1 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Functions and Parameters

Posted 30 June 2008 - 07:52 PM

#include <cctype>
#include <iostream>

using namespace std;

int largest();
void smallest();
char options();


int main()
{
	char choice;

	do
	{
		choice = options();

		switch (choice)
		{
			case 'a': largest(); break; 
			case 'A': largest(); break; 
			case 'b': smallest(); break;
			case 'B': smallest(); break;
		}

	} while ((choice != 'C') && (choice != 'c'));

	return 0;
}

char options()
{
	char choice;
		cout << endl;
		cout << "Enter 'A' to find the largest number with a known quantity of numbers." << endl;
		cout << "Enter 'B' to find the smallest number with an unknown quantity of numbers." << endl;	
		cout << "Enter 'C' to Quit." << endl;
		cout << endl;		
		cout << "Please enter your choice: ";
		cout << endl;
		cin >> choice;
		return choice;
}

 int largest()
{
	int largest = -9999999;
	int numbers = 0, input, C = 0;

	cout << endl;
	cout << "How many numbers would you like to enter? ";
	cin >> numbers;

	for (C = 0; C < numbers; C++)
	{
		cout << "Please Enter Number " << C+1 << ": ";
		cin >> input;
		if (input > largest)
			largest = input;
	}

	cout << endl;
	cout << "The Largest number is " << largest << "." << endl;
return numbers;
}

void smallest()
{
	int smallest = 9999999;
	int input1 = 0, C1=1;
	cout << endl;
 
 			cout << "Enter as many numbers as you want." <<endl; 
			cout << "When you wish to stop entering numbers, enter -99." << endl;   
			cout << endl;
		 
	do
	{
		cout << "Please enter number " << C1 << ": ";
		cin >> input1;
		C1++;
		if ((input1 < smallest) && (input1 != -99))
		smallest = input1;
	} while (input1 != -99);

	cout << endl;
	cout << "The Smallest number is " << smallest << "." << endl;
}


My Teacher says that I need two functions and parameters in "int largest" I thought I did have these but apparently I do not. He also said that I need two return functions? I am lost? any thoughts?

Specific Guilde lines for homework are:
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.

Also:
The code for case 'A' and 'B' now needs to be a function call rather than part of the "main" program. You need to accomplish the following:
1) Start with a working week 5 program
2) Declare the function "largest" at the top of the program. You week 5 program will stay as is till the point that the user once you are in the switch selects A and enters the number he/she wants to enter. At that point you will call largest. Notice that the function that we called "largest" should be declared at the top of the file (your program) but the body of the function is at the end of the file.
3) "The function for B should have no parameters and return the smallest number". Having successfully completed the function for case 'A', this should be a piece of cake. For case 'B' no parameter is passed so the declaration of the function at the top will look like this: int smallest(); Once the user selects B you just make the call to smallest and the code that used to be in main gets executed.

Please note that there is a solution floating on the web that is using functions. That solution will not apply to what we are doing this week because the functions that they created are different than what we expect from you here.
-------------------------------------------------------------------------------
In advance thanks for whatever help you can give me.

This post has been edited by Vision_Dreamer: 30 June 2008 - 07:55 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Functions and Parameters

#2 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: Functions and Parameters

Posted 30 June 2008 - 08:46 PM

It seems that he/she basically wants you to ask the question of how many numbers the user is to enter *OUTSIDE* of the function largest....


I.e.

Ask the user how many numbers that he/she is going to enter in main.
The result is stored in some variable, (for example, it could be int number OfNumbers).

Call the largest function using numberOfNumbers as a parameter, i.e.
int maxNum;
maxNum = largest(numberOfNumbers);

Then, in the largest function, ask the user to input each number.
Was This Post Helpful? 0
  • +
  • -

#3 Einherjar  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 73
  • Joined: 10-February 08

Re: Functions and Parameters

Posted 30 June 2008 - 08:56 PM

Or something like:

int largest[b](int a, int b)[/b] - those are parameters
{
   if(a > b)
	  return a;
   return b;
}



That whole thing is a function.
Was This Post Helpful? 0
  • +
  • -

#4 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Re: Functions and Parameters

Posted 01 July 2008 - 02:49 AM

Thanks so much!
Was This Post Helpful? 0
  • +
  • -

#5 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Re: Functions and Parameters

Posted 01 July 2008 - 02:58 AM

ok so I tried what you said and I am getting errors. Where exactly is int maxNum and maxNum = Largest(numberofnumbers)? I've put them everywhere?
Was This Post Helpful? 0
  • +
  • -

#6 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: Functions and Parameters

Posted 01 July 2008 - 03:59 AM

You need to pass two parameters to the function... a and b.

It will return the largest of the two.
Here's an example:
#include <iostream>

using namespace std;

int largest (int x, int y); // function declaration, we define it later

int main ()
{
    int c = 4;
    int d = 500;
    int biggest = largest (c,d); // pass c and d
    // biggest becomes the return value of the function,
    // which will be the largest of the two passed

    cout << biggest; // just to see what happens

    cin.get (); // pause
    return EXIT_SUCCESS; // program was executed successfully
}

int largest(int x, int y)
{
   if(x > y)
      return x;
   return y;
}

Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#7 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Re: Functions and Parameters

Posted 01 July 2008 - 02:09 PM

I think I am hopeless! lol. BAH! I'll get this if it's the last thing I do.
Was This Post Helpful? 0
  • +
  • -

#8 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Re: Functions and Parameters

Posted 02 July 2008 - 08:19 PM

So this is what I got and I still can't get it to work? Is it syntax or stupidity?

 #include <cctype>
#include <iostream>

using namespace std;

void largest(int largest, int numbers);
void smallest();
char options();


int main()
{
	char choice;
	int largest = -9999999;
	int numbers = 0; 
	do
	{
		choice = options();

		switch (choice)
		{
			case 'a': void largest(int largest, int numbers); break; 
			case 'A': void largest(int largest, int numbers); break; 
			case 'b': smallest(); break;
			case 'B': smallest(); break;
		}

	} while ((choice != 'C') && (choice != 'c'));

	return 0;
}

char options()
{
	char choice;
		cout << endl;
		cout << "Enter 'A' to find the largest number with a known quantity of numbers." << endl;
		cout << "Enter 'B' to find the smallest number with an unknown quantity of numbers." << endl;	
		cout << "Enter 'C' to Quit." << endl;
		cout << endl;		
		cout << "Please enter your choice: ";
		cout << endl;
		cin >> choice;
		return choice;
}

void largest(int largest, int numbers)
{
int input;
char C;
C = 0;
		cout << "How many numbers would you like to enter? ";
		cin >> numbers;
	
	for (C = 0; C < numbers; C++)
	{
		cout << "Please Enter Number " << C+1 << ": ";
		cin >> input;
		if (input > largest)
			largest = input;
	}

	cout << endl;
	cout << "The Largest number is " << largest << "." << endl;
return;
}

void smallest()
{
	int smallest = 9999999;
	int input1 = 0, C1=1;
	cout << endl;

			cout << "Enter as many numbers as you want." <<endl; 
			cout << "When you wish to stop entering numbers, enter -99." << endl;   
			cout << endl;
		 
	do
	{
		cout << "Please enter number " << C1 << ": ";
		cin >> input1;
		C1++;
		if ((input1 < smallest) && (input1 != -99))
		smallest = input1;
	} while (input1 != -99);

	cout << endl;
	cout << "The Smallest number is " << smallest << "." << endl;
return;
}

Was This Post Helpful? 0
  • +
  • -

#9 tortillaboy05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 08

Re: Functions and Parameters

Posted 02 July 2008 - 08:41 PM

First thing i see is your function prototype for the largest function. You don't name your input parameters in the prototype, you just declare the type, like this:

void largest(int, int);

then when you call the function you don't need the type, should look like this:

largest(largest, number);

I don't know for sure but the name of the function and the name of the variable being the same might cause a problem. Maybe call the first variable largestNum or something.

In the actual function void largest:

you're declaring "C" to be a char then trying to assign and int to it in your loop. Actually you don't even need to declare it outside the loop unless you need it else where. I don't know if your teacher has talked about scope yet or not, but you can declare the variable inside the loop like this:

for(int C = 0; C < numbers; C++)

Try that and see what you get. Don't give up keep trying!

edit: oo also noticed something else. Your function smallest needs to return a number (the smallest). The way you have it now it returns nothing. ie void smallest(); void means your not returning anything. it should be int smallest(); I see what you did but that isn't what the problem is asking for. If your teacher is picky then you might want to change it.

Another thing in your smallest function. You don't need to initialize smallest to 999999999 or whatever. Instead do an inital read BEFORE the loop and store that number into smallest. Then the loop will start and ask them for the next number and it will compare the two like it should.

I see something else too but it pertains to what the assignment is asking for and it won't keep the program from running correctly. Post back once you get it running and I'll try to help you understand a little bit more about functions.

This post has been edited by tortillaboy05: 02 July 2008 - 09:04 PM

Was This Post Helpful? 0
  • +
  • -

#10 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Re: Functions and Parameters

Posted 03 July 2008 - 07:30 AM

GOT IT TO WORK!!!!!

 #include <cctype>
#include <iostream>

using namespace std;

void Largestnum(int, int);
void smallest();
char options();


int main()
{
	char choice;
	int largest = -9999999;
	int numbers = 0; 
	do
	{
		choice = options();

		switch (choice)
		{
			case 'a':	  
		cout << "How many numbers would you like to enter? ";
		cin >> numbers; 
		Largestnum(largest, numbers);
		break;
			case 'A':		   
		cout << "How many numbers would you like to enter? ";
		cin >> numbers; 
		Largestnum(largest, numbers);
		break;
			case 'b': smallest(); break;
			case 'B': smallest(); break;
		}

	} while ((choice != 'C') && (choice != 'c'));

	return 0;
}

char options()
{
	char choice;
		cout << endl;
		cout << "Enter 'A' to find the largest number with a known quantity of numbers." << endl;
		cout << "Enter 'B' to find the smallest number with an unknown quantity of numbers." << endl;	
		cout << "Enter 'C' to Quit." << endl;
		cout << endl;		
		cout << "Please enter your choice: ";
		cout << endl;
		cin >> choice;
		return choice;
}

void Largestnum(int largest, int numbers)
{
 int C;
 int input; 
	for (C = 0; C < numbers; C++)
	{
		cout << "Please Enter Number " << C+1 << ": ";
		cin >> input;
		if (input > largest)
			largest = input;
	}

	cout << endl;
	cout << "The Largest number is " << largest << "." << endl;
return;
}

void smallest()
{
	int smallest = 9999999;
	int input1 = 0, C1=1;
	cout << endl;

			cout << "Enter as many numbers as you want." <<endl; 
			cout << "When you wish to stop entering numbers, enter -99." << endl;   
			cout << endl;
		 
	do
	{
		cout << "Please enter number " << C1 << ": ";
		cin >> input1;
		C1++;
		if ((input1 < smallest) && (input1 != -99))
		smallest = input1;
	} while (input1 != -99);

	cout << endl;
	cout << "The Smallest number is " << smallest << "." << endl;
return;
}

Was This Post Helpful? 0
  • +
  • -

#11 tortillaboy05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 08

Re: Functions and Parameters

Posted 03 July 2008 - 08:27 AM

Great job! Now your functions need to work like the problem asks you.
The two functions largest and smallest need to return int values:
int largest(int, int);	  //prototypes
int smallest();



When your functions return values back to main you need to assign it to a variable. For instance declare a variable of the type that your returning, in your case an int. Such as:
int maxNum;

int main()
{
	maxNum = Largest(largest, numbers); //call
}


the number returned from the function is assigned to maxNum.

Then in your function, delete the cout where you display largest, and instead pass the number you want to send back to main:
return largest;


In effect what you are doing is making a copy of largest and putting it into the memory where maxNum is stored. One thing to note is that the variable names do not need to be the same. Once your program leaves your function, largest in that function is actually deleted, but since you returned the value back to main it is saved into maxNum. This way your functions can communicate back with main. Now that you have the largest number stored in maxNum, just print it there in main.

Your smallest function should do the same thing, I got you started up at the top with the prototype of what smallest should look like.

Your teacher is trying to teach you how to pass to and return values from functions and the way you did it kind of bypassed what he was trying to do by making these functions void and then printing from them. Learning how functions work is very very important, so try to figure out exactly what is happening when your program is running.

If you want shoot me an email, I have a file i could send you to reference. em1189@txstate.edu

This post has been edited by tortillaboy05: 03 July 2008 - 09:35 AM

Was This Post Helpful? 0
  • +
  • -

#12 Vision_Dreamer  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 27-June 08

Re: Functions and Parameters

Posted 03 July 2008 - 09:40 AM

only problem is I have to use void because I need two different types of functions.
Was This Post Helpful? 0
  • +
  • -

#13 tortillaboy05  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 02-July 08

Re: Functions and Parameters

Posted 03 July 2008 - 10:00 AM

View PostVision_Dreamer, on 30 Jun, 2008 - 07:52 PM, said:

Specific Guilde lines for homework are:
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.


Look here, function 'A' needs to return the largest number which is an int, function 'B' should return the smallest number which is also an int. Therefore:

int largest(....);
int smallest();
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1