Can someone help me on this?
While considering the following function definition:

void defaultParam(int u, int v = 5, double z = 3.2)
{
     int a;
     u = u + static_cast<int>(2 * v + z);
     a = u + v * z;
     cout << "a = " << a << endl;
}

What does the following output
a)

defaultParam(6);

b)

defaultParam(3,4);

c)

defaultParam(3, 0, 2.8);

I got a) 35 b)28.8 c) 24.96. Is that anywhere close?

Recommended Answers

All 6 Replies

Why don't you just run the code?

I'm not sure how to put it. I have gotten this far but am missing some stuff. I'm mostly concerned about the first one, once I do that I can do the other 2.

#include <iostream>
#include <iomanip>

using namespace std;

void defaultParam(int u, int v = 5, double z = 3.2);

int main()
{
    defaultParam(6);
    cout << defaultParam << endl;
    system ("pause");
    
    return 0;
}

void defaultParam(int u, int v = 5, double z = 3.2)
{
     int a;
     u = u + static_cast<int>(2 * v + z);
     a = u + v * z;
     cout << "a = " << a << endl;
}

Why not get a calculator and do the math on a piece of paper? It'll be substantially quicker than waiting for a forum reply.

>>I got a) 35 b)28.8 c) 24.96. Is that anywhere close?
Quickly doing it in my head, I believe situation "a" is correct.

How can you print an int variable and get a value that is not an integer?

see the function carefully.
void defaultParam(int u, int v = 5, double z = 3.2)
{
int a;
u = u + static_cast<int>(2 * v + z);
a = u + v * z;
cout << "a = " << a << endl;
}
Here you have called 2 integers and a double.
a) defaultParam(6): the function will get value for int u only ; v and z will work with the default values such as 5 and 3.2 respectively.
b) same to a
c) Here u,v and z will get 3,0,2.8.

If you want to print u as an iteger , the you have to type cast u.

My point is that this statement

cout << "a = " << a << endl;

prints the value of a , which is a variable of type int.

That means that it is impossible for this statement to print anything with a decimal point in it. So if you tell me that this statement will print 28.8, I know you're wrong, and I don't even have to look at the rest of 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.