简体   繁体   中英

C# Addition of two 2-Dimensional Arrays

Okay, So as the title says I need help finding a simple way of adding two arrays together. This is my code so far:

static void Main()
{
    Console.Write("Enter Rows: ");
    int row = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter Columns: ");
    int col = Convert.ToInt32(Console.ReadLine());
    int[,] a = new int[row, col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            Console.Write("Enter Matrix({0},{1}): ", i, j);
            a[i, j] = Convert.ToInt32(Console.ReadLine());
        }
    }
    int[,] b = new int[row, col];
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            Console.Write("Enter Matrix({0},{1}): ", i, j);
            a[i, j] = Convert.ToInt32(Console.ReadLine());
        }
    }

So, how would I add these two array together, and print out the result. Thanks.

static void Main()
{
    // Your code

    int[,] result = new int[row, col];

    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < col; j++)
        {
            result [i, j] = a[i,j] + b[i,j];

            Console.Write(result[i, j] + " ");
        }

        Console.WriteLine();
    }
}
int m, n, c, d;
int[,] first = new int[10, 10];
int[,] second = new int[10, 10];
int[,] sum = new int[10, 10];

Console.WriteLine("Enter the number of rows and columns of matrix");
m = Convert.ToInt16(Console.ReadLine());
n = Convert.ToInt16(Console.ReadLine());
Console.WriteLine("\nEnter the elements of first matrix\n");

for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
    {
        Console.WriteLine("Enter Element [" + c + " , " + d + "]");
        first[c, d] = Convert.ToInt16(Console.ReadLine());
    }

Console.WriteLine("Enter the elements of second matrix");
for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
    {
        Console.WriteLine("Enter Element [" + c + " , " + d + "]");
        second[c, d] = Convert.ToInt16(Console.ReadLine());
    }

for (c = 0; c < m; c++)
    for (d = 0; d < n; d++)
        sum[c, d] = first[c, d] + second[c, d];

Console.WriteLine("Sum of entered matrices:-");
for (c = 0; c < m; c++)
{
    for (d = 0; d < n; d++)
        Console.Write(" " + sum[c, d]);
    Console.WriteLine();
}
Console.ReadKey();
using System;
class matrixAdd
{
public static void Main()
{
    int [,] mat1 = new int[3,3];
    int [,] mat2 = new int[3,3];
    int [,] addition = new int[3,3];
    int i, j;
    Console.WriteLine("Enter the elements of the matrix1: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            mat1[i,j] = Convert.ToInt32(Console.ReadLine());
        }
    }
    Console.WriteLine("Entered elements of the matrix1: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            Console.Write("{0} ", mat1[i,j]);
        }
        Console.Write("\n");
    }

    Console.WriteLine("Enter the elements of the matrix2: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            mat2[i,j] = Convert.ToInt32(Console.ReadLine());
        }
    }
    Console.WriteLine("Entered elements of the matrix2: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            Console.Write("{0} ", mat2[i,j]);
        }
        Console.Write("\n");
    }

    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            addition[i,j] = mat1[i,j] + mat2[i,j];
        }
    }

    Console.WriteLine("Addition of the matrix1 and matrix2: ");
    for(i=0; i<3; i++)
    {
        for(j=0; j<3; j++)
        {
            Console.Write("{0} ", addition[i,j]);
        }
        Console.Write("\n");
    }   
}

}

After summing both arrays you need to go for a new for loop to print the new array

 // your code

 int[,] result = new int[row, col];

        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                result[i, j] = a[i, j] + b[i, j];
            }
        }
        for (int i = 0; i < row; i++)
        {
            for (int j = 0; j < col; j++)
            {
                Console.Write(result[i, j] + " ");
            }
            Console.WriteLine();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM