0

Ok I am not completing understand this. I am suppose to only Find the total of each column in the last row. Find the grand total in the last cell (the bottom right corner) Ok i have tried to do the grandtotal but i am not getting what she wants done I have also tried to reverse the arrays but that was wrong to. I am only suppose to add two line one line is find the total of each colums in the last row and to find the grandtotal. Can someone please explain to me what i am doing wrong thank you.

Module Module1

Sub Main()

    Dim sum(5, 4) As Integer
    Dim row, col As Integer
    Dim grandtotal As Integer
    For row = 0 To 4
        For col = 0 To 3
            sum(row, col) = row + col
            sum(row, 4) += row + col

        Next col
    Next row
    For row = 0 To 5
        For col = 0 To 4
            Console.Write(sum(row, col) & vbTab)

        Next col
        Console.WriteLine()

    Next row
End Sub

End Module

1 Answer 1

1

It looks like you have a pretty good grasp of the code you need to use, but I think the main issue here is that you're getting a bit confused with what you actually want the code to do. I too am having a hard time deciding what your actual goal here is, as you say one thing but then have code for something slightly different. I'm assuming this is a homework assignment, so I'm not going to give you the code (especially since I'm not 100% sure what code you might need), but I'm more than happy to go through this stepwise as clearly as I can, and hopefully we'll get to the bottom of your confusion.

To clarify your requirements:

  • You say you're supposed to find the total of each column, and place that total in the last row. According to your code, that would be row 5 (actually the 6th row).
  • You then say you need the grand total in the last cell, the bottom-right corner. Since you have 5 columns (0-4), that would be a total of the first 4 columns of row 5.

If we imagine the 2D sum array as a matrix, the problem as stated should yield the following:

Desired Matrix http://dl.dropbox.com/u/1497850/Hosted%20Images%20for%20Websites/MatrixDesired.jpg

Where the "x's" are blank (technically 0), since that column is reserved as the "grand total" column (only the bottom-right should be filled in). For reference, this array/matrix will have the following coordinates:

Coordinate Matrix http://dl.dropbox.com/u/1497850/Hosted%20Images%20for%20Websites/MatrixCoords.jpg

However, as you currently have it coded, your sum array will look like this:

Actual Matrix http://dl.dropbox.com/u/1497850/Hosted%20Images%20for%20Websites/MatrixActual.jpg

What you've done in your code is made the final column the "total column" and ignored the final row. In this case, the bottom-right cell would still be the grand total, but you'd be summing the numbers in column 4 rather than in row 5. This goes against what you stated you want to happen, but is pretty easy to correct (especially now that you can see what's actually happening compared to what you want to happen).

However, maybe this is what you actually meant to happen, and you just misphrased the question a bit? (Or maybe this is what you meant by saying you tried to reverse the arrays?) The point is, rows and columns in even semi-large arrays/matrices are generally easily transposed, so it's important to make sure you're using the right terminology at all times.

Edit: Sorry, I misread your second For loop when I first answered, I apologize. I see now that you're printing the sum array out, but your problem was still probably due to the fact that you weren't filling in the array as you expected. Once you fill the array the way you actually want, your printing statement should work just fine.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.