Problem initializing an array because of a Parameter Index Error









up vote
1
down vote

favorite












This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.



 char array = digits.ToCharArray();

var intArray = new List<int>(new int[span]);

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

intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));


var data = new List<int>();
int n = 0;
while (data[n].Length == span)

data[n] = intArray.Skip(n).Take(span).ToArray();
n++;




Output:



Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53


Line 20 comes up in my IDE as while (data[n].Length == span).




Updated



I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition function is super helpful in this situation) but I still don't have a solution for my initial problem.



largestSeriesProduct[string_, int_Integer] := Module[numbers,lists,
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]


Which for example returns:



largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]

23520









share|improve this question























  • this is not a compiler error
    – Adrian
    Nov 10 at 17:41






  • 1




    The type/name of the exception itself should give you quite a big hint... ;-)
    – elgonzo
    Nov 10 at 17:43











  • n is the counter starting at zero and then n++
    – BBirdsell
    Nov 10 at 17:43










  • data is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
    – Disaffected 1070452
    Nov 10 at 17:45






  • 1




    Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
    – mjwills
    Nov 12 at 5:23














up vote
1
down vote

favorite












This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.



 char array = digits.ToCharArray();

var intArray = new List<int>(new int[span]);

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

intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));


var data = new List<int>();
int n = 0;
while (data[n].Length == span)

data[n] = intArray.Skip(n).Take(span).ToArray();
n++;




Output:



Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53


Line 20 comes up in my IDE as while (data[n].Length == span).




Updated



I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition function is super helpful in this situation) but I still don't have a solution for my initial problem.



largestSeriesProduct[string_, int_Integer] := Module[numbers,lists,
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]


Which for example returns:



largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]

23520









share|improve this question























  • this is not a compiler error
    – Adrian
    Nov 10 at 17:41






  • 1




    The type/name of the exception itself should give you quite a big hint... ;-)
    – elgonzo
    Nov 10 at 17:43











  • n is the counter starting at zero and then n++
    – BBirdsell
    Nov 10 at 17:43










  • data is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
    – Disaffected 1070452
    Nov 10 at 17:45






  • 1




    Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
    – mjwills
    Nov 12 at 5:23












up vote
1
down vote

favorite









up vote
1
down vote

favorite











This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.



 char array = digits.ToCharArray();

var intArray = new List<int>(new int[span]);

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

intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));


var data = new List<int>();
int n = 0;
while (data[n].Length == span)

data[n] = intArray.Skip(n).Take(span).ToArray();
n++;




Output:



Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53


Line 20 comes up in my IDE as while (data[n].Length == span).




Updated



I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition function is super helpful in this situation) but I still don't have a solution for my initial problem.



largestSeriesProduct[string_, int_Integer] := Module[numbers,lists,
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]


Which for example returns:



largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]

23520









share|improve this question















This is from a series of computer science exercises I'm working through for which I'm stuck on the following compiler error when trying to split a list of integers into sublists. I've tried many variations of initializing the list of integer arrays but can't seem to find a configuration that works in the documentation or on stack overflow.



 char array = digits.ToCharArray();

var intArray = new List<int>(new int[span]);

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

intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));


var data = new List<int>();
int n = 0;
while (data[n].Length == span)

data[n] = intArray.Skip(n).Take(span).ToArray();
n++;




Output:



