Results 1 to 15 of 15

Thread: 2D array summary

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    2D array summary

    So I had to make a 2D array that would append the percentage of change from the previous row to the current row at the end of that row. For example:


    50 100 100 50 0.00 0.00 0.00 0.00
    50 200 50 25 0.00 100 -50 -50

    Now I have this part down but now i need to make it so it creates another row at then end that says the percentage change overall.


    here is the code I have so far
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim col As Integer
    
            Dim rows() As String = IO.File.ReadAllLines("C:\sales.txt")
    
            Dim All(rows.GetUpperBound(0), rows(0).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries).GetUpperBound(0)) As Decimal
    
            Dim newRows As New List(Of String)
    
            For row As Integer = 0 To rows.GetUpperBound(0)
    
                Dim parts() As String = rows(row).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries)
    
                newRows.Add(String.Join("   ", parts) & "   ")
    
                For column As Integer = 0 To parts.GetUpperBound(0)
                    col = parts.GetUpperBound(0)
                    All(row, column) = CDec(parts(column))
    
                    If row > 0 Then
    
                        newRows(newRows.Count - 1) &= (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & "  "
                   
                    ElseIf row = 0 AndAlso column = 0 Then
    
                        Dim d(parts.GetUpperBound(0)) As Decimal
    
                        newRows(newRows.Count - 1) &= String.Join(" ", Array.ConvertAll(Of Decimal, String)(d, Function(v As Decimal) v.ToString("n2")))
    
                    End If
    
                Next
    
            Next
    
            TextBox1.Lines = newRows.ToArray
    
    
    
    
            'to save:
    
            IO.File.WriteAllLines("C:\Solution.txt", newRows.ToArray)
    
            ' squareArray is the array
            MessageBox.Show("Done!!!")
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Close()
    
        End Sub
    End Class

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: 2D array summary

    Were you here under a different name, or is this a class assignment? I ask because that's a really strange requirement, and you are the second person to ask in as many weeks.

    In any case, what is the percentage overall? Is that just the change from the first to the last?
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    I wasn't here, but it is a class assignment.

    yes the change from the first row to the last row.

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    Here is the "sales.txt" file and the "solution.txt" file that my program creates. But the sales.txt file is subject to change, so the program has to manipulate the data dynamically.
    Attached Files Attached Files

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: 2D array summary

    how do you mean 'the percentage change overall'?

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    Basically the percentage of change between the first and last rows of data in Sales.txt

    I can think of doing this in two ways:
    -adding all the values in the newly created rows together in each column
    -(last row/first row * 100) - 100 for each column



    PS: Thank you so much for helping me with this.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: 2D array summary

    i'd use the first method.

    put this after the Dim All... line

    vb Code:
    1. dim difference(All.GetUpperBound(1)) as decimal

    then after the first newRows(newRows.Count - 1) &= line:

    vb Code:
    1. difference(column) += (((All(row, column) / All(row - 1, column)) * 100) - 100)

    the total percentage differences will be in the difference array + i'm sure you'll be able to work out how to add it to the newrows list before you write your new file.

    at least you admitted this is homework that guy last week forgot to mention it...

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    It's saying that the change in column one is -34.38... I don't get it.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: 2D array summary

    Quote Originally Posted by aLittleBunny View Post
    It's saying that the change in column one is -34.38... I don't get it.
    that looks ok to me. if you add all the percentage changes for column1, that would be the answer. don't forget 100 + -50 = 50

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    But if you do the manual change in difference it is only -18.52

    (25.73/36.99) * 100 -100

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: 2D array summary

    i double checked it. here's a test of the 2 methods side by side + there is a slight variation, though i'm not sure why:

    vb Code:
    1. Dim change = (From line In IO.File.ReadAllLines("Solution.txt") _
    2.                       Let fields = line.Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries) _
    3.                       Select CDec(fields(7))).Sum
    4. Dim lines() As String = IO.File.ReadAllLines("Solution.txt")
    5. Dim change2 As Decimal = ((CDec(lines(lines.GetUpperBound(0)).Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)(0)) / CDec(lines(0).Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)(0))) * 100) - 100

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    I am still getting the same problem...
    Rather than getting the overall percentage change for each column I'm getting -34.41

    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim col As Integer
    
            Dim rows() As String = IO.File.ReadAllLines("C:\sales.txt")
    
            Dim All(rows.GetUpperBound(0), rows(0).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries).GetUpperBound(0)) As Decimal
    
            Dim newRows As New List(Of String)
    
            For row As Integer = 0 To rows.GetUpperBound(0)
    
                Dim parts() As String = rows(row).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries)
    
                newRows.Add(String.Join("   ", parts) & "   ")
    
                For column As Integer = 0 To parts.GetUpperBound(0)
                    col = parts.GetUpperBound(0)
                    All(row, column) = CDec(parts(column))
    
                    If row > 0 Then
    
                        newRows(newRows.Count - 1) &= (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & "  "
                   
                    ElseIf row = 0 AndAlso column = 0 Then
    
                        Dim d(parts.GetUpperBound(0)) As Decimal
    
                        newRows(newRows.Count - 1) &= String.Join(" ", Array.ConvertAll(Of Decimal, String)(d, Function(v As Decimal) v.ToString("n2")))
    
                    End If
    
                Next
    
            Next
    
            TextBox1.Lines = newRows.ToArray
    
    
    
    
    
            'to save:
    
            IO.File.WriteAllLines("C:\Solution.txt", newRows.ToArray)
    
    
            Dim change = (From line In IO.File.ReadAllLines("C:\Solution.txt") Let fields = line.Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries) Select CDec(fields(7))).Sum
    
    
            TextBox1.AppendText(vbCrLf & change)
    
            'Dim lines() As String = IO.File.ReadAllLines("C:\Solution.txt")
    
            '   Dim change2 As Decimal = ((CDec(lines(lines.GetUpperBound(0)).Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)(0)) / CDec(lines(0).Split(New String() {Chr(32)}, StringSplitOptions.RemoveEmptyEntries)(0))) * 100) - 100.ToString("n2") & "  "
    
    
    
    
            ' Dim WriteSolution As System.IO.StreamWriter
            ' WriteSolution = System.IO.File.AppendText("C:\Solution.txt")
            'WriteSolution.WriteLine(TextBox1.Text)
            'WriteSolution.Close()
    
    
            IO.File.WriteAllLines("C:\Solution.txt", TextBox1.Text)
    
            MessageBox.Show("Done!!!")
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Close()
    
        End Sub
    End Class

    I also don't know why I get:
    "Value of type 'String' cannot be converted to '1-dimensional array of String'."

    When i try the uncommented out method of writing to the text file.

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: 2D array summary

    vb Code:
    1. IO.File.WriteAllLines("C:\Solution.txt", TextBox1.lines)

    or:

    vb Code:
    1. IO.File.WriteAlltext("C:\Solution.txt", TextBox1.Text)

  14. #14

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    8

    Re: 2D array summary

    Got it
    Code:
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim col As Integer
            '  Dim lengthof As Integer
            Dim rows() As String = IO.File.ReadAllLines("C:\sales.txt")
    
            Dim All(rows.GetUpperBound(0), rows(0).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries).GetUpperBound(0)) As Decimal
    
            Dim difference(All.GetUpperBound(1)) As Decimal
    
            Dim newRows As New List(Of String)
    
            For row As Integer = 0 To rows.GetUpperBound(0)
    
                Dim parts() As String = rows(row).Split(New String() {Chr(9)}, StringSplitOptions.RemoveEmptyEntries)
    
                newRows.Add(String.Join("   ", parts) & "   ")
    
                For column As Integer = 0 To parts.GetUpperBound(0)
                    col = parts.GetUpperBound(0)
                    All(row, column) = CDec(parts(column))
    
                    If row > 0 Then
    
                        newRows(newRows.Count - 1) &= (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & "  "
                        difference(column) += (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2") & "  "
    
                    ElseIf row = 0 AndAlso column = 0 Then
    
                        Dim d(parts.GetUpperBound(0)) As Decimal
    
                        newRows(newRows.Count - 1) &= String.Join(" ", Array.ConvertAll(Of Decimal, String)(d, Function(v As Decimal) v.ToString("n2")))
    
                    End If
    
                Next
    
            Next
    
            TextBox1.Lines = newRows.ToArray
            TextBox1.AppendText(vbCrLf)
    
            For count As Integer = 0 To difference.Length - 1
    
    
                TextBox1.AppendText(difference(count) & "    ")
    
    
            Next
    
            'to save:
    
            IO.File.WriteAllText("C:\Solution.txt", TextBox1.Text)
    
    
    
            MessageBox.Show("Done!!!")
    
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Me.Close()
    
        End Sub
    End Class

  15. #15
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: 2D array summary

    think it through. All(,) is a decimal, difference() is a decimal, so to add a decimal element + a decimal element, why convert 1 of those values to a string?

    vb Code:
    1. difference(column) += (((All(row, column) / All(row - 1, column)) * 100) - 100).ToString("n2")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width