Hey all,

I'm doing hw, and I did a desk-check for this program and got completely different answers than that of the computer when the program was put into the compiler.

The question says to consider the function defaultParam

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 is the output of the following function calls?

defaultParam(6) ;
defaultParam(3, 4) ;
defaultParam(3, 0, 2.8) ;

Here is my code:

#include <iostream>
using namespace std ;

void defaultParam ( int u , int v = 5 , double z = 3.2 ) ;
int main ()
{
    defaultParam(6) ;
    defaultParam(3 , 4) ;
    defaultParam(3 , 0 , 2.8) ;

    return 0 ;
}

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

My output from this code is:

a = 35
a = 26
a = 5
Press any key to continue . . .

but when I did the desk-checking I got:

50
54.2
3

Is my program setup to give me the correct results? Or am I right? Or neither :mrgreen:?

Recommended Answers

All 4 Replies

Your desk-check of the first call (6) is wrong.

hmm. is it 52?

Of course not....

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 ;
}

defaultParam(6) ;

What's u, v, and z?

Now what's u + (( 2 * v ) + z ) ? Replace u with this value -- as an integer

Now what's u + (v * z) , also as an integer after calculating.

Are the lack of parentheses in the original equations throwing you? If so, prime reasong to paranthesize everything!

oh wow, i've been multiplying 2*v*z. sheesh

thanks

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.