Inputting 10 numbers in an array

Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
I would like some tips on how I could simplify my code. Basically what I am trying to do is to input 10 numbers in an array but this looks messy so I am trying to change it to look less messy.
Imports System
Public Module Module1
Public Sub Main()
Dim score(10) as decimal
Console.WriteLine("Enter the score")
score(0) = Console.ReadLine
Console.WriteLine("Enter the score")
score(1) = Console.ReadLine
Console.WriteLine("Enter the score")
score(2) = Console.ReadLine
Console.WriteLine("Enter the score")
score(3) = Console.ReadLine
Console.WriteLine("Enter the score")
score(4) = Console.ReadLine
Console.WriteLine("Enter the score")
score(5) = Console.ReadLine
Console.WriteLine("Enter the score")
score(6) = Console.ReadLine
Console.WriteLine("Enter the score")
score(7) = Console.ReadLine
Console.WriteLine("Enter the score")
score(8) = Console.ReadLine
Console.WriteLine("Enter the score")
score(9) = Console.ReadLine
Console.WriteLine(score(0) + score(1) + score(2) + score(3) + score(4) + score(5) + score(6) + score(7) + score(8) + score(9))
End Sub
beginner vb.net
add a comment |Â
up vote
5
down vote
favorite
I would like some tips on how I could simplify my code. Basically what I am trying to do is to input 10 numbers in an array but this looks messy so I am trying to change it to look less messy.
Imports System
Public Module Module1
Public Sub Main()
Dim score(10) as decimal
Console.WriteLine("Enter the score")
score(0) = Console.ReadLine
Console.WriteLine("Enter the score")
score(1) = Console.ReadLine
Console.WriteLine("Enter the score")
score(2) = Console.ReadLine
Console.WriteLine("Enter the score")
score(3) = Console.ReadLine
Console.WriteLine("Enter the score")
score(4) = Console.ReadLine
Console.WriteLine("Enter the score")
score(5) = Console.ReadLine
Console.WriteLine("Enter the score")
score(6) = Console.ReadLine
Console.WriteLine("Enter the score")
score(7) = Console.ReadLine
Console.WriteLine("Enter the score")
score(8) = Console.ReadLine
Console.WriteLine("Enter the score")
score(9) = Console.ReadLine
Console.WriteLine(score(0) + score(1) + score(2) + score(3) + score(4) + score(5) + score(6) + score(7) + score(8) + score(9))
End Sub
beginner vb.net
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I would like some tips on how I could simplify my code. Basically what I am trying to do is to input 10 numbers in an array but this looks messy so I am trying to change it to look less messy.
Imports System
Public Module Module1
Public Sub Main()
Dim score(10) as decimal
Console.WriteLine("Enter the score")
score(0) = Console.ReadLine
Console.WriteLine("Enter the score")
score(1) = Console.ReadLine
Console.WriteLine("Enter the score")
score(2) = Console.ReadLine
Console.WriteLine("Enter the score")
score(3) = Console.ReadLine
Console.WriteLine("Enter the score")
score(4) = Console.ReadLine
Console.WriteLine("Enter the score")
score(5) = Console.ReadLine
Console.WriteLine("Enter the score")
score(6) = Console.ReadLine
Console.WriteLine("Enter the score")
score(7) = Console.ReadLine
Console.WriteLine("Enter the score")
score(8) = Console.ReadLine
Console.WriteLine("Enter the score")
score(9) = Console.ReadLine
Console.WriteLine(score(0) + score(1) + score(2) + score(3) + score(4) + score(5) + score(6) + score(7) + score(8) + score(9))
End Sub
beginner vb.net
I would like some tips on how I could simplify my code. Basically what I am trying to do is to input 10 numbers in an array but this looks messy so I am trying to change it to look less messy.
Imports System
Public Module Module1
Public Sub Main()
Dim score(10) as decimal
Console.WriteLine("Enter the score")
score(0) = Console.ReadLine
Console.WriteLine("Enter the score")
score(1) = Console.ReadLine
Console.WriteLine("Enter the score")
score(2) = Console.ReadLine
Console.WriteLine("Enter the score")
score(3) = Console.ReadLine
Console.WriteLine("Enter the score")
score(4) = Console.ReadLine
Console.WriteLine("Enter the score")
score(5) = Console.ReadLine
Console.WriteLine("Enter the score")
score(6) = Console.ReadLine
Console.WriteLine("Enter the score")
score(7) = Console.ReadLine
Console.WriteLine("Enter the score")
score(8) = Console.ReadLine
Console.WriteLine("Enter the score")
score(9) = Console.ReadLine
Console.WriteLine(score(0) + score(1) + score(2) + score(3) + score(4) + score(5) + score(6) + score(7) + score(8) + score(9))
End Sub
beginner vb.net
edited Apr 8 at 21:37
Jamalâ¦
30.1k11114225
30.1k11114225
asked Apr 8 at 5:41
jeffery lam
261
261
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
Repetition. Always a sign that something could be improved. DRY (Don't repeat yourself) is the mantra here.
Magic Numbers. Passable for one-off code, but can get in the way if you wish to maintain or extend the code.
I will deal with the magic numbers first (and you'll see why when I address the repetition)
You have Dim score(10) as decimal where there is no real reason for 10 except that it is a nice number. We can fix this (may seem superfluous at first, but it is a good habit) by the following:
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Now, if you wanted to work with 12 numbers you only have to change the assignment of numOfInputs. You have two areas of repetition. Addressing the main block:
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
Next iterator
See how I was also able to "personalise" the prompt string! Now if you want to do something else in here, you only have to fix the code once, and not ten times.
Addressing the output. There are a couple of ways to do this. One of those is to set up another loop, loop through the array and then use the output. But we already have the exact same loop that we would use to add them all up.
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Dim finalCount as decimal = 0 'OK, the default value is 0 but I like to be explicit
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
finalCount += score(iterator) ' simply add up the items as we go along.
Next iterator
As you can see, I have now put in place finalCount so that we can add up as we go along - which is what I would have done if I had it in a separate loop anyway.
Oh, and now you can print it out through Console.WriteLine(finalCount).
Key lessons
- Loop rather than repeat. Much easier to maintain
- Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.
- Replace magic numbers with variables (constants) that are defined somewhere logical. Much easier to replace a well-named constant only once than trying to find that
10and then working out if that needs to change to12or if it really is10.
add a comment |Â
up vote
1
down vote
When you declare an array in VB.NET, the number you provide is the last index you can access.
So the line
Dim score(10) as decimal
means that score really has 11 numbers in it. It's one of the many differences between C# and VB.NET.
The wasted space isn't huge, but writing a loop to score.Length will give unexpected results when you get to the extra element you didn't know you had.
Here is some more information about arrays in VB.NET:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
Repetition. Always a sign that something could be improved. DRY (Don't repeat yourself) is the mantra here.
Magic Numbers. Passable for one-off code, but can get in the way if you wish to maintain or extend the code.
I will deal with the magic numbers first (and you'll see why when I address the repetition)
You have Dim score(10) as decimal where there is no real reason for 10 except that it is a nice number. We can fix this (may seem superfluous at first, but it is a good habit) by the following:
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Now, if you wanted to work with 12 numbers you only have to change the assignment of numOfInputs. You have two areas of repetition. Addressing the main block:
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
Next iterator
See how I was also able to "personalise" the prompt string! Now if you want to do something else in here, you only have to fix the code once, and not ten times.
Addressing the output. There are a couple of ways to do this. One of those is to set up another loop, loop through the array and then use the output. But we already have the exact same loop that we would use to add them all up.
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Dim finalCount as decimal = 0 'OK, the default value is 0 but I like to be explicit
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
finalCount += score(iterator) ' simply add up the items as we go along.
Next iterator
As you can see, I have now put in place finalCount so that we can add up as we go along - which is what I would have done if I had it in a separate loop anyway.
Oh, and now you can print it out through Console.WriteLine(finalCount).
Key lessons
- Loop rather than repeat. Much easier to maintain
- Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.
- Replace magic numbers with variables (constants) that are defined somewhere logical. Much easier to replace a well-named constant only once than trying to find that
10and then working out if that needs to change to12or if it really is10.
add a comment |Â
up vote
4
down vote
Repetition. Always a sign that something could be improved. DRY (Don't repeat yourself) is the mantra here.
Magic Numbers. Passable for one-off code, but can get in the way if you wish to maintain or extend the code.
I will deal with the magic numbers first (and you'll see why when I address the repetition)
You have Dim score(10) as decimal where there is no real reason for 10 except that it is a nice number. We can fix this (may seem superfluous at first, but it is a good habit) by the following:
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Now, if you wanted to work with 12 numbers you only have to change the assignment of numOfInputs. You have two areas of repetition. Addressing the main block:
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
Next iterator
See how I was also able to "personalise" the prompt string! Now if you want to do something else in here, you only have to fix the code once, and not ten times.
Addressing the output. There are a couple of ways to do this. One of those is to set up another loop, loop through the array and then use the output. But we already have the exact same loop that we would use to add them all up.
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Dim finalCount as decimal = 0 'OK, the default value is 0 but I like to be explicit
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
finalCount += score(iterator) ' simply add up the items as we go along.
Next iterator
As you can see, I have now put in place finalCount so that we can add up as we go along - which is what I would have done if I had it in a separate loop anyway.
Oh, and now you can print it out through Console.WriteLine(finalCount).
Key lessons
- Loop rather than repeat. Much easier to maintain
- Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.
- Replace magic numbers with variables (constants) that are defined somewhere logical. Much easier to replace a well-named constant only once than trying to find that
10and then working out if that needs to change to12or if it really is10.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
Repetition. Always a sign that something could be improved. DRY (Don't repeat yourself) is the mantra here.
Magic Numbers. Passable for one-off code, but can get in the way if you wish to maintain or extend the code.
I will deal with the magic numbers first (and you'll see why when I address the repetition)
You have Dim score(10) as decimal where there is no real reason for 10 except that it is a nice number. We can fix this (may seem superfluous at first, but it is a good habit) by the following:
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Now, if you wanted to work with 12 numbers you only have to change the assignment of numOfInputs. You have two areas of repetition. Addressing the main block:
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
Next iterator
See how I was also able to "personalise" the prompt string! Now if you want to do something else in here, you only have to fix the code once, and not ten times.
Addressing the output. There are a couple of ways to do this. One of those is to set up another loop, loop through the array and then use the output. But we already have the exact same loop that we would use to add them all up.
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Dim finalCount as decimal = 0 'OK, the default value is 0 but I like to be explicit
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
finalCount += score(iterator) ' simply add up the items as we go along.
Next iterator
As you can see, I have now put in place finalCount so that we can add up as we go along - which is what I would have done if I had it in a separate loop anyway.
Oh, and now you can print it out through Console.WriteLine(finalCount).
Key lessons
- Loop rather than repeat. Much easier to maintain
- Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.
- Replace magic numbers with variables (constants) that are defined somewhere logical. Much easier to replace a well-named constant only once than trying to find that
10and then working out if that needs to change to12or if it really is10.
Repetition. Always a sign that something could be improved. DRY (Don't repeat yourself) is the mantra here.
Magic Numbers. Passable for one-off code, but can get in the way if you wish to maintain or extend the code.
I will deal with the magic numbers first (and you'll see why when I address the repetition)
You have Dim score(10) as decimal where there is no real reason for 10 except that it is a nice number. We can fix this (may seem superfluous at first, but it is a good habit) by the following:
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Now, if you wanted to work with 12 numbers you only have to change the assignment of numOfInputs. You have two areas of repetition. Addressing the main block:
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
Next iterator
See how I was also able to "personalise" the prompt string! Now if you want to do something else in here, you only have to fix the code once, and not ten times.
Addressing the output. There are a couple of ways to do this. One of those is to set up another loop, loop through the array and then use the output. But we already have the exact same loop that we would use to add them all up.
Const numOfInputs as Long = 10 ' You can always call it what you like, but make it sensible and descriptive
Dim score(numOfInputs) as decimal
Dim finalCount as decimal = 0 'OK, the default value is 0 but I like to be explicit
For iterator as Long = 0 to numOfInputs - 1 ' 0 based array.
Console.WriteLine("Enter the score (input number " & iterator & ")")
score(iterator) = Console.ReadLine
finalCount += score(iterator) ' simply add up the items as we go along.
Next iterator
As you can see, I have now put in place finalCount so that we can add up as we go along - which is what I would have done if I had it in a separate loop anyway.
Oh, and now you can print it out through Console.WriteLine(finalCount).
Key lessons
- Loop rather than repeat. Much easier to maintain
- Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.
- Replace magic numbers with variables (constants) that are defined somewhere logical. Much easier to replace a well-named constant only once than trying to find that
10and then working out if that needs to change to12or if it really is10.
answered Apr 8 at 6:41
AJD
1,0251213
1,0251213
add a comment |Â
add a comment |Â
up vote
1
down vote
When you declare an array in VB.NET, the number you provide is the last index you can access.
So the line
Dim score(10) as decimal
means that score really has 11 numbers in it. It's one of the many differences between C# and VB.NET.
The wasted space isn't huge, but writing a loop to score.Length will give unexpected results when you get to the extra element you didn't know you had.
Here is some more information about arrays in VB.NET:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
add a comment |Â
up vote
1
down vote
When you declare an array in VB.NET, the number you provide is the last index you can access.
So the line
Dim score(10) as decimal
means that score really has 11 numbers in it. It's one of the many differences between C# and VB.NET.
The wasted space isn't huge, but writing a loop to score.Length will give unexpected results when you get to the extra element you didn't know you had.
Here is some more information about arrays in VB.NET:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
add a comment |Â
up vote
1
down vote
up vote
1
down vote
When you declare an array in VB.NET, the number you provide is the last index you can access.
So the line
Dim score(10) as decimal
means that score really has 11 numbers in it. It's one of the many differences between C# and VB.NET.
The wasted space isn't huge, but writing a loop to score.Length will give unexpected results when you get to the extra element you didn't know you had.
Here is some more information about arrays in VB.NET:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
When you declare an array in VB.NET, the number you provide is the last index you can access.
So the line
Dim score(10) as decimal
means that score really has 11 numbers in it. It's one of the many differences between C# and VB.NET.
The wasted space isn't huge, but writing a loop to score.Length will give unexpected results when you get to the extra element you didn't know you had.
Here is some more information about arrays in VB.NET:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/arrays/
answered Apr 21 at 21:32
Brian J
26618
26618
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f191510%2finputting-10-numbers-in-an-array%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password