User-Defined Function - setup right?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

User-Defined Function - setup right?

 
0
  #1
Apr 3rd, 2007
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

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

What is the output of the following function calls?

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

Here is my code:

  
  1. #include <iostream>
  2. using namespace std ;
  3.  
  4. void defaultParam ( int u , int v = 5 , double z = 3.2 ) ;
  5. int main ()
  6. {
  7. defaultParam(6) ;
  8. defaultParam(3 , 4) ;
  9. defaultParam(3 , 0 , 2.8) ;
  10.  
  11. return 0 ;
  12. }
  13.  
  14. void defaultParam ( int u , int v , double z )
  15. {
  16. int a ;
  17. u = u + static_cast<int>( 2 * v + z ) ;
  18. a = u + v * z ;
  19. cout << "a = " << a << endl ;
  20. }


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:?
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: User-Defined Function - setup right?

 
0
  #2
Apr 3rd, 2007
Your desk-check of the first call (6) is wrong.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - setup right?

 
0
  #3
Apr 3rd, 2007
hmm. is it 52?
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: User-Defined Function - setup right?

 
0
  #4
Apr 4th, 2007
Of course not....
  1. void defaultParam ( int u , int v = 5 , double z = 3.2 )
  2. {
  3. int a ;
  4. u = u + static_cast<int>( 2 * v + z ) ;
  5. a = u + v * z ;
  6. cout << "a = " << a << endl ;
  7. }

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!
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 1,169
Reputation: Duki has a spectacular aura about Duki has a spectacular aura about Duki has a spectacular aura about 
Solved Threads: 9
Duki's Avatar
Duki Duki is offline Offline
Veteran Poster

Re: User-Defined Function - setup right?

 
0
  #5
Apr 4th, 2007
oh wow, i've been multiplying 2*v*z. sheesh

thanks
It is practically impossible to teach good programming style to students that have had prior exposure to Basic; as potential programmers they are mentally mutilated beyond hope of regeneration.

-Edsger Dijkstra
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC