im a newbie here, im taking intro to programming wth VB in college and im having problem with the homework

here's what im supposed to do

36. Read 10 digits contained in a text file into an array and then display three columns in a list box as follows: column 1 should contain the original 10 digits, column 2 should contain these digits in reverse order, and column 3 should contain the average of the corresponding digits' in columns 1 and 2.

i asked someone else for help and they gave me a layout, they offered me their code but im trying to hold off on that

here's what i have so far, can't get past the firs part =/

the text file has 10 numbers(vertically)
1
2
3

Public Class Form1

    Dim x() As Integer '///Array for the numbers is order
    Dim y() As Integer '///array for the reversed numbers
    Dim z() As Integer '///array for the average
    Dim sr As IO.StreamReader

    Sub read(ByRef x() As Integer, ByRef sr As IO.StreamReader)

        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0

        Do Until (count = 10)
            x(count) = sr.ReadLine'///ERROR:Object reference not set to an instance of an object.
            count += 1
            ListBox1.Items.Add(x(count))'///Display x value 
        Loop

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        read(x, sr)
    End Sub

End Class

Recommended Answers

All 7 Replies

See if this helps for "the first part".

Public Class Form1
    '/////////////////////////////-- array for 10 Integers.
    Dim x(9) As Integer '///Array for the numbers is order
    '//////////////////////////
    Dim y() As Integer '///array for the reversed numbers
    Dim z() As Integer '///array for the average
    Dim sr As IO.StreamReader

    Sub read(ByRef x() As Integer, ByRef sr As IO.StreamReader)

        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0

        Do Until (count = 10)
            x(count) = sr.ReadLine
            ListBox1.Items.Add(x(count)) '///Display x value 
            '///////////////////////////
            count += 1 '// added last.
            '/////////////////////////
        Loop

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        read(x, sr)
    End Sub

End Class

aahh it worked =)

so i got half of the second part to work, which is displaying he numbers backwards


all it does is give me the bottom half of the numbers,i tried a temp but it failed.
0(x5)
5
4
3
2
1

Sub bckWrd(ByVal x() As Integer, ByVal y() As Integer, ByVal sr As IO.StreamReader)

        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0
        Dim count2 As Integer = 9

        Do Until (count = 10)
            x(count) = sr.ReadLine()
            y(count2) = x(count)
           
             ListBox1.Items.Add(y(count))

            count += 1
            count2 -= 1
 Loop
End Sub

See if this helps for the "second part".

Sub bckWrd(ByVal x() As Integer, ByVal y() As Integer, ByVal sr As IO.StreamReader)
        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0
        Do Until (count = 10)
            x(count) = sr.ReadLine
            '//////////// -- ListBox.Items.Add Code Removed.
            count += 1 '// added last.
        Loop
        '/////////////////////////-- loop thru all of the x arrays backwards.
        For i As Integer = 9 To 0 Step -1
            ListBox1.Items.Add(x(i)) '// add x(array number)
        Next
        '////////////////////////
    End Sub

I used the same code that you use for the

Sub read(ByRef x() As Integer, ByRef sr As IO.StreamReader)

with just a few slight modifications.

If you do not want to use a "For/Next" loop, you can replace it in the above code with a "Do Until/Loop".

'/////////////////////////-- loop thru all of the x arrays backwards.
        count = 9
        Do Until count = 0
            ListBox1.Items.Add(x(count))
            count -= 1 '// added last.
        Loop
        '////////////////////////

ok, so i finally got both the arrays set, now what i'm trying to call sub read(), and sub bckWard(), into sub avg(), but the array values aren't being transfered. I don't know how to do this, i thought they would be passed.
EDIT: errm well at least now i see numbers, but i keep getting 5.5(x10)

here is what i have so far

Public Class Form1
    '/////////////////////////////-- array for 10 Integers.
    Dim x(9) As Integer '///Array for the numbers is order
    Dim y(9) As Integer '///array for the reversed numbers
    Dim z(9) As Double '///Array for the averages; it's double cuz i wont be getting integers as a result
    Dim result As Integer
    Dim sr As IO.StreamReader



    Sub read(ByRef x() As Integer, ByRef sr As IO.StreamReader)
        '//////Imports values to x array from file
        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0
        Do Until (count = 10)
            x(count) = sr.ReadLine()
            count += 1 '// added last
        Loop
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '//////////shows numbers in order
        Call read(x, sr)
        For i As Integer = 0 To 9
            ListBox1.Items.Add(x(i)) '/////displays the x array
        Next
    End Sub



    Sub bckWrd(ByRef x() As Integer, ByRef y() As Integer, ByRef sr As IO.StreamReader)
        '////assign the values to the y array but backwards; ex y(1) = 10, y(2) = 9
        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0
        Dim count2 As Integer = 9
        Do Until (count = 10)
            x(count) = sr.ReadLine
            '//////////// -- ListBox.Items.Add Code Removed.
            y(count2) = x(count)
            count += 1 '// added last.
            count2 -= 1
        Loop
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'shows numbers in backward order
        Call bckWrd(x, y, sr)
        For i As Integer = 0 To 9
            ListBox1.Items.Add(y(i)) '// add y(array number)
        Next
    End Sub


    '//////Trying to call for other subs but the values of the arrays aren't being passed
    Sub avg(ByRef x() As Integer, ByRef y() As Integer, ByRef z() As Double, ByRef sr As IO.StreamReader)
        '//////////// find the average of the rows: {x(1)+y(1)} /2 = z(1)
        Call read(x, sr)
        Call bckWrd(x, y, sr)
        '/////////
        For i As Integer = 0 To 9
            z(i) = (x(i) + y(i)) / 2 '///
        Next
        '////////
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'shows the averages 
        Call avg(x, y, z, result, sr)
        For i As Integer = 0 To 9
            ListBox1.Items.Add(z(i))
        Next
    End Sub


End Class

im stupid lol, ignore the above post.

Thank you so much for your help i finally go it working, i spent more than 6 hours on this and it was just homework, i still learned a lot from this, much appreciated.

Loop
(Thank you) x 100
End Loop

Public Class Form1
    '/////////////////////////////-- array for 10 Integers.
    Dim x(9) As Integer '///Array for the numbers is order
    Dim y(9) As Integer '///array for the reversed numbers
    Dim z(9) As Double '///Array for the averages
    Dim sr As IO.StreamReader



    Sub read(ByRef x() As Integer, ByRef sr As IO.StreamReader)
        '//////Imports values to x array from file
        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0
        Do Until (count = 10)
            x(count) = sr.ReadLine()
            count += 1 '// added last
        Loop
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '//////////shows numbers in order
        Call read(x, sr)
        For i As Integer = 0 To 9
            ListBox1.Items.Add(x(i)) '/////displays the x array
        Next
    End Sub



    Sub bckWrd(ByRef x() As Integer, ByRef y() As Integer, ByRef sr As IO.StreamReader)
        '////assign the values to the y array but backwards; ex y(1) = 10, y(2) = 9
        sr = IO.File.OpenText("D:\digits.txt") '///opens file
        Dim count As Integer = 0
        Dim count2 As Integer = 9
        Do Until (count = 10)
            x(count) = sr.ReadLine
            '//////////// -- ListBox.Items.Add Code Removed.
            y(count2) = x(count)
            count += 1 '// added last.
            count2 -= 1
        Loop
        '/////////////////////////-- loop thru all of the x arrays backwards.
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'shows numbers in backward order
        Call bckWrd(x, y, sr)
        For i As Integer = 0 To 9
            ListBox1.Items.Add(y(i)) '// add y(array number)
        Next
    End Sub




    Sub avg(ByRef x() As Integer, ByRef y() As Integer, ByRef z() As Double, ByRef sr As IO.StreamReader)
        '//////////// find the average of the rows: {x(1)+y(1)} /2 = z(1)
        Call read(x, sr)
        Call bckWrd(x, y, sr)
        Dim count As Integer = 0
        Do Until count = 10
            z(count) = (x(count) + y(count)) / 2
            count += 1
        Loop
        '////////
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        'shows the averages 
        Call avg(x, y, z, sr)
        For i As Integer = 0 To 9
            ListBox1.Items.Add(z(i)) '// add y(array number)
        Next
    End Sub




    Sub Display(ByRef x() As Integer, ByRef y() As Integer, ByRef z() As Double, ByRef sr As IO.StreamReader)
        'pulls out all the arrays x,y,z for use
        Call read(x, sr)
        Call bckWrd(x, y, sr)
        Call avg(x, y, z, sr)
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        '///displays Average Array
        Call Display(x, y, z, sr)
        Dim column As String = "{0, 0}{1,10}{2,10}" '///sets 3 columns for the arrays
        For i As Integer = 0 To 9
            ListBox1.Items.Add(String.Format(column, x(i), y(i), z(i)))
        Next
    End Sub
End Class

:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.