1

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.

4
  • 2
    Are you trying to add one of them into the other, or create a third array? Do you even need to put the result in another array if you're just printing it out? You clearly know how to loop across each element of an array - think about what you need to do for the addition. Also note that currently you're not using b at all... I suspect you meant to in the second loop. Commented Mar 29, 2013 at 10:22
  • 1
    what do you mean by "adding"? sum of the values on the same position or concatenation of arrays? Commented Mar 29, 2013 at 10:24
  • I'm guessing he means matrix addition. Commented Mar 29, 2013 at 10:34
  • In your second loop (after int[,] b = new int[row, col];) you are setting the values to a. It seems it must be b Commented Mar 29, 2013 at 10:34

4 Answers 4

4
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();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

OK. don't forget to change the second a[i, j] = Convert.ToInt32(Console.ReadLine()); to b[i, j] = Convert.ToInt32(Console.ReadLine());
0
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();

Comments

0
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");
    }   
}

}

Comments

0

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();
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.