Science Forums

# 1 dimensional array and 2 dim array in vb.net

## Recommended Posts

well its a work that am trying to do below are my answer to the question . Am however stacked and actually dont know if am right. please check and help me.

1, You have been given a 1 dimensional array. Write a vb program that will change all numbers less than 5 to their inverse

Ans

for i= 1 to len(arr)

if arr(i)<5 then arr(i) = 1 / arr(i)

next

2,Given a 2 dim array write a vb program that will add all odd numbers greater than 3

##### Share on other sites
... please check and help me.

1, You have been given a 1 dimensional array. Write a vb program that will change all numbers less than 5 to their inverse.

Okay, your solution is nearly correct. You must add one more line to avoid dividing by zero.

for i= 1 to len(arr)

if arr(i) = 0 then next

if arr(i) < 5 then arr(i) = 1 / arr(i)

next

2,Given a 2 dim array write a vb program that will add all odd numbers greater than 3

I don't know this version of vb (I know VBA in Excel), but it would look something like:

function IsOdd (number as int)

oddflag = true

testvalue = integer(number/2) //remove the fractional part if any

testvalue = testvalue * 2

if testvalue = number then oddflag= false //number is even

return oddflag

end function

total = 0

for i= 1 to len(arr)

for j= 1 to width(arr)

num= arr(i,j)

if IsOdd(num) and if num>3 then total = total + num

next j

next i

print total

I hope that helps. Of course, you will have to translate into vb.

##### Share on other sites

Cool! A vb.net question!!!

Pyro is right on the money for the general programming. There are some slight adjustments to make to vb.net.

in .net arrays begin with element 0 (this is mandated where in VB you can declare the upper and lower bounds of an array freely), not with element 1. An array with a length one 1 is declared like Dim i(0) as ... To declare a two dimensional array that is 4 x 4 you would Dim i(3,3) as ...

Also an array data object, and any object in vb.net, is manipulated using properties and methods of the object rather than functions. Len(x) is calling the Function "Len" and passing it the object x, and it will return a length as a number. In .net you would use x.GetLength(0) to find the length of the first dimension of the array. The x.Length method returns to total length of the array, so the 4x4 array would return a length of 16.

In the case of vb.net there are several ways to move through a dimension of an array..

Where a is an array and i is an integer used for counting...

```For i = a.GetLowerBound(0) to a.GetUpperBound(0)
- or -
For i = 0 to a.GetUpperBound(0)
- or -
For i = 0 to a.GetLength(0)-1
- or for a single dimensional array -
For i = 0 to a.Length```

Programatically the first one is the tightest, but they will all produce the same result. If somehow in a later version of .net they allow arrays who's dimensional indexes do not start at zero the first option would still work, while the others would need to be rewritten to avoid possible failures (how's that for covering one's ***?).

```Sub Problem1

Dim a(9) as Integer  ' this is our array with a length of 10
Dim i as Integer    ' this is the counter we will use
Dim RandomNumber as New Random()

For i = a.GetLowerBound(0) to a.GetUpperBound(0)
a(i) = RandomNumber.Next(0,10)    ' assigns a random number form 0 to 10
If a(i) < 5 AND a(i)<>0 THEN a(i) = 1 / a(i)
Next i

End Sub

Sub Problem2

Dim a(9,9) as Integer  ' this is a 10 x 10 array
Dim i as Integer   ' first counter
Dim j as Integer   ' second counter
Dim Total as Integer    ' used for the running total
Dim RandomNumber as New Random()

For i = a.GetLowerBound(0) to a.GetUpperBound(0)
For j = a.GetLowerBound(1) to a.GetUpperBound(1)
a(i,j) = RandomNumber.Next(0,10)
If a(i,j)/2 = Int(a(i,j)/2) THEN Total += a(i,j)
Next j
Next i

End Sub```

Good luck!!!

Bill

## Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account. ×   Pasted as rich text.   Paste as plain text instead

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.