10 Replies - 1387 Views - Last Post: 02 December 2012 - 10:06 AM Rate Topic: -----

#1 SalGiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-November 12

Arguments in a function

Posted 30 November 2012 - 11:50 AM

My problem is with the "default argument of 2 for p" part of this question:
Posted Image

Here's my code:
#include<iostream>
using namespace std;
double power(double, int);
int main(){
    double n;
    int p;
    double ans;
    cout<<"Enter the number first and then the power to which it is to be raised: \n";
    cin>>n>>p;
    ans = power(n,p);
    cout<<n<<" raised to the power "<<p<< " is "<<ans<<endl;
    system("pause");
    return 0;
}
double power(double n, int p)
{
       double total = 1;
       for(int i=1;i<=p;i++)
       {
             total = total*n;}  
       if(p<0)
       {return n*n;}
       else
       return total;
       }


The code works fine and gives the correct results but I have no idea how the value of p can be omitted by the user? I mean that is there any way in which the user can NOT enter the value of p with the code that I've made? Also, how should I modify the "if" part in my function so that my code is the correct answer to the question that I have posted?

Any help would be appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Arguments in a function

#2 Mrk  Icon User is offline

  • D.I.C Head

Reputation: 29
  • View blog
  • Posts: 66
  • Joined: 03-December 09

Re: Arguments in a function

Posted 30 November 2012 - 12:33 PM

In your function prototype, you can place a default argument to be used if the user doesn't input that argument.
For example:
double power(double, int = 2);

Now 2 will be used as a default argument if one is not provided by the user. But keep in mind that omitted arguments must be on the end of the list or the compiler won't be able to tell which arguments were left out.

This post has been edited by Mrk: 30 November 2012 - 12:34 PM

Was This Post Helpful? 0
  • +
  • -

#3 blackcompe  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1152
  • View blog
  • Posts: 2,530
  • Joined: 05-May 05

Re: Arguments in a function

Posted 30 November 2012 - 12:34 PM

Have you searched for "C++ default function arguments"? I'm sure you'll find plenty of sources that will answer your question.
Was This Post Helpful? 0
  • +
  • -

#4 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Arguments in a function

Posted 30 November 2012 - 12:39 PM

Well! There could be many ways to do that. The first you might come up with is that ask the user whether he/she is going to enter the value for p and put on some conditions...

It would be really up to what you are intended to do. BTW, your whole program is using the value of p, why omit it then?

Alternatively, you can read the inputs as a string, maybe separated by space and use std::stringstream to hold it and put them back into int type variables...

And for the function, you can use a default parameter and check the default value in the body which would mean that the value is not passed. Or overload the function...

Hope this Helps!
Was This Post Helpful? 0
  • +
  • -

#5 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Arguments in a function

Posted 30 November 2012 - 05:27 PM

double power(double n, int p)
{
       double total = 1;
       for(int i=1;i<=p;i++)
       {
             total = total*n;}  
       if(p<0)
       {return n*n;}
       else
       return total;
       }



Lets take a look at your logic here. What would the result of the following function call

power(2, -2)

By your algorithm, the if statement condition p < 0 would evaluate to true, and therefore you would return 2 * 2 = 4...... 4 does not equal 2 ^ -2.

Here are three basic "properties" that you can use for your power function

n ^ 0 = 1
n ^ -p = 1 / (n ^ p)
Was This Post Helpful? 0
  • +
  • -

#6 SalGiki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 30-November 12

Re: Arguments in a function

Posted 01 December 2012 - 09:04 PM

View PostMrk, on 30 November 2012 - 12:33 PM, said:

In your function prototype, you can place a default argument to be used if the user doesn't input that argument.
For example:
double power(double, int = 2);

Now 2 will be used as a default argument if one is not provided by the user. But keep in mind that omitted arguments must be on the end of the list or the compiler won't be able to tell which arguments were left out.


Got that, but how can I make user not enter the value of p? In my main function, I have put
cin>>p
. So isn't it mandatory for user to give a value of p?

View Postblackcompe, on 30 November 2012 - 12:34 PM, said:

Have you searched for "C++ default function arguments"? I'm sure you'll find plenty of sources that will answer your question.


Sorry, searched for it but couldn't find any helpful topics.

View PostAKMafia001, on 30 November 2012 - 12:39 PM, said:

Well! There could be many ways to do that. The first you might come up with is that ask the user whether he/she is going to enter the value for p and put on some conditions...

It would be really up to what you are intended to do. BTW, your whole program is using the value of p, why omit it then?

That is the problem. How can I have the user NOT enter the value of p?

View PostAKMafia001, on 30 November 2012 - 12:39 PM, said:

Alternatively, you can read the inputs as a string, maybe separated by space and use std::stringstream to hold it and put them back into int type variables...

Haven't done 'strings' yet in my CS class.

View PostAKMafia001, on 30 November 2012 - 12:39 PM, said:

And for the function, you can use a default parameter and check the default value in the body which would mean that the value is not passed. Or overload the function...

