Inputting 10 numbers in an array

The name of the pictureThe name of the pictureThe name of the pictureClash 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






share|improve this question



























    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






    share|improve this question























      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






      share|improve this question













      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








      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 8 at 21:37









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Apr 8 at 5:41









      jeffery lam

      261




      261




















          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



          1. Loop rather than repeat. Much easier to maintain

          2. Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.

          3. 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 10 and then working out if that needs to change to 12 or if it really is 10.





          share|improve this answer




























            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/






            share|improve this answer





















              Your Answer




              StackExchange.ifUsing("editor", function ()
              return StackExchange.using("mathjaxEditing", function ()
              StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
              StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
              );
              );
              , "mathjax-editing");

              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              convertImagesToLinks: false,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );








               

              draft saved


              draft discarded


















              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






























              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



              1. Loop rather than repeat. Much easier to maintain

              2. Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.

              3. 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 10 and then working out if that needs to change to 12 or if it really is 10.





              share|improve this answer

























                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



                1. Loop rather than repeat. Much easier to maintain

                2. Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.

                3. 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 10 and then working out if that needs to change to 12 or if it really is 10.





                share|improve this answer























                  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



                  1. Loop rather than repeat. Much easier to maintain

                  2. Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.

                  3. 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 10 and then working out if that needs to change to 12 or if it really is 10.





                  share|improve this answer













                  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



                  1. Loop rather than repeat. Much easier to maintain

                  2. Reuse loops where you can - it is much more efficient. Think about your program logic so you can be efficient with loops.

                  3. 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 10 and then working out if that needs to change to 12 or if it really is 10.






                  share|improve this answer













                  share|improve this answer



                  share|improve this answer











                  answered Apr 8 at 6:41









                  AJD

                  1,0251213




                  1,0251213






















                      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/






                      share|improve this answer

























                        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/






                        share|improve this answer























                          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/






                          share|improve this answer













                          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/







                          share|improve this answer













                          share|improve this answer



                          share|improve this answer











                          answered Apr 21 at 21:32









                          Brian J

                          26618




                          26618






















                               

                              draft saved


                              draft discarded


























                               


                              draft saved


                              draft discarded














                              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













































































                              Popular posts from this blog

                              Python Lists

                              Aion

                              JavaScript Array Iteration Methods