Tuesday, June 19, 2012

Homework 7: Function Invocations

1.  add is a function that accepts two  int parameters and returns their sum. Two  int variables,  euroSales and  asiaSales , have already been declared and initialized. Another  int variable,  eurasiaSales , has already been declared.Write a statement that calls  add to compute the sum of  euroSales and  asiaSales and store this value in  eurasiaSales .

eurasiaSales = add ( euroSales , asiaSales ) ;

2. toThePowerOf is a function that accepts two  int parameters and returns the value of the first parameter raised to the power of the second. An  int variable  cubeSide has already been declared and initialized. Another  int variable,  cubeVolume , has already been declared. Write a statement that calls  toThePowerOf to compute the value of  cubeSide raised to the power of 3, and store this value in  cubeVolume .

cubeVolume = toThePowerOf ( cubeSide , 3 ) ;

3. max is a function that accepts two  int parameters and returns the value of the larger one. Two  int variables,  population1 and  population2 , have already been declared and initialized. Write an expression (not a statement!) whose value is the larger of  population1 and  population2 by calling  max .


max ( population1 , population2 )

4. max is a function that accepts two  int parameters and returns the value of the larger one. Four  int variables,  population1 ,  population2 ,  population3 , and  population4 have already been declared and initialized. Write an expression (not a statement!) whose value is the largest of  population1 ,  population2 ,  population3 , and  population4 by calling  max . (HINT: you will need to call  max three times and you will need to pass the return values of two of those calls as arguments to  max . REMEMBER: write an expression, not a statement.) 

max ( max ( population1 , population2 ) , max ( population3 , population4 ) )

No comments:

Post a Comment