I am using visual basic as the coding lanuage, if I could get some help with 3-5
ID: 3695821 • Letter: I
Question
I am using visual basic as the coding lanuage, if I could get some help with 3-5 that would be much appreciated! Thank you.
1) Write a bit of code to initialize an integer array with even values from 2 to 500.
Module Module1
Sub Main()
Dim a() As Integer
Dim n As Integer
n = InputBox("How many elements do u want to enter")
ReDim a(n)
For i = 2 To n
a(i) = i
Next
For i = 2 To n
If a(i) Mod 2 = 0 Then
Console.WriteLine(a(i))
End If
Next
End Sub
End Module
2) Write a bit of code to display the above array in reverse order. (Console.writeline is fine.)
Module Module1
Sub Main()
Dim a() As Integer
Dim b() As Integer
Dim n As Integer
Dim j As Integer
n = InputBox("How many elements do u want to enter")
ReDim a(n)
ReDim b(n)
For i = 2 To n
a(i) = i
Next
j = 0
For i = 2 To n
If a(i) Mod 2 = 0 Then
b(j) = a(i)
j = j + 1
Console.WriteLine(a(i))
End If
Next
Array.Reverse(b, 0, j)
For i = 0 To j - 1
Console.WriteLine(b(i))
Next
End Sub
End Module
3) Write a bit of code to show every third element of the above array.
4) Write a bit of code to sum all the elements in the above array.
5) Write a bit of code to display the elements in a triangular format. e.g.
2
4 6
8 10 12 (etc..)
Explanation / Answer
'4. Write a bit of code to sum all the elements in the above array.
Module Module1
Sub Main()
Dim sum As Integer = 0
Dim a() As Integer
Dim n As Integer
n = InputBox("How many elements do u want to enter")
ReDim a(n)
For i = 1 To n
a(i) = i
Next
'calculate sum
For i = 1 To n
sum = sum + a(i)
Next
Console.WriteLine("Sum of all elements in the Array: " & sum)
End Sub
End Module
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'3. Write a bit of code to show every third element of the above array.
Module Module1
Sub Main()
Dim sum As Integer = 0
Dim a() As Integer
Dim n As Integer
n = InputBox("How many elements do u want to enter")
ReDim a(n)
For i = 1 To n
a(i) = i
Next
'showing every third elements
For i = 1 To n
If (i Mod 3 = 0) Then
Console.WriteLine(a(i))
End If
Next
End Sub
End Module
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'5) Write a bit of code to display the elements in a triangular format.
Module Module1
Sub Main()
Dim sum As Integer = 0
Dim a() As Integer
Dim n As Integer
n = InputBox("How many elements do u want to enter")
ReDim a(n)
For i = 1 To n
a(i) = i
Next
'Triangular format
For i = 1 To n
Console.WriteLine("")
For j = 1 To i
Console.Write(a(i) & " ")
Next
Next
End Sub
End Module
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.