Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,494 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,053 people online right now. Registration is fast and FREE... Join Now!




Functions and Parameters

2 Pages V  1 2 >  
Reply to this topicStart new topic

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

Vision_Dreamer
post 30 Jun, 2008 - 06:52 PM
Post #1


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

CODE
#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 Jun, 2008 - 06:55 PM
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 30 Jun, 2008 - 07:46 PM
Post #2


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Einherjar
post 30 Jun, 2008 - 07:56 PM
Post #3


D.I.C Head

**
Joined: 10 Feb, 2008
Posts: 73


My Contributions


Or something like:

CODE

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


That whole thing is a function.
User is offlineProfile CardPM

Go to the top of the page

Vision_Dreamer
post 1 Jul, 2008 - 01:49 AM
Post #4


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

Thanks so much!
User is offlineProfile CardPM

Go to the top of the page

Vision_Dreamer
post 1 Jul, 2008 - 01:58 AM
Post #5


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

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?
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 1 Jul, 2008 - 02:59 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,433



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


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

It will return the largest of the two.
Here's an example:
cpp
#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 smile.gif
User is offlineProfile CardPM

Go to the top of the page

Vision_Dreamer
post 1 Jul, 2008 - 01:09 PM
Post #7


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

I think I am hopeless! lol. BAH! I'll get this if it's the last thing I do.
User is offlineProfile CardPM

Go to the top of the page

Vision_Dreamer
post 2 Jul, 2008 - 07:19 PM
Post #8


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

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

CODE
#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;
}
User is offlineProfile CardPM

Go to the top of the page

tortillaboy05
post 2 Jul, 2008 - 07:41 PM
Post #9


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 20


My Contributions


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: 2 Jul, 2008 - 08:04 PM
User is offlineProfile CardPM

Go to the top of the page

Vision_Dreamer
post 3 Jul, 2008 - 06:30 AM
Post #10


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

GOT IT TO WORK!!!!!

CODE
#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;
}
User is offlineProfile CardPM

Go to the top of the page

tortillaboy05
post 3 Jul, 2008 - 07:27 AM
Post #11


New D.I.C Head

*
Joined: 2 Jul, 2008
Posts: 20


My Contributions


Great job! Now your functions need to work like the problem asks you.
The two functions largest and smallest need to return int values:
CODE

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:
CODE

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:
CODE

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: 3 Jul, 2008 - 08:35 AM
User is offlineProfile CardPM

Go to the top of the page

Vision_Dreamer
post 3 Jul, 2008 - 08:40 AM
Post #12


New D.I.C Head

*
Joined: 27 Jun, 2008
Posts: 11

only problem is I have to use void because I need two different types of functions.
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/22/08 04:57PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month