Using specific array count The Next CEO of Stack OverflowHow do you convert a byte array to a hexadecimal string, and vice versa?How would you count occurrences of a string (actually a char) within a string?Comparing in ArrayExcluding value from array & Counting itWhy not inherit from List<T>?Array to Sort High ScoresC# Variable int assumes a different valueC# Looping int to array then countingRemove all similar elements in an array if count of element is less than 'n'How to manipulate List object or an array that will enable to calculate the Sum, Average, Count?

Finitely generated matrix groups whose eigenvalues are all algebraic

Free fall ellipse or parabola?

Small nick on power cord from an electric alarm clock, and copper wiring exposed but intact

logical reads on global temp table, but not on session-level temp table

Is it a bad idea to plug the other end of ESD strap to wall ground?

Compensation for working overtime on Saturdays

Simplify trigonometric expression using trigonometric identities

What did the word "leisure" mean in late 18th Century usage?

Why do we say “un seul M” and not “une seule M” even though M is a “consonne”?

Is it correct to say moon starry nights?

Calculating discount not working

Why was Sir Cadogan fired?

Can you teleport closer to a creature you are Frightened of?

How to pronounce fünf in 45

How dangerous is XSS

Can a PhD from a non-TU9 German university become a professor in a TU9 university?

The sum of any ten consecutive numbers from a fibonacci sequence is divisible by 11

Which acid/base does a strong base/acid react when added to a buffer solution?

What steps are necessary to read a Modern SSD in Medieval Europe?

Early programmable calculators with RS-232

What does this strange code stamp on my passport mean?

How do I secure a TV wall mount?

Find the majority element, which appears more than half the time

Man transported from Alternate World into ours by a Neutrino Detector



Using specific array count



The Next CEO of Stack OverflowHow do you convert a byte array to a hexadecimal string, and vice versa?How would you count occurrences of a string (actually a char) within a string?Comparing in ArrayExcluding value from array & Counting itWhy not inherit from List<T>?Array to Sort High ScoresC# Variable int assumes a different valueC# Looping int to array then countingRemove all similar elements in an array if count of element is less than 'n'How to manipulate List object or an array that will enable to calculate the Sum, Average, Count?










-1















My array max size is 20. If I were to enter data that would be less than 20,how do I get it where my program only counts the used arrays?



for (int i = 0; i < Score.Length; i++)

sum = sum + Score[i];


average = sum / Score.Length;


If I use this for loop above, it always divides by 20 for the average. I need it to only count the ones I entered, not 20. I would prefer solutions using arrays










share|improve this question



















  • 10





    It sounds like you want to use a List<T> rather than an array - that way you don't need to specify the size up-front.

    – Jon Skeet
    Mar 7 at 19:35











  • You need to keep track of how many items you add in a separate variable. If you want to have this done for you, then use a List instead.

    – Kenneth K.
    Mar 7 at 19:35











  • in that case use List<int> rather

    – Rahul
    Mar 7 at 19:36















-1















My array max size is 20. If I were to enter data that would be less than 20,how do I get it where my program only counts the used arrays?



for (int i = 0; i < Score.Length; i++)

sum = sum + Score[i];


average = sum / Score.Length;


If I use this for loop above, it always divides by 20 for the average. I need it to only count the ones I entered, not 20. I would prefer solutions using arrays










share|improve this question



















  • 10





    It sounds like you want to use a List<T> rather than an array - that way you don't need to specify the size up-front.

    – Jon Skeet
    Mar 7 at 19:35











  • You need to keep track of how many items you add in a separate variable. If you want to have this done for you, then use a List instead.

    – Kenneth K.
    Mar 7 at 19:35











  • in that case use List<int> rather

    – Rahul
    Mar 7 at 19:36













-1












-1








-1








My array max size is 20. If I were to enter data that would be less than 20,how do I get it where my program only counts the used arrays?



for (int i = 0; i < Score.Length; i++)

