Reputation: 35
I have managed to write code that sums values, but only if I know how many values the user want to sum from the beginning (using array).
Here is my code that works fine, if the user first enters the number of values he/she wants to sum:
Public Class WholeNumbersForAdd
Private numberOfVal As Integer
Private sum As Integer
Private Sub NumbOfVal()
Console.Write("Numbers of values to sum? ")
numberOfVal = CInt(Console.ReadLine())
Console.WriteLine()
End Sub
Private Sub enterVal()
Dim nums(numberOfVal) As Integer
For i As Integer = 1 To numberOfVal Step 1
Console.Write("Please give the value no " & i & " (whole number): ")
nums(i) = CInt(Console.ReadLine)
Next
For Each value As Integer In nums
sum = sum + value
Next
End Sub
Now, I am trying to make changes that sum an undetermined number of values using a while-loop but I can't seem to get a grip of the While-loop, that I must use in this exercise. I want the program to keep taking in new values (one value per row) until the user presses the Q-button. When the user press Q, I want the program to sum all values that the user had entered.
Here is what I got so far, but I know that I am faaaaaar away from a solution.
Private Sub enterVal()
Dim nums As Double
Dim inputValue As Double
Dim sumValues As Double
While (inputValue <> Q)
Console.Write("Write an amount to sum or press Q to finish: ")
nums(inputValues) = CDbl(Console.ReadLine)
End While
For Each value As Integer In inputValue
sumValues = sumValues + value
Next
End Sub
Btw. I am not allowed to use "Do Loop While".
Upvotes: 0
Views: 2337
Reputation: 1
Module Module1
Sub Main()
Dim m, n, c, d As Int16
Dim first(,) As Int16 = New Int16(5, 5) {}
Dim second(,) As Int16 = New Int16(5, 5) {}
Dim sum(,) As Int16 = New Int16(5, 5) {}
Console.WriteLine("Enter the number of rows and columns of matrix")
m = Convert.ToInt16(Console.ReadLine())
n = Convert.ToInt16(Console.ReadLine())
Console.WriteLine("\nEnter the elements of first matrix\n")
c = 0
While c < m
d = 0
While d < n
Console.WriteLine("Enter Element [" + c.ToString() + " , " + d.ToString() + "]")
first(c, d) = Convert.ToInt16(Console.ReadLine())
d = d + 1
End While
c = c + 1
End While
Console.WriteLine("Enter the elements of second matrix")
c = 0
While c < m
d = 0
While d < n
Console.WriteLine("Enter Element [" + c.ToString() + " , " + d.ToString() + "]")
second(c, d) = Convert.ToInt16(Console.ReadLine())
d = d + 1
End While
c = c + 1
End While
c = 0
While c < m
d = 0
While d < n
sum(c, d) = first(c, d) + second(c, d)
d = d + 1
End While
c = c + 1
End While
Console.WriteLine("Sum of entered matrices:-")
c = 0
While c < m
d = 0
While d < n
Console.Write(" " + sum(c, d).ToString())
d = d + 1
End While
Console.WriteLine()
c = c + 1
End While
Console.ReadKey()
End Sub
End module
Upvotes: 0
Reputation: 2213
You are entering value through Console.ReadLine
so your While (inputValue <> Q)
will never work properly
While True
Console.Write("Write an amount to sum or press Q to finish: ")
Dim inputString as String = Console.ReadLine
If inputString.ToUpper = "Q" Then Exit While
nums(inputValues) = CDbl(inputString)
End While
You do not need a value in While, loop indefinitely, hence While True
Take the input into a string, because you are expecting Q, which cannot be a number as it was in your example.
Check if you entered q or Q (remove .ToUpper
if you don't need to check for lower case q) and exit the loop if it is.
The general design is not a good one, but I left it in in case that's part of your assignment.
NOTE: The code above is not tested
Upvotes: 1