Question What am I doing wrong? - Logic error

Mynotoar

Active member
Joined
Sep 27, 2012
Messages
28
Location
England
Programming Experience
Beginner
Hey guys - I came across a programming problem that I wanted to solve, and I've almost got a program working that does what I want it to do, but not quite.

What I want to do is to create a digital river - this is where you add up every digit within a number, and then add that result to the number. For example, for the river of 456, you get 471, then 493, then 509. 4+5+6 = 15, and 456 + 15 = 471. The code I've got to get it working is, I reckon, stupidly overcomplicated, as you'll see from the code - I start by using a For loop to iterate through every number using a Mid. But because I couldn't get the Mid to read from a number, I had to convert River (the number you put in to generate a River,) into a string, and then for all ten numbers, convert it back into an integer value. So for every digit inside the number, it goes through it and adds it together, then once it's finished going through the number, it adds that sum to the first number declared as river.

VB.NET:
Sub Main()
Dim Index As Integer
    Dim DigitInRiver As String
    Dim River, IterationsOfRiver As Integer
    Dim AddToRiver As Integer
        Console.Write("Enter a number to make a Digital River from: ")
        River = Console.ReadLine
        Console.Write("How many iterations of the river would you like to display: ")
        IterationsOfRiver = Console.ReadLine
        Console.Write("River: ")
        Console.ForegroundColor = ConsoleColor.Cyan
        Console.Write(River & " ")
        For h = 1 To IterationsOfRiver
            For i = 1 To Len(River)
                Index += 1
                River.ToString()
                DigitInRiver = Mid(River, Index, 1)
                If DigitInRiver = "1" Then
                    AddToRiver += 1
                ElseIf DigitInRiver = "2" Then
                    AddToRiver += 2
                ElseIf DigitInRiver = "3" Then
                    AddToRiver += 3
                ElseIf DigitInRiver = "4" Then
                    AddToRiver += 4
                ElseIf DigitInRiver = "5" Then
                    AddToRiver += 5
                ElseIf DigitInRiver = "6" Then
                    AddToRiver += 6
                ElseIf DigitInRiver = "7" Then
                    AddToRiver += 7
                ElseIf DigitInRiver = "8" Then
                    AddToRiver += 8
                ElseIf DigitInRiver = "9" Then
                    AddToRiver += 9
                ElseIf DigitInRiver = "0" Then
                    AddToRiver += 0
                End If
            Next
            River += AddToRiver
            Console.Write(River & " ")
        Next
        Console.ResetColor()
        Console.ReadKey()
Sub Main()

For the first iteration, it works wonderfully. If I type in 48, it will correctly tell me that the next number in the River is 60, because 4+8 is 12, and 48+12 = 60. But then it keeps adding 12 to the sequence to get 72, 84, etc... it should go through the For loop again for however many iterations you specified and each time give a new value for the AddToRiver variable (which is what is added onto River,) but no, it defines AddToRiver once and then uses that value all the way through. I've tried setting it to 0 at various places throughout the For loop and it hasn't worked. It may be something to do with the Index variable, which I used to iterate through the digits, but when I tried setting that to 0 in various places that also didn't work.

Can anyone help me to understand what's gone wrong here?
 
Try something like this instead:

Private Function GetRiver(RiverSeed As Integer, Iterations As Integer) As List(Of Integer)
    GetRiver = New List(Of Integer)
    
    Dim CurrentRiver As Integer = RiverSeed
    For I = 1 To Iterations
        Dim strRiver As String = CurrentRiver.ToString
        For J = 0 To strRiver.Length - 1
            CurrentRiver += CInt(strRiver.SubString(J, 1))
        Next
        GetRiver.Add(CurrentRiver)
    Next
End Function
 
Here's how you should be approaching problems like this. First, you know that you need to calculate a river, so write a function to do that. The function will take a number as an argument, break it up into digits, add them all to the original number and return the result. Write and test it and, when you're happy it's working, that's a unit of functionality that you can use anywhere else with a single method call. Next, you know that you need to calculate the river for a number of iterations. The function will take a number and a count and, in a loop, call your first function. This second function is much simpler because the actual calculation of the river for each iteration is a single call to a method that you already know is working, so it's much easier to read, test and isolate any errors. Once that function is working you can then move on and write higher level functionality that will call that method, Continue like this until you have the whole thing working.
 
Back
Top