a. Look at the following array declarations and indicate the number of elements
ID: 664225 • Letter: A
Question
a. Look at the following array declarations and indicate the number of elements in each array:
1. Dim dblNums(100) As Double
2. Dim intValues() As Integer = {99, 99, 99}
3. Dim intArray(0) As Integer
b. The following code totals the values in two Integeer arrays: intNumberArray1 and intNumberArray2. Both arrays have 25 elements. Will the code print the correct sum of values for both arrays? Why or why not?
Dim intTotal As Integer = 0 ' Accumulator
For intCount = 0 to 24
intTotal += intNumberArray1(intCount)
Next
MessageBox.Show("Total for intNumberArray1 is " & intTotal.ToString())
For intCount = 0 to 24
intTotal += intNumberArray2(intCount)
Next
MessageBox.Show("Total for intNumberArray2 is " & intTotal.ToString())
Explanation / Answer
a.
The above array dblNums can store 101 elements.
The above array intValues contains 3 elements.
The above array intValues can store 2 elements.
b.
No, the given code will not print s um of values for both arrays
A statement “ intTotal=0” is missed before for loop that calculate the sum of inNumberArray2
So, add the statement intTotal=0 between two for statements as follows.
Dim intTotal As Integer = 0 ' Accumulator
For intCount = 0 To 24
intTotal += intNumberArray1(intCount)
Next
MessageBox.Show("Total for intNumberArray1 is " & intTotal.ToString())
'clear the counter before calculating sum of intNumberArray2
intTotal=0
For intCount = 0 To 24
intTotal += intNumberArray2(intCount)
Next
MessageBox.Show("Total for intNumberArray2 is " & intTotal.ToString())
The above code will print the sums for both arrays.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.