Parameter name: index
at System.Collections.Generic.List`1.get_Item(Int32 index)
at LargestSeriesProduct.GetLargestProduct(String digits, Int32 span) in /Users/.../LargestSeriesProduct.cs:line 20
at seriesTest.Main() in /Users/.../LargestSeriesProduct.cs:line 53


Line 20 comes up in my IDE as while (data[n].Length == span).




Updated



I thought it might be a helpful exercise to write the code in Mathematica first to get more insight into the list of list problems I'm having in C#. It certainly was enlightening (the partition function is super helpful in this situation) but I still don't have a solution for my initial problem.



largestSeriesProduct[string_, int_Integer] := Module[numbers,lists,
numbers = ToExpression /@ Characters @ string;
lists = Times @@@ Partition[numbers,int,1];
Max @ lists
]


Which for example returns:



largestSeriesProduct["73167176531330624919225119674426574742355349194934",6]

23520






c#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 19:30

























asked Nov 10 at 17:40









BBirdsell

1194




1194











  • this is not a compiler error
    – Adrian
    Nov 10 at 17:41






  • 1




    The type/name of the exception itself should give you quite a big hint... ;-)
    – elgonzo
    Nov 10 at 17:43











  • n is the counter starting at zero and then n++
    – BBirdsell
    Nov 10 at 17:43










  • data is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
    – Disaffected 1070452
    Nov 10 at 17:45






  • 1




    Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
    – mjwills
    Nov 12 at 5:23
















  • this is not a compiler error
    – Adrian
    Nov 10 at 17:41






  • 1




    The type/name of the exception itself should give you quite a big hint... ;-)
    – elgonzo
    Nov 10 at 17:43











  • n is the counter starting at zero and then n++
    – BBirdsell
    Nov 10 at 17:43










  • data is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
    – Disaffected 1070452
    Nov 10 at 17:45






  • 1




    Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
    – mjwills
    Nov 12 at 5:23















this is not a compiler error
– Adrian
Nov 10 at 17:41




this is not a compiler error
– Adrian
Nov 10 at 17:41




1




1




The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43





The type/name of the exception itself should give you quite a big hint... ;-)
– elgonzo
Nov 10 at 17:43













n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43




n is the counter starting at zero and then n++
– BBirdsell
Nov 10 at 17:43












data is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
– Disaffected 1070452
Nov 10 at 17:45




data is not an array, it is a List. It is an empry List with nothing in it because it was just created 2 lines earlier. use the AWESOME Step Debugger
– Disaffected 1070452
Nov 10 at 17:45




1




1




Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23




Possible duplicate of What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?
– mjwills
Nov 12 at 5:23












2 Answers
2






active

oldest

votes

















up vote
0
down vote













to help you out:



var intArray = new List<int>(new int[span]);

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

intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));






share|improve this answer




















  • That is indeed an improvement. I've updated the question.
    – BBirdsell
    Nov 11 at 20:05










  • Is there a better option than ElementAt?
    – mjwills
    Nov 12 at 5:22










  • Yes, simply array[i]
    – Aldert
    Nov 12 at 7:19

















up vote
0
down vote













My solution.



 var intArray = new List<int>(new int[array.Length]);
for (int i = 0; i < array.Length; i++)

intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));

var data = new List<int>();
int n = 0;
while (intArray.Skip(n).Take(span).ToArray().Length == span)

data.Add(intArray.Skip(n).Take(span).ToArray());
n++;

var list = new List<long>(data.Count());
foreach (int nums in data)

list.Add((long)nums.Aggregate((total, next) => total * next));

long maxProduct = list.Max();
return maxProduct;





share|improve this answer




















    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',
    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%2f53241686%2fproblem-initializing-an-array-because-of-a-parameter-index-error%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    to help you out:



    var intArray = new List<int>(new int[span]);

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

    intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));






    share|improve this answer




















    • That is indeed an improvement. I've updated the question.
      – BBirdsell
      Nov 11 at 20:05










    • Is there a better option than ElementAt?
      – mjwills
      Nov 12 at 5:22










    • Yes, simply array[i]
      – Aldert
      Nov 12 at 7:19














    up vote
    0
    down vote













    to help you out:



    var intArray = new List<int>(new int[span]);

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

    intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));






    share|improve this answer




















    • That is indeed an improvement. I've updated the question.
      – BBirdsell
      Nov 11 at 20:05










    • Is there a better option than ElementAt?
      – mjwills
      Nov 12 at 5:22










    • Yes, simply array[i]
      – Aldert
      Nov 12 at 7:19












    up vote
    0
    down vote










    up vote
    0
    down vote









    to help you out:



    var intArray = new List<int>(new int[span]);

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

    intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));






    share|improve this answer












    to help you out:



    var intArray = new List<int>(new int[span]);

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

    intArray.Add((int)Char.GetNumericValue(array.ElementAt(i)));







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 10 at 18:46









    Aldert

    747210




    747210











    • That is indeed an improvement. I've updated the question.
      – BBirdsell
      Nov 11 at 20:05










    • Is there a better option than ElementAt?
      – mjwills
      Nov 12 at 5:22










    • Yes, simply array[i]
      – Aldert
      Nov 12 at 7:19
















    • That is indeed an improvement. I've updated the question.
      – BBirdsell
      Nov 11 at 20:05










    • Is there a better option than ElementAt?
      – mjwills
      Nov 12 at 5:22










    • Yes, simply array[i]
      – Aldert
      Nov 12 at 7:19















    That is indeed an improvement. I've updated the question.
    – BBirdsell
    Nov 11 at 20:05




    That is indeed an improvement. I've updated the question.
    – BBirdsell
    Nov 11 at 20:05












    Is there a better option than ElementAt?
    – mjwills
    Nov 12 at 5:22




    Is there a better option than ElementAt?
    – mjwills
    Nov 12 at 5:22












    Yes, simply array[i]
    – Aldert
    Nov 12 at 7:19




    Yes, simply array[i]
    – Aldert
    Nov 12 at 7:19












    up vote
    0
    down vote













    My solution.



     var intArray = new List<int>(new int[array.Length]);
    for (int i = 0; i < array.Length; i++)

    intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));

    var data = new List<int>();
    int n = 0;
    while (intArray.Skip(n).Take(span).ToArray().Length == span)

    data.Add(intArray.Skip(n).Take(span).ToArray());
    n++;

    var list = new List<long>(data.Count());
    foreach (int nums in data)

    list.Add((long)nums.Aggregate((total, next) => total * next));

    long maxProduct = list.Max();
    return maxProduct;





    share|improve this answer
























      up vote
      0
      down vote













      My solution.



       var intArray = new List<int>(new int[array.Length]);
      for (int i = 0; i < array.Length; i++)

      intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));

      var data = new List<int>();
      int n = 0;
      while (intArray.Skip(n).Take(span).ToArray().Length == span)

      data.Add(intArray.Skip(n).Take(span).ToArray());
      n++;

      var list = new List<long>(data.Count());
      foreach (int nums in data)

      list.Add((long)nums.Aggregate((total, next) => total * next));

      long maxProduct = list.Max();
      return maxProduct;





      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        My solution.



         var intArray = new List<int>(new int[array.Length]);
        for (int i = 0; i < array.Length; i++)

        intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));

        var data = new List<int>();
        int n = 0;
        while (intArray.Skip(n).Take(span).ToArray().Length == span)

        data.Add(intArray.Skip(n).Take(span).ToArray());
        n++;

        var list = new List<long>(data.Count());
        foreach (int nums in data)

        list.Add((long)nums.Aggregate((total, next) => total * next));

        long maxProduct = list.Max();
        return maxProduct;





        share|improve this answer












        My solution.



         var intArray = new List<int>(new int[array.Length]);
        for (int i = 0; i < array.Length; i++)

        intArray[i] = (int)Char.GetNumericValue(array.ElementAt(i));

        var data = new List<int>();
        int n = 0;
        while (intArray.Skip(n).Take(span).ToArray().Length == span)

        data.Add(intArray.Skip(n).Take(span).ToArray());
        n++;

        var list = new List<long>(data.Count());
        foreach (int nums in data)

        list.Add((long)nums.Aggregate((total, next) => total * next));

        long maxProduct = list.Max();
        return maxProduct;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 19:29









        BBirdsell

        1194




        1194



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53241686%2fproblem-initializing-an-array-because-of-a-parameter-index-error%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

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            政党

            天津地下鉄3号線