# how to convert elements in a 2d string array to a 2d int array?

Page 1 of 1

## 1 Replies - 246 Views - Last Post: 27 October 2020 - 11:36 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=420338&amp;s=b3879dfb231c7b18c94bf951fdf8491b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 leomarrg

Reputation: 0
• Posts: 21
• Joined: 26-November 19

# how to convert elements in a 2d string array to a 2d int array?

Posted 27 October 2020 - 10:31 AM

I'm trying to convert the elements of a 2d string array to a 2d int array to add the rows and get the average, the string array is 5x4, all the elements in the array are user input using a inputbox. I tried converting the array to int using tryparse and Cint but it gives me the same result. i input 100, 100 100 100 100 90 90 90 90 80 80 80 80 70 70 70 70 60 60 60 60 to test if im adding the rows correctly all i get in my average listbox is 60 60 60 60 60. Im not calculating average yet because my sum is not working. this is my code.

```For Rows = 0 To StudentsScores.GetUpperBound(0) - 1
TempTotal = 0
For Columns = 0 To StudentsScores.GetUpperBound(1) - 1

TempArrayInt(Rows, Columns) = CInt(StudentsScores(Rows, Columns))
TempTotal += TempArrayInt(Rows, Columns)
Next
'TempTotal /= 4
TempTotalStr = TempTotal.ToString()
Next
```

Is This A Good Question/Topic? 0

## Replies To: how to convert elements in a 2d string array to a 2d int array?

### #2 modi123_1

• Suitor #2

Reputation: 15965
• Posts: 63,898
• Joined: 12-June 08

## Re: how to convert elements in a 2d string array to a 2d int array?

Posted 27 October 2020 - 11:36 AM

I would go about this a slightly different way. No real need to have a temporary integer array to hold all the converted strings as integers. Just convert in place, add to a 1d array for your totals, and have a second 1d double array for your average.

Example:

```    Dim StudentsScores(,) As Int32 = {{100, 100},
{100, 90},
{90, 80},
{80, 70},
{70, 60}}
Const maxRow As Int32 = 5
Const maxCol As Int32 = 2

Dim totals(maxRow - 1) As Int32
Dim ave(maxRow - 1) As Double

For Rows = 0 To maxRow - 1
For col = 0 To maxCol - 1
totals(Rows) += StudentsScores(Rows, col)'total into one 1d array
Next

ave(Rows) = totals(Rows) / maxCol 'average in the other 1d array
Next

'print out the goodies
For Rows = 0 To maxRow - 1
For col = 0 To maxCol - 1
Next
Console.WriteLine(\$" = {totals(Rows)} : {ave(Rows)} ")

Next
```

```100 100  = 200 : 100
100  90  = 190 : 95
90  80  = 170 : 85
80  70  = 150 : 75
70  60  = 130 : 65
```