sum = sum + Score[i];


average = sum / Score.Length;


If I use this for loop above, it always divides by 20 for the average. I need it to only count the ones I entered, not 20. I would prefer solutions using arrays










share|improve this question
















My array max size is 20. If I were to enter data that would be less than 20,how do I get it where my program only counts the used arrays?



for (int i = 0; i < Score.Length; i++)

sum = sum + Score[i];


average = sum / Score.Length;


If I use this for loop above, it always divides by 20 for the average. I need it to only count the ones I entered, not 20. I would prefer solutions using arrays







c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 7 at 20:08









Andronicus

6,10621733




6,10621733










asked Mar 7 at 19:34









FalictFalict

32




32







  • 10





    It sounds like you want to use a List<T> rather than an array - that way you don't need to specify the size up-front.

    – Jon Skeet
    Mar 7 at 19:35











  • You need to keep track of how many items you add in a separate variable. If you want to have this done for you, then use a List instead.

    – Kenneth K.
    Mar 7 at 19:35











  • in that case use List<int> rather

    – Rahul
    Mar 7 at 19:36












  • 10





    It sounds like you want to use a List<T> rather than an array - that way you don't need to specify the size up-front.

    – Jon Skeet
    Mar 7 at 19:35











  • You need to keep track of how many items you add in a separate variable. If you want to have this done for you, then use a List instead.

    – Kenneth K.
    Mar 7 at 19:35











  • in that case use List<int> rather

    – Rahul
    Mar 7 at 19:36







10




10





It sounds like you want to use a List<T> rather than an array - that way you don't need to specify the size up-front.

– Jon Skeet
Mar 7 at 19:35





It sounds like you want to use a List<T> rather than an array - that way you don't need to specify the size up-front.

– Jon Skeet
Mar 7 at 19:35













You need to keep track of how many items you add in a separate variable. If you want to have this done for you, then use a List instead.

– Kenneth K.
Mar 7 at 19:35





You need to keep track of how many items you add in a separate variable. If you want to have this done for you, then use a List instead.

– Kenneth K.
Mar 7 at 19:35













in that case use List<int> rather

– Rahul
Mar 7 at 19:36





in that case use List<int> rather

– Rahul
Mar 7 at 19:36












3 Answers
3






active

oldest

votes


















0














If you insist in using arrays, then you must keep track of how many items you added to the array, like:



int[] Score = new int[20];
Random rdn = new Random();

int size=0;
for(int i=0;i<rdn.Next(0,20);i++)

Score[i] = rdn.Next();
size++;


int sum = 0;

for (int i = 0; i < size; i++)

sum = sum + Score[i];


double average = sum / size;


A better option is to use the List class that keep track for you of the number of items you add



List<int> Score = new List<int>();
Random rdn = new Random();


for(int i=0;i<rdn.Next(0,20);i++)

Score.Add(rdn.Next());


int sum = 0;

for (int i = 0; i < Score.Count; i++)

sum = sum + Score[i];

double average = sum / Score.Count;


And of course, as you didn't say the type of your data you could use other data types, like double, float, long, decimal for both solutions.






