-
Apr 24th, 2014, 09:37 PM
#1
Thread Starter
New Member
assignment for intro to programming csci
hey i have been working on this diagonal sum function for my college intro course and was hoping i could get some help
here is the code i have got so far.
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim myarray(3, 3) As Integer
'table numbers
myarray(0, 0) = 1
myarray(0, 1) = -2
myarray(0, 2) = 3
myarray(0, 3) = 14
myarray(1, 0) = 0
myarray(1, 1) = 55
myarray(1, 2) = 3
myarray(1, 3) = -17
myarray(2, 0) = -2
myarray(2, 1) = 4
myarray(2, 2) = 44
myarray(2, 3) = 117
myarray(3, 0) = 9
myarray(3, 1) = 0
myarray(3, 2) = 23
myarray(3, 3) = 4
Dim result As String = ""
For row = 0 To 3
For col = 0 To 3
Next
Next
'putting array into message box
For row = 0 To 3
For col = 0 To 3
result += myarray(row, col).ToString + ControlChars.Tab
Next
result += ControlChars.CrLf
Next
MessageBox.Show(result)
'sum for loop
Dim sum As Integer = 0
For i = myarray(0, 0) To myarray(3, 3)
sum = (myarray(0, 0) + myarray(1, 1) + myarray(2, 2) + myarray(3, 3))
Next
MessageBox.Show(sum.ToString)
Me.Close()
End Sub
End Class
Ive gotten the code to where it gives my the forms and answers i need but i was wondering if there was another way to do the for loop
-
Apr 25th, 2014, 04:26 AM
#2
Re: assignment for intro to programming csci
Yes indeed there is a different way to do the For loop (I'm assuming we're talking about the summing loop at the end?). If you turned in that code you'd probably fail the assignment, unfortunately.
What you're doing is looping over all the integers from the value in (0,0) to the value in (3,3), so in this case from 1 to 4. On each of those 4 iterations you set the 'sum' variable to the result of the expression, which is the sum of the diagonal cells. This is not what you're aiming for. The For loop is not actually gaining you anything here, you're just doing the same hardcoded sum over and over.
Instead, you should loop over an index into the array, and take one cell at a time and add it to a running total. In other words:
- Start with sum = 0
- Consider the first cell you want to add to the sum. That's (0,0). Take the value of that cell and add it to the sum variable, and store the result in the sum variable.
- Consider the next cell you want to add to the sum. That's (1,1). Take the value of that cell and add it to the sum variable, and store the result in the sum variable.
- Consider the next cell you want to add to the sum. That's (2,2). Take the value of that cell and add it to the sum variable, and store the result in the sum variable.
- Consider the next cell you want to add to the sum. That's (3,3). Take the value of that cell and add it to the sum variable, and store the result in the sum variable.
- The sum variable now contains the sum of the diagonal cells.
Now, when expressed like that, can you spot the repeated code that goes inside the For loop, and the bits that change that need to be part of the For loop expression?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|