27 April, 2011

Array




1.            Calculate the sum of numbers stored in an array.

Dim i As Integer
Dim s As Integer = 0
Dim a(4) As Integer
For i = 0 To 3
a(i) = i
s = s + a(i)
Next i
TextBox1.Text = s
End Sub


2.            Determine the maximum and minimum number among the numbers stored in an array.

Dim i As Integer
Dim s As Integer = 0
Dim a(3) As Integer
For i = 0 To 2
a(i) = i
Next i
If a(0) > a(1) And a(0) > a(2) Then
TextBox1.Text = "a(0) is max)"
ElseIf a(1) > a(0) And a(1) > a(2) Then
TextBox1.Text = "a(1) is max"
Else
TextBox1.Text = "a(2) is max"
End If
End Sub


3.            Display the name of a month from the month number. For instance, if the user inputs 2, the program should display February. [Hint: Create an array of 12 strings, one for each month of the year]

Dim j As Integer
Dim a(12) As String
a(1) = "january"
a(2) = "february"
a(3) = "march"
a(4) = "april"
a(5) = "may"
a(6) = "june"
a(7) = "july"
a(8) = "august"
a(9) = "september"
a(10) = "october"
a(11) = "november"
a(12) = "december"
j = TextBox1.Text
If j = 1 Then
 TextBox2.Text = "January"
ElseIf j = 2 Then
TextBox2.Text = "February"
ElseIf j = 3 Then
TextBox2.Text = "March"
ElseIf j = 4 Then
TextBox2.Text = "April"
ElseIf j = 5 Then
TextBox2.Text = "May"
ElseIf j = 6 Then
TextBox2.Text = "June"
ElseIf j = 7 Then
TextBox2.Text = "July"
ElseIf j = 8 Then
TextBox2.Text = "August"
ElseIf j = 9 Then
TextBox2.Text = "September"
ElseIf j = 10 Then
TextBox2.Text = "October"
ElseIf j = 11 Then
TextBox2.Text = "November"
ElseIf j = 12 Then
TextBox2.Text = "December"
Else
TextBox2.Text = "out of the count"
End If
End Sub


4.            Calculate the average marks obtained by a student for 10 different courses. The marks for each course will be entered by the user and stored in an array. 

Dim i As Integer
Dim a(10) As Integer
Dim s As Integer
a(0) = TextBox1.Text
a(1) = TextBox2.Text
a(2) = TextBox3.Text
a(3) = TextBox4.Text
a(4) = TextBox5.Text
a(5) = TextBox6.Text
a(6) = TextBox7.Text
a(7) = TextBox8.Text
a(8) = TextBox9.Text
a(9) = TextBox10.Text
For i = 0 To 10
s = s + a(i)
Next i
TextBox11.Text = s / 10
End Sub

0 comments:

Post a Comment