Jon Smit

Jon Smit

  • NA
  • 6
  • 1.8k

assigning new array to the array passed by reference

Dec 16 2018 8:33 AM
write a OddNumbers method,The method should take an integer array passed using the ref keyword.
After the method is executed the array that was passed in should retain contain only the even numbers in the same order, and should have no space free
 
using System;

namespace HelloWorld
{
class Program
{
static string GetArrayAsString(int[] array)
{
if(array.Length == 0) return "";

string str = "";
int i = 0;
for(; i < array.Length - 1; i++)
{
str += array[i] + ", ";
}
str += array[i];

return str;
}


static void Main(string[] args)
{
int[] array = { 3, 8, 12, 31, 5, 78, 90 };
removeOddNumbers(ref array);

Console.WriteLine(GetArrayAsString(array));
}
}

Answers (1)