Friday, February 12, 2016

Visual Basic Chapter 6 Procedures and Functions

6.1 Procedure Invocations
 
 
40073
Write the code for a message whose method name is sendSignal. There are no arguments for this message.  
 
sendSignal()
 
40439
printTodaysDate is a function that accepts no parameters and returns no value.
Write a statement that calls printTodaysDate

printTodaysDate()


40440
printErrorDescription is a function that accepts one int parameter and returns no value.
Write a statement that calls the function printErrorDescription, passing it the value 14.
 
 
printErrorDescription(14)
 
 
40441
printLarger is a function that accepts two int parameters and returns no value.
Two int variables , sales1 and sales2, have already been declared and initialized.
Write a statement that calls printLarger, passing it sales1 and sales2


printLarger(sales1, sales2)


Procedure Definitions
41082
Write the definition of a function named quadratic that receives three double parameters a, b, c. If the value of a is 0 then the function prints the message "no solution for a=0" and returns. If the value of "b squared" – 4ac is negative, then the code prints out the message "no real solutions" and returns. Otherwise the function prints out the largest solution to the quadratic equation. The formula for the solutions to this equation can be found here: Quadratic Equation on Wikipedia
 
 Function quadratic(a as double, b as double, c as double) as double
 Dim x,y as double
 If (a = 0) Then
  System.Console.Writeline("no solution for a=0")
 ElseIf ((b*b - 4*a*c) < 0)
  System.Console.Writeline("no real solutions")
 Else
  x = ((-b + Math.Sqrt(b*b - 4*a*c)) / (2*a))
  y = ((-b - Math.Sqrt(b*b - 4*a*c)) / (2*a))
  If (x > y) Then
   System.Console.Writeline(x)
  Else
   System.Console.Writeline(y)
  End If    End If
 End Function
 
 
6.2 Passing Arguments to Procedures an argument to a procedure
 
40074
  Write the code for a message whose method name is sendNumber. There is one argument for this message, which is an int. Send the number 5 as an argument in the message. 
 
sendNumber( 5)
 
40075
Write the code for a message whose method name is sendVariable. There is one argument for this message, which is an int.
Suppose an int variable  called x has been declared and initialized to some value. Send this variable  in your message.
 
 
sendVariable (x)
 
40076
 Write the code for a message whose method name is sendTwo. There are two arguments for this message: a double and an int. Send the Double value of 15.955 and the Integer value of 133 in the message. 
 
 
sendTwo(15.955, 133)
 
40077
Write the code for a message whose method name is sendObject. There is one argument for this message, which is of type Customer.
Suppose there is an object of type Customer, called John_Doe. Send this object within your message.
 
 
 sendObject(John_Doe)
 
 
 
6.3 Functions
 
Function invocations
 
 
40442
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)
 
40443
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)
 
40804
If a right triangle has sides of length A, B and C and if C is the largest, then it is called the "hypotenuse" and its length is the square root of the sum of the squares of the lengths of the shorter sides (A and B). Assume that variables  a and b have been declared as doubles and that a and b contain the lengths of the shorter sides of a right triangle: write an expression for the length of the hypotenuse.
 
Math.sqrt(Math.pow(a,2) + Math.pow(b,2))
 
 
MPL Extra: composition
 
40445
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))


40801 
 Assume that x is a variable  that has been declared as a double and been given a value.

Write an expression to compute the quartic root of x. The quartic root of a number is the square root of its square root.

EXAMPLES: For example, the quartic root of 16.0 is 2.0 because: the square root of 16.0 is 4.0 and the square root of 4.0 is 2.0. Another example: the quartic root of 81.0 is 3.0 because the square root of 81.0 is 9.0 and the square root of 9.0 is 3.0. Thus, to find the quartic root of a number you take the square root of the number and then take the square root of that.

In this exercise you must find the quartic root of x in a single expression-- you must not write any statements. Also, you may only use the sqrt() function-- no other functions.

(HINT: you will need to call the sqrt() function twice-- and you will need to pass the return value of one of those calls as argument to the other call. AND REMEMBER: write an expression, not a statement.)
 
 
sqrt(sqrt(x))
 
 
 
 
40802
Assume that x is a variable  that has been declared as an int and been given a value. Assume that twice is a function that receives a single integer parameter and returns twice its value. (So if you pass 7 to twice it will return 14. Thus the expression twice(7) has the value 14.

Write an expression whose value is eight times that of x without using the standard C arithmetic operators (*,+, etc.). Instead, use calls to twice to accomplish this.

In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the twice() function-- no other functions or operators.  


twice(twice(twice(x)))
 
40803
 Assume that x is a variable  that has been declared as an int and been given a value. Assume that oneMore is a function that receives a single integer parameter and returns a value that is one greater than its argument. (So if you pass 7 to oneMore it will return 8. Thus, the expression oneMore(9) has the value 10.

Write an expression whose value is four more than x without using the standard C++ arithmetic operators (*,+, etc.). Instead, use calls to oneMore to accomplish this.

In this exercise you must write this as a single expression-- you must not write any statements. Also, you may only use the oneMore function-- no other functions or operators.
 
 
 
 
oneMore(oneMore(oneMore(oneMore(x))))
 
 
 
 
 
 

5 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Great Job, one suggestion however, is on 41082, the last two "End If" statements should be on different lines instead of on the same one. Small thing i noticed

    ReplyDelete