Friday, February 12, 2016

Visual Basic Chapter 8 Arrays and More

8.1 Arrays in a calculation
 
 
40195
 Given an array a, write an expression that refers to the first element of the array.  
 
 
a(0)


40196
 Given an array a, declared to contain 34 elements, write an expression that refers to the last  element of the array. 
 
 a(33)
 
 
40197
 Assume that the array arr has been declared. Write a statement that assigns the next to last   element of the array to the variable  x, which has already been declared. 
 
 
x = arr(arr.length-2)
 
40198
 Given that the array monthSales of integers has already been declared and that its elements contain sales data for the 12 months of the year in order (i.e., January, February, etc.), write a statement that writes to standard output the element corresponding to October. Do not write anything else out to standard output. 
 
Console.Write(monthSales(9))
 
40211
 We informally define the term corresponding element as follows: The first element in an array and the last element of the array are corresponding elements. Similarly, the second element and the element just before the last element are corresponding elements. The third element and the element just before the element just before the last element are corresponding elements -- and so on. Given an array a, write an expression for the corresponding element of a(i).
 
 a(a.length - 1 - i)
 
 
40199
 Given that an array named a with elements of type Integer has been declared, assign 3 to its  first element. 
 
a(0) = 3
 
40200
 Assume that an array named salarySteps whose elements are of type int and that has exactly five elements has already been declared.
Write a single statement to assign the value 30000 to the first element of this array.
 
salarySteps(0) = 30000
 
 
40201
Assume that an array of integers named salarySteps that contains exactly five elements has been declared.
Write a statement that assigns the value 160000 to the last element of the array salarySteps

salarySteps (4)= 160000
 
 
40202
 Assume that an array named a containing exactly 5 integers has been declared and initialized.
Write a single statement that adds 10 to the value stored in the first element of the array. 

a(0) += 10
 
40203
 Given that an array of Integers named a with 30 elements has been declared, assign 5 to its last element. 
 
a(29) = 5
 
40204
  Assume that an array of Integers named a has been declared with 12 elements. The integer variable  k holds a value between 0 and 6. Assign 15 to the array element whose index is k
 
a(k) = 15
 
40205
 Given that an array named a whose elements are of type Integer has been declared, assign the value
         -1
to the last element in a
 
a(a.length - 1) = -1
 
40206
 An array of Integers named a has been declared with 12 elements. The integer variable  k holds a value between 0 and 6. Assign 9 to the element just after a[k]
 
a(k+1) = 9
 
40207
 An array of Integers named a has been declared with 12 elements. The integer variable  k holds a value between 2 and 8. Assign 22 to the element just before a[k]
 
a(k-1) = 22
 
40208
 Assume that an array of Integers named a that contains exactly five elements has been declared and initialized. In addition, an Integer variable  j has also been declared and initialized to a value somewhere between 0 and 3.
Write a single statement that assigns a new value to element of the array indexed by j. This new value should be equal to twice the value stored in the next element of the array (i.e. the element after the element indexed by j

a(j) = a(j+1) * 2
 
40209
 Assume that an array of integers named a has been declared and initialized. Write a single statement that assigns a new value to the first element of the array. The new value should be equal to twice the value stored in the last element of the array. 
 
a(0) = a(a.length - 1) * 2
 
 
 
 
 
8.2 Array Processing Techniques
 
 
 
40210
  Given an array temps of Doubles, containing temperature data, compute the average temperature. Store the average in a variable  called avgTemp. Besides temps and avgTemp, you may use only two other variables  -- an Integer variable  k and a Double variable  named total, which have been declared. 
 
 
k = 0
total = 0
Do
 total += temps(k)
 k += 1
Loop Until (k = temps.length)
avgTemp = total / k
 
 
40213
 Given:
  • an Integer variable  k,
  • an Integer array currentMembers that has been declared and initialized,
  • an Integer variable  memberID that has been initialized, and
  • an Boolean variable  isAMember,
write code that assigns True to isAMember if the value of memberID can be found in currentMembers, and that assigns false to isAMember otherwise. Use only k, currentMembers, memberID, and isAMember


isAMember = false
k = 0
Do
 If memberID = currentMembers(k) Then
  isAMember = true
 End If
 k += 1
Loop Until (k = currentMembers.length)
 
 
40214
 You are given an Integer variable  k, an Integer array zipcodeList that has been declared and initialized, and an Boolean variable  duplicates. Write some code that assigns True to duplicates if there are two adjacent elements in the array that have the same value, and that assigns False to duplicates otherwise. Use only k, zipcodeList, and duplicates.
 
 duplicates = false
k = 0
Do
 If zipcodeList(k) = zipcodeList(k+1) Then
  duplicates = true
 End If
 k += 1
Loop Until (k = zipcodeList.length - 1)
 
 
40215
 You are given two Integer variables  j and k, an Integer array zipcodeList that has been declared and initialized, and an Boolean variable  duplicates. Write some code that assigns True to duplicates if any two elements in the array have the same value, and that assigns False to duplicates otherwise. Use only j, k, zipcodeList, and duplicates.
 
 
duplicates = false
k = 0
j = 0
For k = 0 to zipcodeList.length-1
 For j = 0 to zipcodeList.length-1
  If (zipcodeList(k) = zipcodeList(j)) And (j<>k) Then
   duplicates = true
  End If
 Next
Next
 
40216
Give
  • an Integer variable  k,
  • an Integer array incompletes that has been declared and initialized,
  • an int variable  studentID that has been initialized, and
  • an int variable  numberOfIncompletes,
write code that counts the number of times the value of studentID appears in incompletes and assigns this value to numberOfIncompletes.
You may use only k, incompletes, studentID, and numberOfIncompletes


numberOfIncompletes = 0
For k = 0 to (incompletes.length-1)
 If studentID = incompletes(k) Then
  numberOfIncompletes += 1
 End If
Next
 
 
 
40219
 An array of integers named parkingTickets has been declared and initialized to the number of parking tickets given out by the city police each day since the beginning of the current year. (Thus, the first element of the array contains the number of tickets given on January 1; the last element contains the number of tickets given today.)

A variable  named mostTickets has been declared, along with a variable  k.

Without using any additional variables , write some code that results in mostTickets containing the largest value found in parkingTickets
 
 
 mostTickets = 0
For k = 0 to (parkingTickets.length-1)
 If mostTickets < parkingTickets(k) Then
  mostTickets = parkingTickets(k)
 End If
Next
 
 
 
40212
 Reversing the elements of an array involves swapping the corresponding elements of the array: the first with the last, the second with the next to the last, and so on, all the way to the middle of the array.
Given an array a and two other Integer variables , k and temp, write a loop that reverses the elements of the array.
Do not use any other variables  besides a, k, and temp.
 
 For k = 0 to ((a.length-1)/2)
 temp = a(k)
 a(k) = a((a.length-1)-k)
 a((a.length-1)-k) = temp
Next
 
 
 
 
 
 
 
 
 

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))))