share|improve this answer






























    0














    That is probably an overkill, but another approach would be to use a SparseVector class of the Math.Numerics package:



    Sparse Vector uses two arrays which are usually much shorter than the vector. One array stores all values that are not zero, the other stores their indices.




    PM > Install-Package MathNet.Numerics




    var vector = SparseVector.Build.SparseOfArray(Score);
    var sum = vector.Sum();


    Sum() will only go through non-empty elements.






    share|improve this answer
































      -1














      You need to keep track of the record that are != 0, so



      int count = 0;
      for(int i = 0; i < array.Length; i++)

      if ( array[i] != 0 )

      count++;
      sum += array[i];


      average = sum / count;


      And beware of division by 0 ;)






      share|improve this answer




















      • 2





        OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

        – Magnetron
        Mar 7 at 20:08











      • @Magnetron what values hold an empty array of integers?

        – Davide Vitali
        Mar 7 at 20:11











      • Anyway, edited to count occurrences of used elements instead of just positive ones

        – Davide Vitali
        Mar 7 at 20:14











      • Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

        – Magnetron
        Mar 7 at 20:15






      • 1





        You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

        – UnholySheep
        Mar 7 at 20:34











      Your Answer






      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: "1"
      ;
      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',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051512%2fusing-specific-array-count%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      If you insist in using arrays, then you must keep track of how many items you added to the array, like:



      int[] Score = new int[20];
      Random rdn = new Random();

      int size=0;
      for(int i=0;i<rdn.Next(0,20);i++)

      Score[i] = rdn.Next();
      size++;


      int sum = 0;

      for (int i = 0; i < size; i++)

      sum = sum + Score[i];


      double average = sum / size;


      A better option is to use the List class that keep track for you of the number of items you add



      List<int> Score = new List<int>();
      Random rdn = new Random();


      for(int i=0;i<rdn.Next(0,20);i++)

      Score.Add(rdn.Next());


      int sum = 0;

      for (int i = 0; i < Score.Count; i++)

      sum = sum + Score[i];

      double average = sum / Score.Count;


      And of course, as you didn't say the type of your data you could use other data types, like double, float, long, decimal for both solutions.






      share|improve this answer



























        0














        If you insist in using arrays, then you must keep track of how many items you added to the array, like:



        int[] Score = new int[20];
        Random rdn = new Random();

        int size=0;
        for(int i=0;i<rdn.Next(0,20);i++)

        Score[i] = rdn.Next();
        size++;


        int sum = 0;

        for (int i = 0; i < size; i++)

        sum = sum + Score[i];


        double average = sum / size;


        A better option is to use the List class that keep track for you of the number of items you add



        List<int> Score = new List<int>();
        Random rdn = new Random();


        for(int i=0;i<rdn.Next(0,20);i++)

        Score.Add(rdn.Next());


        int sum = 0;

        for (int i = 0; i < Score.Count; i++)

        sum = sum + Score[i];

        double average = sum / Score.Count;


        And of course, as you didn't say the type of your data you could use other data types, like double, float, long, decimal for both solutions.






        share|improve this answer

























          0












          0








          0







          If you insist in using arrays, then you must keep track of how many items you added to the array, like:



          int[] Score = new int[20];
          Random rdn = new Random();

          int size=0;
          for(int i=0;i<rdn.Next(0,20);i++)

          Score[i] = rdn.Next();
          size++;


          int sum = 0;

          for (int i = 0; i < size; i++)

          sum = sum + Score[i];


          double average = sum / size;


          A better option is to use the List class that keep track for you of the number of items you add



          List<int> Score = new List<int>();
          Random rdn = new Random();


          for(int i=0;i<rdn.Next(0,20);i++)

          Score.Add(rdn.Next());


          int sum = 0;

          for (int i = 0; i < Score.Count; i++)

          sum = sum + Score[i];

          double average = sum / Score.Count;


          And of course, as you didn't say the type of your data you could use other data types, like double, float, long, decimal for both solutions.






          share|improve this answer













          If you insist in using arrays, then you must keep track of how many items you added to the array, like:



          int[] Score = new int[20];
          Random rdn = new Random();

          int size=0;
          for(int i=0;i<rdn.Next(0,20);i++)

          Score[i] = rdn.Next();
          size++;


          int sum = 0;

          for (int i = 0; i < size; i++)

          sum = sum + Score[i];


          double average = sum / size;


          A better option is to use the List class that keep track for you of the number of items you add



          List<int> Score = new List<int>();
          Random rdn = new Random();


          for(int i=0;i<rdn.Next(0,20);i++)

          Score.Add(rdn.Next());


          int sum = 0;

          for (int i = 0; i < Score.Count; i++)

          sum = sum + Score[i];

          double average = sum / Score.Count;


          And of course, as you didn't say the type of your data you could use other data types, like double, float, long, decimal for both solutions.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 7 at 20:27









          MagnetronMagnetron

          2,9141922




          2,9141922























              0














              That is probably an overkill, but another approach would be to use a SparseVector class of the Math.Numerics package:



              Sparse Vector uses two arrays which are usually much shorter than the vector. One array stores all values that are not zero, the other stores their indices.




              PM > Install-Package MathNet.Numerics




              var vector = SparseVector.Build.SparseOfArray(Score);
              var sum = vector.Sum();


              Sum() will only go through non-empty elements.






              share|improve this answer





























                0














                That is probably an overkill, but another approach would be to use a SparseVector class of the Math.Numerics package:



                Sparse Vector uses two arrays which are usually much shorter than the vector. One array stores all values that are not zero, the other stores their indices.




                PM > Install-Package MathNet.Numerics




                var vector = SparseVector.Build.SparseOfArray(Score);
                var sum = vector.Sum();


                Sum() will only go through non-empty elements.






                share|improve this answer



























                  0












                  0








                  0







                  That is probably an overkill, but another approach would be to use a SparseVector class of the Math.Numerics package:



                  Sparse Vector uses two arrays which are usually much shorter than the vector. One array stores all values that are not zero, the other stores their indices.




                  PM > Install-Package MathNet.Numerics




                  var vector = SparseVector.Build.SparseOfArray(Score);
                  var sum = vector.Sum();


                  Sum() will only go through non-empty elements.






                  share|improve this answer















                  That is probably an overkill, but another approach would be to use a SparseVector class of the Math.Numerics package:



                  Sparse Vector uses two arrays which are usually much shorter than the vector. One array stores all values that are not zero, the other stores their indices.




                  PM > Install-Package MathNet.Numerics




                  var vector = SparseVector.Build.SparseOfArray(Score);
                  var sum = vector.Sum();


                  Sum() will only go through non-empty elements.







                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 7 at 20:44









                  Wai Ha Lee

                  6,115124166




                  6,115124166










                  answered Mar 7 at 20:35









                  koryakinpkoryakinp

                  1,77121140




                  1,77121140





















                      -1














                      You need to keep track of the record that are != 0, so



                      int count = 0;
                      for(int i = 0; i < array.Length; i++)

                      if ( array[i] != 0 )

                      count++;
                      sum += array[i];


                      average = sum / count;


                      And beware of division by 0 ;)






                      share|improve this answer




















                      • 2





                        OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

                        – Magnetron
                        Mar 7 at 20:08











                      • @Magnetron what values hold an empty array of integers?

                        – Davide Vitali
                        Mar 7 at 20:11











                      • Anyway, edited to count occurrences of used elements instead of just positive ones

                        – Davide Vitali
                        Mar 7 at 20:14











                      • Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

                        – Magnetron
                        Mar 7 at 20:15






                      • 1





                        You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

                        – UnholySheep
                        Mar 7 at 20:34















                      -1














                      You need to keep track of the record that are != 0, so



                      int count = 0;
                      for(int i = 0; i < array.Length; i++)

                      if ( array[i] != 0 )

                      count++;
                      sum += array[i];


                      average = sum / count;


                      And beware of division by 0 ;)






                      share|improve this answer




















                      • 2





                        OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

                        – Magnetron
                        Mar 7 at 20:08











                      • @Magnetron what values hold an empty array of integers?

                        – Davide Vitali
                        Mar 7 at 20:11











                      • Anyway, edited to count occurrences of used elements instead of just positive ones

                        – Davide Vitali
                        Mar 7 at 20:14











                      • Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

                        – Magnetron
                        Mar 7 at 20:15






                      • 1





                        You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

                        – UnholySheep
                        Mar 7 at 20:34













                      -1












                      -1








                      -1







                      You need to keep track of the record that are != 0, so



                      int count = 0;
                      for(int i = 0; i < array.Length; i++)

                      if ( array[i] != 0 )

                      count++;
                      sum += array[i];


                      average = sum / count;


                      And beware of division by 0 ;)






                      share|improve this answer















                      You need to keep track of the record that are != 0, so



                      int count = 0;
                      for(int i = 0; i < array.Length; i++)

                      if ( array[i] != 0 )

                      count++;
                      sum += array[i];


                      average = sum / count;


                      And beware of division by 0 ;)







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 7 at 20:17

























                      answered Mar 7 at 19:42









                      Davide VitaliDavide Vitali

                      694316




                      694316







                      • 2





                        OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

                        – Magnetron
                        Mar 7 at 20:08











                      • @Magnetron what values hold an empty array of integers?

                        – Davide Vitali
                        Mar 7 at 20:11











                      • Anyway, edited to count occurrences of used elements instead of just positive ones

                        – Davide Vitali
                        Mar 7 at 20:14











                      • Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

                        – Magnetron
                        Mar 7 at 20:15






                      • 1





                        You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

                        – UnholySheep
                        Mar 7 at 20:34












                      • 2





                        OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

                        – Magnetron
                        Mar 7 at 20:08











                      • @Magnetron what values hold an empty array of integers?

                        – Davide Vitali
                        Mar 7 at 20:11











                      • Anyway, edited to count occurrences of used elements instead of just positive ones

                        – Davide Vitali
                        Mar 7 at 20:14











                      • Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

                        – Magnetron
                        Mar 7 at 20:15






                      • 1





                        You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

                        – UnholySheep
                        Mar 7 at 20:34







                      2




                      2





                      OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

                      – Magnetron
                      Mar 7 at 20:08





                      OP doesn't say that the valid items are positive. He wants to know the used range of the array, so he need a variable to keep the count at the time the array was filled, not at the time of the sum.

                      – Magnetron
                      Mar 7 at 20:08













                      @Magnetron what values hold an empty array of integers?

                      – Davide Vitali
                      Mar 7 at 20:11





                      @Magnetron what values hold an empty array of integers?

                      – Davide Vitali
                      Mar 7 at 20:11













                      Anyway, edited to count occurrences of used elements instead of just positive ones

                      – Davide Vitali
                      Mar 7 at 20:14





                      Anyway, edited to count occurrences of used elements instead of just positive ones

                      – Davide Vitali
                      Mar 7 at 20:14













                      Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

                      – Magnetron
                      Mar 7 at 20:15





                      Althought the default value for integers is zero, you can't tell if a zero value is because the item of the array is undefined by the user or if the user set it's value for zero. So if the array is [1,2,0,3,4,...] or [1,2,-1,3,4,...] your code will fail.

                      – Magnetron
                      Mar 7 at 20:15




                      1




                      1





                      You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

                      – UnholySheep
                      Mar 7 at 20:34





                      You could use an array of nullable integers (int?[]) but that seems like just adding a lot of unnecessary complexity compared to a List<int>

                      – UnholySheep
                      Mar 7 at 20:34

















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051512%2fusing-specific-array-count%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      AWS Lex not identifying response if by a variable The 2019 Stack Overflow Developer Survey Results Are In Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) The Ask Question Wizard is Live! Data science time! April 2019 and salary with experienceEnforcing custom enumeration in AWS LEX for slot valuesHow to give response based on user response in Amazon Lex?Intercepting AWS Lambda Response to a AWS Lex QueryLex chat bot error: Reached second execution of fulfillment lambda on the same utteranceamazon lex showing invalid responseLambda response send back to Lex slot?Response card in Amazon lexAmazon Lex - Lambda response return HTML to botHow can I solve 424 (Failed Dependency) (python) obtained from Amazon lex?

                      Алба-Юлія

                      Захаров Федір Захарович