1 Replies - 246 Views - Last Post: 27 October 2020 - 11:36 AM Rate Topic: -----

#1 leomarrg   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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()
            lstAvg.Items.Add(TempTotalStr)
        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   User is online

  • Suitor #2
  • member icon



Reputation: 15965
  • View blog
  • 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
                Console.Write($"{StudentsScores(Rows, col).ToString().PadLeft(3)} ")
            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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1