-6

I need help for the following code. I have an array, which skip every 3 elements in the array. The code works with indexes of the array.

The parts with ----- have to fill with some code.

Can anyone help me? I have the following code:

I tried this.

static int[] SkipEvery(int[] numbers,int skip)
{
    int[] skipped = new int[numbers.Length];

    for (int i = 0; i < numbers.Length; i++)
    {
        if ((numbers[i]) % (skip + 1) == 0)
        {
            skipped[i] = numbers[i];
        }
        else
        {
            skipped[i] = numbers[i];
        }
    }
    return skipped;
}

public static void Main(string[] args)
{
    int[] numbers = new int[] { 7, 2, 24, 69, 101, 42, 84, 100, 72 };
    numbers = SkipEvery(numbers, 3);
}

The output must be

[0]7   [1]2   [2]24   [3]-1    [4]101   [5]42   [6]84   [7]-1   [8]72

But with this code the output is

[0]7   [1]2   [2]24   [3]69    [4]101   [5]42   [6]84   [7]100   [8]72
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Wouter
  • 23
  • 1

4 Answers4

1

You can try Linq in order to filter out the items and create a new array:

  int[] numbers = new int[] { 7, 2, 24, 69, 101, 42, 84, 100, 72 };

  int[] skipped = numbers
    .Select((item, index) => (index + 1) % 4 != 0 ? item : -1)
    .ToArray();

If you want to modify existing array, a simple loop will do

  for (int i = 3; i < numbers.Length; i += 4)
    numbers[i] = -1; 

Finally, let's have a look at the outcome (skipped):

  string report = string.Join(" ", skipped
    .Select((item, index) => $"[{index}]{item}"));

  Console.Write(report);

Outcome:

  [0]7 [1]2 [2]24 [3]-1 [4]101 [5]42 [6]84 [7]-1 [8]72

Edit: Your current code amended:

static int[] SkipEvery(int[] numbers, int skip) {
  int[] skipped = new int[numbers.Length];

  for (int i = 0; i < numbers.Length; i++) {
    if ((i + 1) % (skip + 1) == 0) { // check indexes - i, not values - numbers[i]
      skipped[i] = -1;               // well, you have to skip!   
    }
    else {
      skipped[i] = numbers[i];
    }
  }
  return skipped;
}
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

Try something like this:

if(i>=3 && i%3==0){continue;}

Modulus operator

dino
  • 58
  • 6
0

How about an extension method:

public static class Extensions
{
    public static T[] ReplaceEvery<T>(this T[] values, int every, T replacement)
    {
        return values.Select( (x,i) => (((i+1)%every) == 0) ? replacement : x).ToArray();
    }
}

Usage:

int[] numbers = new int[] { 7, 2, 24, 69, 101, 42, 84, 100, 72 };
var result = numbers.ReplaceEvery(3,-1);
Console.WriteLine(String.Join(",",result));
//output: 7,2,-1,69,101,-1,84,100,-1
Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

You can skip on every element and use yield return to yield the value back to the caller.

static IEnumerable<int> SkipEvery(int[] numbers, int skip)
{
    for (int i = 1; i <= numbers.Length; i++)
    {
        if ((i % (skip + 1)) != 0)
            yield return numbers[i - 1];
        else
            yield return -1;
    }
}
static void Main(string[] args)
{
    int[] numbers = new int[] { 7, 2, 24, 69, 101, 42, 84, 100, 72 };
    string result = string.Join("\t", SkipEvery(numbers, 3).Select((x, i) => $"[{i}]{x}"));
    Console.WriteLine(result);
}

Outputting:

[0]7    [1]2    [2]24   [3]-1   [4]101  [5]42   [6]84   [7]-1   [8]72
Kunal Mukherjee
  • 5,775
  • 3
  • 25
  • 53