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
 
 
 
 
 
 
 
 
 

1 comment:

  1. For 40212:
    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

    ReplyDelete