I have this homework assignment where I have array called myArr with values 4, 9, 2, 5, 1, 4, 8, 3, 3 and I have to extract even numbers out of there and place them in myOtherArray and then printing them with foreach. I tried doing the code but I kinda got stuck since I know it won't work.
Here is my attempt:
int[] myArr = { 4, 9, 2, 5, 1, 4, 8, 3, 3 };
int num = 0;
int[] myOtherArr = new int[num];
for (int i = 0; i < myArr.Length; i++)
{
if (myArr[i] % 2 == 0)
{
num++;
}
}
Num is the counter to count how many even numbers are there, but right now I don't know how to save them into myOtherArray because I get the error that Index was outside the bounds of the array.
for (int i = 0; i < myArr.Length; i++)
{
if (myArr[i] % 2 == 0)
{
myArr[i] = myOtherArr[i];
}
}
Right now I don't know how to do this. Please help I want to be ready for my exam comming soon.
Thank you all in forward for your answers.