Hope this Helps!

For default parameter, do you mean doing this to the prototype?:
double power(double, int = 2);


As for overloading, I have been taught that overloading is to create the functions with the same names as original one but with different type of variables. Please give me any hints on how can that help me to make user OMIT the value?


View Postjjl, on 30 November 2012 - 05:27 PM, said:

double power(double n, int p)
{
       double total = 1;
       for(int i=1;i<=p;i++)
       {
             total = total*n;}  
       if(p<0)
       {return n*n;}
       else
       return total;
       }



Lets take a look at your logic here. What would the result of the following function call

power(2, -2)

By your algorithm, the if statement condition p < 0 would evaluate to true, and therefore you would return 2 * 2 = 4...... 4 does not equal 2 ^ -2.

Here are three basic "properties" that you can use for your power function

n ^ 0 = 1
n ^ -p = 1 / (n ^ p)


I just put p<0 so that the program could run correctly for +ve values of p. I have removed it now.

My modified code(with the prototype modified):
#include<iostream>
using namespace std;
double power(double, int p =2);
int main(){
    double n;
    int p;
    double ans;
    cout<<"Enter the number first and then the power to which it is to be raised: \n";
    cin>>n>>p;
    ans = power(n,p);
    cout<<n<<" raised to the power "<<p<< " is "<<ans<<endl;
    system("pause");
    return 0;
}
double power(double n, int p)
{
       double total = 1;
       for(int i=1;i<=p;i++)
       {
             total = total*n;}  
       return total;
       }
                 

Was This Post Helpful? 0
  • +
  • -

#7 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Arguments in a function

Posted 02 December 2012 - 03:14 AM

Not just with the prototype, you need your declaration and implementation to match... Also, remember that default parameters should be at the end of the parameter list...

So, you can call your functions in two ways:
power(2, 3);   // provide both arguments
power(2);      // leave out the default parameter, the default value will be considered



The question is, why you want the exponent to be optional input? Is it that you don't need to calculate for negative exponent value?

Well, do you need something like:
cin >> p;

if(p < 0)
    power(n);
else
    power(n, p);

//...
// power function
double power(double n, int p = 0) {
    if(p < 0)
        return 0.0;    
    else if(p == 0)
        return 1.0;
    else
        // calculate power
}



Well! You need to be more clear of why you want the power to be optional...

Here are optional reading of default parameters and function overloading...
Was This Post Helpful? 0
  • +
  • -

#8 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Arguments in a function

Posted 02 December 2012 - 09:43 AM

I think the OP wants the end-user of the application to be able to omit entering any value for p.
You could do that like this:
std::string line;
std::getline(std::cin, line);
//possibly trim the string (remove whitespace)
if (line.empty()) { // user jsut pressed enter
  // handle it
} else {
  // ... use stringstream to get things out of the "line"?
}

Of course if you used std::cin::operator>> before std::getline(), you may want to clear the std::cin's buffer (using std::istream::ignore()).

This post has been edited by Xupicor: 02 December 2012 - 09:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Arguments in a function

Posted 02 December 2012 - 09:50 AM

@Xupicor

View PostAKMafia001, on 01 December 2012 - 12:39 AM, said:

Alternatively, you can read the inputs as a string, maybe separated by space and use std::stringstream to hold it and put them back into int type variables...


View PostSalGiki, on 02 December 2012 - 09:04 AM, said:

Haven't done 'strings' yet in my CS class.

Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg  Icon User is online

  • member icon


Reputation: 4013
  • View blog
  • Posts: 12,392
  • Joined: 25-December 09

Re: Arguments in a function

Posted 02 December 2012 - 10:01 AM

Then use getline() using a C-string then check the length of this string. If it is greater than 0, use atoi() to convert this string to a number and call the function with both parameters, otherwise call the function with one argument. You will need to include the <cstring> header to use the strlen() function and <cstdlib> to use the atoi() function. And be sure to ignore() the end of line character left in the input buffer by the preceding cin.

Jim

This post has been edited by jimblumberg: 02 December 2012 - 10:03 AM
Reason for edit:: Added link

Was This Post Helpful? 1
  • +
  • -

#11 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 249
  • View blog
  • Posts: 582
  • Joined: 31-May 11

Re: Arguments in a function

Posted 02 December 2012 - 10:06 AM

@AKMafia001
First quote - two very different things we're talking about. You - emulating default values of function parameters using a string parameter which would be then parsed; me - letting the end-user (not the programmer) skip some values on input (i.e. when interacting with the application).

As for the "I haven't learn that yet, therefore I can't use it", I already voiced my opinion on that matter I believe, at least once.
I'd just learn about them whether they are or not to be on the course, and then make two versions of the program. One using strings, and one using character arrays, just in case the teacher is not happy about using fancy things not covered yet.

This post has been edited by Xupicor: 02 December 2012 - 10:08 AM

Was This Post Helpful? 2
  • +
  • -

Page 1 of 1