-3

I'm a total beginner. I'm trying to find Tribonnaci numbers but I don't understand why I'm getting this error.

System.IndexOutOfRangeException

btw that's a method i didnt put all the line but first line was public static void TribonacciNumbs(int n)

edit: thanks to replies i undertand i was creating a new array instead of adding new value which what i want to do

i changed for loop as these lines

Array.Resize(ref tribo, i+1);

num = tribo[i-1]+tribo[i-2]+tribo[i-3];

tribo[i]=num;

now it works

using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;

namespace TestArea
{
    internal class Program
    {
        static void Main(string[] args)
        {
            TribonacciNumbs(4);

            Console.ReadLine();
        }

        public static void TribonacciNumbs(int n)
        {
            int[] tribo;
            tribo = new int[] { 0, 1, 1 };

            int num=0;
            if (n >= 3)
            {
                for (int i = 3; i <= n; i++)
                {
                    num = tribo[i - 1] + tribo[i - 2] + tribo[i - 3];
                    tribo = new int[] {num};
                      
                }
            }

            if (n == 0)
            {
                Console.WriteLine(0);
            }else if (n == 1)
            {
                Console.WriteLine(1);
            }else if(n == 2)
            {
                Console.WriteLine(1);
            }else Console.WriteLine(tribo[n]);
           
        }
        
    }
}


playsf
  • 5
  • 2
  • 2
    Why did somebody upvote a question that doesn't even tell us what the error is? – ProgrammingLlama Oct 16 '22 at 13:43
  • Rounding up...way up/ – Ňɏssa Pøngjǣrdenlarp Oct 16 '22 at 13:45
  • 2
    `tribo = new int[] { num };` what do you think this does? – bolov Oct 16 '22 at 13:51
  • Does [this](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) answer your question? – ProgrammingLlama Oct 16 '22 at 13:51
  • @bolov adding new value to array? – playsf Oct 16 '22 at 13:52
  • if `i == 4` then `i - 1 == 3`. Even from the beginning the `tribo` array contains only 3 items (addressable at indices 0, 1, and 2). How would you possibly access an item as index 3 if there's only 0, 1, and 2? Furthermore, you're replacing the array with a 1-item array in the first iteration of the loop... so how would accessing at any other index than 0 possibly work? It can't. – ProgrammingLlama Oct 16 '22 at 13:53
  • 3
    No, it doesn't. It creates a *new* array, and assigns a reference to that array to the variable `tribo`. So after that statement, `tribo.Length` is 1. If you want a collection that you can expand, consider using `List` instead, and the `Add` method. – Jon Skeet Oct 16 '22 at 13:54
  • welcome to stackoverflow playsf! please take a [tour] and learn [ask]. i do think this question can be polished if you clarified what you want to do and few edits to make it more readable. – Bagus Tesa Oct 16 '22 at 14:02
  • [Tutorial: Learn to debug C# code using Visual Studio](https://learn.microsoft.com/en-us/visualstudio/get-started/csharp/tutorial-debugger?view=vs-2022) – Luuk Oct 16 '22 at 14:06
  • Why are these lines in your code: 1) `using Microsoft.Win32;` 2) `using System.Runtime.InteropServices.WindowsRuntime;` 3) `using System.Security.Cryptography.X509Certificates;` – Luuk Oct 16 '22 at 14:26
  • @Luuk not sure, i was using same file for testing/trying things. – playsf Oct 16 '22 at 14:53

3 Answers3

1

You're reinitializing your tribo array to only hold a single number.

tribo = new int[] {num};

But in the next step of your loop, you're checking for 3 spots in this array that only has one number.

num = tribo[i - 1] + tribo[i - 2] + tribo[i - 3];

Therefor you get an out of range exception for each of these tribo[...] statements except for the one that happens to be 0 (the first and only element).

You'll also get this error in your final Console.WriteLine statement, since it references tribo[n].

Stanislas
  • 1,893
  • 1
  • 11
  • 26
  • thank you, now it works well with this line in for loop `Array.Resize(ref tribo, i+1); num = tribo[i - 1] + tribo[i - 2] + tribo[i - 3]; tribo[i] = num;` – playsf Oct 16 '22 at 14:08
0

As the error is:

System.IndexOutOfRangeException

It means that your code is trying to access an invalid index in the array. Remember, arrays are 0 index, so if you try to access a block which is equal to its size, you will get an error.

Just replace the last else block with this:

Console.WriteLine(tribo[n-1]);

then it should work.

Abhishek Dutt
  • 1,308
  • 7
  • 14
  • 24
0

This line is your culprit. You're instantiating a new int array with the size of one element:

tribo = new int[] {num};

The second interation of the for loop has to throw System.IndexOutOfRangeException, because you're trying to access indices that don't exist.

Julian
  • 5,290
  • 1
  • 17
  • 40