Home > other >  How could I iterate through a 2D array, double each number, then print the updated array?
How could I iterate through a 2D array, double each number, then print the updated array?

Time:03-15

Like the question says, I am trying to take the array listed below and go through each number, double the number, and return the new number to the array, and when it's all done, call the method to print the new array. My initial thoughts were that maybe I could go through by row/column and get the number that way, multiply by 2, and then return it, but I'm being told something along the lines of int can't be converted to int[,]. I can not seem to figure out where to go from here.

int[,] array_2d = {{1, 2, 3},{ 4, 5, 6},{7, 8, 9}};

public static int[ , ] DoubleArray(int[ , ] array_2d){
for (int row = 0; row < 3; row  ){
  for (int col = 0; col < 3; col   ){
    int val = array_2d[row,col];
    return val * 2;
  }
 }
}

Also to clarify, The array is in the main method while this is a separate method needing to be called in the main with the array as the parameter.

Thanks in advance!

CodePudding user response:

The reason for your int-related error is that you state your return type as 'int[,]' and then in your return statement, you are attempting to return a value of type 'int'. In order to fix this, you need to return a 2d int array as specified by your return type. I would also recommend fetching the length of the array dynamically instead of hardcoding in the three. The below code should do the job.

public static int[,] DoubleArray(int[,] array_2d)
{
    for (int row = 0; row < array_2d.GetLength(0); row  )
    {
        for (int col = 0; col < array_2d.GetLength(1); col  )
            {
                int val = array_2d[row, col];
                array_2d[row, col] = val * 2;
            }
        }
    }
    return array_2d;
}

EDIT: As @hijinxbassist pointed out, this will modify the original array, if instead you would like to keep the value of the original, you can use the slightly altered function below.

public static int[,] DoubleArray(int[,] array_2d)
{
    int[,] newArray = (int[,])array_2d.Clone();

    for (int row = 0; row < newArray.GetLength(0); row  )
    {
        for (int col = 0; col < newArray.GetLength(1); col  )
            {
                int val = newArray[row, col];
                newArray[row, col] = val * 2;
            }
        }
    }
    return newArray;
}

CodePudding user response:

i think you can do something like that:

public static int[,] DoubleArray(int[,] array_2d)
{
    for (int row = 0; row < 3; row  )
    {
        for (int col = 0; col < 3; col  )
        {
            array_2d[row, col] = array_2d[row, col] * 2;
        }
    }

    return array_2d;
}

public static void PrintArray(int[,] array_2d)
{
    for (int row = 0; row < 3; row  )
    {
        for (int col = 0; col < 3; col  )
        {
            Console.Write($"{array_2d[row, col]} ");
        }
    }
}

public static void Main(string[] args)
{
    int[,] array_2d = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

    var doubledArray = DoubleArray(array_2d);

    PrintArray(doubledArray);
}
  • Related