lucians
lucians

Reputation: 2269

Check if item position in array is even or odd (C#)

What I want to do is, given an array, select the items which have even/odd index insite the list.

I'll explain better: if I have an array like this [1,4,6,2,8], I want to add to a list (evenList) all the items which have even position (position zero, two, four, etc.) in the array (in this case 1,6,8). Same thing for the odd items.

I have the developed the following code but I am stuck.

class CheckItem
{
    static readonly string myNumber = "5784230137691";

    static int[] firstTwelveList = new int[12];
    static int[] arrayEvenPosition = new int[(myNumber.Length / 2)];
    static int[] arrayOddPosition = new int[(myNumber.Length / 2)];

    static readonly int idx = 0;

    public static void Position()
    {
        firstTwelveList = myNumber.Substring(0, 12).Select(c => c - '0').ToArray();

        foreach (var even in firstTwelveList)
        {
            if(Array.IndexOf(firstTwelveList, idx) % 2 == 0) //never enter here...
            {
                Array.Copy(firstTwelveList, arrayEvenPosition, (myNumber.Length / 2));
            }
        }
        Console.ReadLine();
    }
}

What I expect is that the arrayEvenPosition will contain 5,8,2,0,3,6,1 and arrayOddPosition 7,4,3,1,7,9

Upvotes: 1

Views: 4097

Answers (3)

Alexey  Usharovski
Alexey Usharovski

Reputation: 1442

I think for your task the base form of a for loop is better then foreach.

int j=0;
int k=0;
for (int i=0; i<firstTwelveList.Length; i++) {
  if (i % 2 == 0) {
     arrayEvenPosition[j++] = firstTwelveList[i];
  } else {
     arrayOddPosition[k++] = firstTwelveList[i];
  }
}

Notice that my code is not a full solution but only an idea of what you should do. Good luck!

Upvotes: 3

user3785553
user3785553

Reputation: 121

Other answers are right that there are easier ways to split the array to two arrays based on the index (odd /even) . However i have noticed there are two issues with your code.

if(Array.IndexOf(firstTwelveList, idx) % 2 == 0) //never enter here..

the two parameters to IndexOf are the array and the item you want to find the index of an element, idx is not the correct parameter. it should be the even variable from the foreach loop

Secondly when copying the correct value to the other array, you are copying to the new array the first 6 characters of the 1st array.

Quoting the reference documents of Array.Copy(Array, Array, Int32) , it just copies the required range of elements from the first array to the 2nd array.

So you need to modify the code to copy the element that satisfies the condition of even/odd. for that you need an variable to track the current index of the new array

The whole class can be modified this way

class CheckItem
{
    static readonly string myNumber = "5784230137691";

    static int[] firstTwelveList = new int[12];
    static int[] arrayEvenPosition = new int[(myNumber.Length / 2)];
    static int[] arrayOddPosition = new int[(myNumber.Length / 2)];

    static int idx = 0;
    static int evenIdx = 0; // track current index of new array

    public static void Position()
    {
        firstTwelveList = myNumber.Substring(0, 12).Select(c => c - '0').ToArray();

        foreach (var even in firstTwelveList)
        {
            if (Array.IndexOf(firstTwelveList, even) % 2 == 0) // replace idx with even...
            {
                Array.Copy(firstTwelveList, idx, arrayEvenPosition, evenIdx, 1); // copy the element from the current index of first array to new array
                evenIdx++;
            }
            idx++;
        }
        Console.ReadLine();
    }
}

Upvotes: 0

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186668

Try Linq:

 firstTwelveList = myNumber
   .Take(12)
   .Select(c => c - '0')
   .ToArray();

 arrayEvenPosition = firstTwelveList
   .Where((item, index) => index % 2 == 0)
   .ToArray();

 arrayOddPosition = firstTwelveList
   .Where((item, index) => index % 2 != 0)
   .ToArray();

Upvotes: 4

Related Questions