简体   繁体   中英

system index out of range c#

I'm trying to make a program which will figure out which half of an array added together is greater, however keep getting this error and I can not seem to figure out why

"An unhandled exception of type 'System.IndexOutOfRangeException' occurred in Average Mark.exe" on line 31 when trying to work from the back of the array.

I am very new to c# and thought what I was doing should have worked? Many thanks!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace most_frequent_int
{
    class Program
    {
        static void Main(string[] args)
        {
            halfCheck(new int[] {1,1,2,3});
        }

        static void halfCheck(int[] checkArray)
        {
            int fHalf = 0; 
            int sHalf=0;

            //even method
            if (checkArray.Length % 2 == 0) 
            {               
                for (int i= 0;i<checkArray.Length/2;i++)//check first half even
                {
                    fHalf = fHalf + checkArray[i];
                }

                for (int i=checkArray.Length;i>checkArray.Length/2;i--)
                {
                    sHalf = sHalf + checkArray[i];
                    Console.WriteLine(sHalf);
                }

                if (fHalf > sHalf)
                {
                    Console.WriteLine("The first half is bigger");
                }
                else
                {
                    Console.WriteLine("The second half is bigger");
                }
                Console.ReadLine();

            }

            //odd method
            else
            {
                Console.WriteLine("odd");
            }   
        }
    }
}

The error is in this code:

for (int i=checkArray.Length;i>checkArray.Length/2;i--)
{
    sHalf = sHalf + checkArray[i];
    Console.WriteLine(sHalf);
}

The Length property is 1-based, as it counts the elements in the array, but indexers are zero-based. When you try to access checkArray[i] , the i value is past the end of the array. Consider starting with checkArray.Length - 1 .

On this line:

for (int i = checkArray.Length; i > checkArray.Length / 2; i--)

You are assigning i to the array's length on the first iteration. You cannot index an array by its length, at it is one more than the last valid position.

Try this:

for (int i = checkArray.Length - 1; i > checkArray.Length / 2; i--)

The problem is in the following line:

 for (int i=checkArray.Length;i>checkArray.Length/2;i--)

The first element you access is checkArray[checkArray.Length] which is out of the array. The allowed indexes are to checkArray.Length -1 .

Try with:

for (int i=checkArray.Length - 1; i > checkArray.Length/2; i-- )

This line:

for (int i=checkArray.Length;i>checkArray.Length/2;i--)

Will throw an IndexOutofRangeException every time. When using arrays, the .length property returns the number of elements in the array, which is NOT the same as the highest index.

For example, if checkArray has 5 elements, the indexes will be 0, 1, 2, 3, and 4. checkArray.Length will return the number of elements, which is 5.

Since your for loop begins with i=checkArray.length , the first index it tries will ALWAYS be out of bounds. Try i=checkArray.Length-1 instead.

You just need to change what i starts in the second loop. i = checkArray.Length - 1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace most_frequent_int
{
class Program
{
    static void Main(string[] args)
    {
     halfCheck(new int[] {1,1,2,3});
    }

    static void halfCheck(int[] checkArray)
    {
        int fHalf = 0; 
        int sHalf=0;

        //even method
        if (checkArray.Length % 2 == 0) 
        {               
            for (int i= 0;i<checkArray.Length/2;i++)//check first half even
            {
                fHalf = fHalf + checkArray[i];
            }

        for (int i=checkArray.Length - 1;i>checkArray.Length/2;i--)
            {
                sHalf = sHalf + checkArray[i];
                Console.WriteLine(sHalf);
            }

            if (fHalf > sHalf)
            {
                Console.WriteLine("The first half is bigger");
            }
            else
            {
                Console.WriteLine("The second half is bigger");
            }
            Console.ReadLine();

        }

        //odd method
        else
        {
            Console.WriteLine("odd");
        }

    }

}
}

The array is indexed starting at 0 but length starts at 1. So your array length is 4 and but checkArray[4] doesn't exist as the last item in the array is 3. Length -1 should fix it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM