0% found this document useful (0 votes)
205 views

Display The Sum and Average of All The Elements in An Array

The document contains 6 examples demonstrating the use of arrays in VB coding. The first example shows how to display the sum and average of elements in an array. The second transfers elements from one array to another. The third displays even and odd positions from an array. The fourth transfers elements between two dimensional arrays in a zig-zag manner. The fifth accepts data from two arrays and displays their product in a third. The sixth does the same but in a zig-zag manner.

Uploaded by

Aashish Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
205 views

Display The Sum and Average of All The Elements in An Array

The document contains 6 examples demonstrating the use of arrays in VB coding. The first example shows how to display the sum and average of elements in an array. The second transfers elements from one array to another. The third displays even and odd positions from an array. The fourth transfers elements between two dimensional arrays in a zig-zag manner. The fifth accepts data from two arrays and displays their product in a third. The sixth does the same but in a zig-zag manner.

Uploaded by

Aashish Singh
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 12

1. Display the sum and average of all the elements in an array.

Coding:

Dim a(10) As Integer


Dim i As Integer
Dim sum As Integer
Dim avg As Single

Private Sub cmdDisplay_Click()


For i = 0 To 9
sum = sum + a(i)
lstDisplay.AddItem a(i)
Next i
avg = sum / 10
lblDisplay.Visible = True
lblDisplay.Caption = "The Sum of all the elements is " & sum & " and Average is " &
avg
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdOk_Click()


a(i) = Val(txtEnter.Text)
i=i+1
txtEnter.Text = ""
txtEnter.SetFocus
If i = 10 Then
cmdOk.Visible = False
cmdDisplay.Visible = True
End If
End Sub

Output:
2. Declare two single dimensional arrays and transfer the contents of first array to
the corresponding second array.
3. Display all the even and odd positions from array.

Coding:

Dim a(10) As Integer


Dim i As Integer

Private Sub cmdDisplay_Click()


For i = 0 To 9
If i Mod 2 = 0 Then
lstOdd.AddItem "Position " & i & ": " & a(i)
Else
lstEven.AddItem " Position " & i & ": " & a(i)
End If
Next i
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdOk_Click()


a(i) = Val(txtAdd.Text)
i=i+1
txtAdd.Text = ""
txtAdd.SetFocus
If i = 10 Then
cmdOk.Visible = False
cmdDisplay.Visible = True
End If
End Sub

Output:
4. Declare two dimensional array and transfer the elements into another array in
zig-zag manner.

Coding:

Dim a(4) As Integer


Dim b(4) As Integer
Dim i As Integer

Private Sub cmdDisplay_Click()


For i = 0 To 3
b(i) = a(3 - i)
lstFirst.AddItem "Position " & i & ": " & a(i)
lstSecond.AddItem "Position " & i & ": " & b(i)
Next i
End Sub

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdOk_Click()


a(i) = Val(txtAdd.Text)
i=i+1
txtAdd.Text = ""
txtAdd.SetFocus
If i = 4 Then
cmdOk.Visible = False
cmdDisplay.Visible = True
End If
End Sub

Output:
5. Declare 3 single dimensional arrays. Accept the data from first two arrays and in
third display their product.

Coding:

Dim a(5) As Integer


Dim b(5) As Integer
Dim c(5) As Integer
Dim i As Integer

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdOk_Click()


If txtFAdd.Text = "" Or txtSAdd.Text = "" Then
MsgBox "Enter elements"
Else
a(i) = Val(txtFAdd.Text)
b(i) = Val(txtSAdd.Text)
i=i+1
txtFAdd.Text = ""
txtSAdd.Text = ""
txtFAdd.SetFocus
End If
If i = 5 Then
cmdOk.Visible = False
End If
End Sub

Private Sub cmdProduct_Click()


For i = 0 To 4
c(i) = a(i) * b(i)
lstFirst.AddItem "Element is: " & a(i)
lstSecond.AddItem "Element is: " & b(i)
lstProduct.AddItem "Element is: " & c(i)
Next i
End Sub

Output:
6. Declare 3 single dimensional arrays. Accept the data from first two arrays and in
third display their product in zig-zag manner.

Coding:

Dim a(5) As Integer


Dim b(5) As Integer
Dim c(5) As Integer
Dim i As Integer

Private Sub cmdExit_Click()


End
End Sub

Private Sub cmdOk_Click()


If txtFAdd.Text = "" Or txtSAdd.Text = "" Then
MsgBox "Enter elements"
Else
a(i) = Val(txtFAdd.Text)
b(i) = Val(txtSAdd.Text)
i=i+1
txtFAdd.Text = ""
txtSAdd.Text = ""
txtFAdd.SetFocus
End If
If i = 5 Then
cmdOk.Visible = False
End If
End Sub

Private Sub cmdProduct_Click()


For i = 0 To 4
c(i) = a(i) * b(4 - i)
lstFirst.AddItem "Element is: " & a(i)
lstSecond.AddItem "Element is: " & b(i)
lstProduct.AddItem "Product is: " & c(i)
Next i
End Sub

Output:

You might also like