Home > Enterprise >  Using foreach to get the even numbers out of array c#
Using foreach to get the even numbers out of array c#

Time:03-28

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.

CodePudding user response:

you can also try using Array.FindAll() built-in method to achieve this , instead of looping construct. Please try below code.

var myArr = new int[] { 4, 9, 2, 5, 1, 4, 8, 3, 3 };
var myOtherArr = Array.FindAll(myArr, v => v%2 == 0);

Thank you.

CodePudding user response:

I would try something like this

    int[] myArr = { 4, 9, 2, 5, 1, 4, 8, 3, 3 };
    
    var i=0;
    foreach (var item in myArr)
        if (item % 2 == 0) i  ;
    
    var myOtherArr= new int[i];
    i=0;
    foreach (var item in myArr)
        if (item % 2 == 0) { myOtherArr[i] = item; i  ; }

CodePudding user response:

I did it this way because you don't have to figure out the length of the array.

int[] myArr = { 4, 9, 2, 5, 1, 4, 8, 3, 3 };
int[] myOtherArr;

List<int> cache = new List<int>();

//goes through each number in this array and stores the ones for that round in "num".
//If the number is "% 2 == 0" then it will be added to a list.
foreach (int num in myArr)
{
    if (num % 2 == 0)
    {
        cache.Add(num);
    }
}


//the list is converted and stored in your array
myOtherArr = cache.ToArray();

//all numbers are displayed
foreach (int num in myOtherArr)
{
    Console.WriteLine(num);
}
     

but you can also write it like this (I like to do this, for example).

int[] myArr = { 4, 9, 2, 5, 1, 4, 8, 3, 3 };
int[] myOtherArr;

List<int> cache = new List<int>();

//goes through each number in this array and stores the ones for that round in "num".
//If the number is "% 2 == 0" then it will be added to a list.
foreach (int num in myArr)
    if (num % 2 == 0)
        cache.Add(num);

//the list is converted and stored in your array
myOtherArr = cache.ToArray();

//all numbers are displayed
foreach (int num in myOtherArr)
    Console.WriteLine(num);

CodePudding user response:

Well, having count even numbers (num):

int[] myArr = new int[] { 4, 9, 2, 5, 1, 4, 8, 3, 3 };

for (int i = 0; i < myArr.Length; i  )
{
    if (myArr[i] % 2 == 0)
    {
        num  ;
    }
}

you should create myOtherArr array (note, that you should create myOtherArr after num is computed):

int[] myOtherArr = new int[num];

then you can loop over myArr while assigning values to myOtherArr:

// Index within myOtherArr 
// Note, that we have two indexes:
//   i = 0 .. myArr.Length
//   otherIndex = 0 .. num 
int otherIndex = 0; 

for (int i = 0; i < myArr.Length; i  )
  if (myArr[i] % 2 == 0)
    myOtherArr[otherIndex  ] = myArr[i];

In real life we, usually, query with a help of Linq:

int[] myOtherArr = myArr
  .Where(item => item % 2 == 0)
  .ToArray();

CodePudding user response:

The problem is here:

int[] myOtherArr = new int[num]; // you initialize the array to 0 length.

Try this:

int[] myArr = { 4, 9, 2, 5, 1, 4, 8, 3, 3 };

int num = 0;



for (int i = 0; i < myArr.Length; i  )
{
    if (myArr[i] % 2 == 0)
    {
        num  ;
    
    }
}

int[] myOtherArr = new int[num];
int j = 0;
for (int i = 0; i < myArr.Length; i  )
{
     if (myArr[i] % 2 == 0)
     {
          myOtherArr[j] = myArr[I];
          j  ;
     }
}
  •  Tags:  
  • c#
  • Related