Home > Blockchain >  Extract from arrays - Not all code paths return a value
Extract from arrays - Not all code paths return a value

Time:11-04

I'm trying to extract even number from an array.

I feel like I'm almost getting it, but I keep getting this error and don't know what to do.

(int[])': not all code paths return a value

I know I need a return value but I want to return nothing, but just the values as per the if statement.

The If statement is basically:

if (arr[i] % 2 == 0)

so this is the condition for me to get even values in the array, which is what I want.

Then now I need to return something apparently, based on my research. But I just want to return the even values as gotten from the if statement. My current code:

static int[] ExtractEvenNumber(int[] arr)
{
    
    for (int i = 0; i < arr.Length; i  )
    {
        if (arr[i] % 2 == 0)
        {
            Console.Write(arr[i]   " ");
        }               
        
    }
    
    // so I need a return value here? what do I return? 
    //I dont want to return arr; cause it'll just repeat the arr. 
}

EDIT:

This is what I want to get (the bold value):

[ 4 1 2 5 6 1 3 ] -> [ 4 2 6 ]

The sample array is on the left.

I tried to return int[];

but it is giving me more errors?

enter image description here

Question has been solved. Thank you everyone! :)

CodePudding user response:

It looks like you're supposed to be implementing this using low-level code which doesn't require Linq or even List<T>.

You can do this using just loops and a results array using the following algorithm:

  1. Determine how many numbers there will be in the returned array. You can do this by looping over the input array and counting the number of even numbers. You need to know this because you want to create an array the correct size to hold all the results.
  2. Create a results array of the size that you just calculated.
  3. Loop over the input again and this time add each even number to the results array. You can use a counter (an integer variable) as an index which you increment after adding each even number to the results, so that you know where each new number should go in the results array.
  4. Finally, you can return the results array.

Here's a sample implementation - read the code carefully to make sure that you understand each step.

static int[] ExtractEvenNumber(int[] arr)
{
    // (1) Count how many even numbers there are.

    int n = 0;

    foreach (int number in arr) 
    {
        if (number % 2 == 0)
        {
              n;
        }
    }

    // (2) Create a result array of the correct size.

    int[] result = new int[n];

    // (3) Copy into the array all the even numbers.
    // Here we use i to index where the next even number should go in the results.

    int i = 0;

    foreach (int number in arr)
    {
        if (number % 2 == 0)
        {
            result[i] = number;
              i; // Next even number will go into the next result element.
        }
    }

    // (4) Return the results;

    return result;
}

CodePudding user response:

You said in comments you can't remove static int[] ExtractEvenNumber(int[] arr) because it is the default code in your assignment, so you have to return an int[]. It can be an empty array if nothing has been found. In C# you can't add elements to int[] object because you can't change their length, so I suggest you to use List to temporary store numbers you found, then convert it to int[] before returning result:

    static int[] ExtractEvenNumber(int[] arr)
    {
        List<int> result = new();
        
        for (int i = 0; i < arr.Length; i  )
        {
            if (arr[i] % 2 == 0)
            {
                result.Add(arr[i]);
                Console.Write(arr[i]   " "); // Optional
            }

        }
        
        return result.ToArray();
    }

You can also use foreach to iterate over your array, because you don't need the indices of the number, but only the value. I think this is the most understandable if you are new at coding:

static int[] ExtractEvenNumber(int[] arr)
    {
        List<int> result = new();

        foreach (int number in arr)
        {
            if (number % 2 == 0)
            {
                result.Add(number);
                Console.Write(number   " "); // Optional
            }

        }

        return result.ToArray();
    }

Simpler, you can use .Where:

static int[] ExtractEvenNumber(int[] arr)
    {
        return arr.Where(number => number % 2 == 0).ToArray();
    }
  • Related