Ridvan Tahir
Ridvan Tahir

Reputation: 31

Cannot implicitly convert type 'int' to 'int[]' on a 2 dimentional array

In the below code I get

Cannot implicitly convert type 'int' to 'int[]'

The line causing the error is

return firstNum - secondNum;

This is my code.

namespace Determinant
{
    class Program
    {
        static int[] BerekenDeterminant (int[,] determinant)
        {
            int firstNum = determinant[0,0] * determinant[1, 1];
            int secondNum = determinant[0,1] * determinant[1, 0];
            return firstNum - secondNum;
        }
        static void Main(string[] args)
        {
            int[,] aMatrix =
            {
                {2,4},
                {3,5}
            };
            Console.WriteLine($"Determinant van matrix is {BerekenDeterminant(aMatrix)}");
            Console.ReadLine();
        }
    }
}

Upvotes: 1

Views: 186

Answers (2)

m-rmgx
m-rmgx

Reputation: 16

You need to create a new array:

static int[] BerekenDeterminant(int[,] determinant)
    {        
        int firstNum = determinant[0, 0] * determinant[1, 1];
        int secondNum = determinant[0, 1] * determinant[1, 0];
        int[] inttab = new int[] {firstNum, secondNum};
        // or
        // int[] inttab = new int[] { firstNum - secondNum };
        return inttab;
    }

Upvotes: 0

ian_stagib
ian_stagib

Reputation: 158

return new[] {firstNum - secondNum};

or just change return type to int only. Up to how you want to work with that method

Upvotes: 1

Related Questions