Yushell
Yushell

Reputation: 745

Sum, Subtract and Multiply arrays

I've got the following arrays:

int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };

What I'd like to know how to do is:

  1. Create a new array with the sum of myArray1 and myArray2
  2. Create a new array with the subtraction of myArray1 and myArray2
  3. Create a new array with the multiplication of myArray1 and myArray2

Result of sum would be:

int[,] myArray3 = new int[2, 3] { { 7, 6, 0 }, { -4, 4, 0 } };

Result of subtraction would be:

int[,] myArray3 = new int[2, 3] { { 5, 2, 6 }, { 12, 8, 16 } };

Result of multiplication would be:

int[,] myArray3 = new int[2, 3] { { 6, 8, 9 }, { 32, 12, 64 } };

Can this be done similar to printing out the arrays, with for loops? I tried looking for examples but found none that I could use for my specific problem.

Upvotes: 5

Views: 32695

Answers (5)

Alex Martin
Alex Martin

Reputation: 196

If you want to do array manipulation faster use the C# Parallel.For loop from System.Threading.Tasks:

For simple arithmetic parallelizing the outer loop is much faster than not on a modern PC processor. For more complex operations, or for small array sizes, the parallel version can be slower for various reasons.

Thus, use a stopwatch to time your matrix operations, and use the fastest solution. Parallelization makes doing array / image processing in C# much faster if implemented right.

Beware of overflowing your datatypes after arithmetic operations and also sharing variables between multiple threads (see System.Threading.Interlocked for help with that)...

Subtraction below. Similar for addition and multiplication:

Parallel.For(0, array.GetLength(1), y=>
{
    for (int x = 0; x < array.GetLength(0); x++)
        {
            difference[x,y] = minuend[x,y] - subtrahend[x,y];
        }
    }
});

Upvotes: 2

Preston
Preston

Reputation: 1346

Yes this would be done exactly like printing out the arrays with for loops

c# has foreach loops which would be even easier to use

Note: I get the idea this is for homework so I'm not going to give a 100% conclusive end all be all answer.

 int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
 int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };
    
          foreach (int[] a1 in myArray1) 
          {
             foreach(int i in a1)
             {
                //operation here
                //you get the idea
             }        
          }

Upvotes: 1

Sayse
Sayse

Reputation: 43320

int[,] a3 = new int[2,3];

for(int i = 0; i < myArray1.GetLength(0); i++)
{
for(int j = 0; j < myArray1.GetLength(1); j++)
{
a3[i,j] = myArray1[i,j] + myArray2[i,j];
a3[i,j] = myArray1[i,j] - myArray2[i,j];
a3[i,j] = myArray1[i,j] * myArray2[i,j];
}
}

need to store a3 before doing a new calculation obviously

Upvotes: 5

Shaharyar
Shaharyar

Reputation: 12449

For Sum:

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = myArray1[i, j] + myArray2[i, j];
            }                
        }

For Subtraction:

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = myArray2[i, j] - myArray1[i, j];
            }                
        }

For Multiplication:

for (int i = 0; i < 2; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                myArray3[i, j] = A[i, j] * B[i, j];
            }
        }

Upvotes: 3

d.moncada
d.moncada

Reputation: 17402

If you want to use a for loop, you can iterate through the rows/columns of the multi-d array as follows:

for (int i = 0; i < myArray1.GetLength(0); i++)
{
    for (int j = 0; j < myArray1.GetLength(1); j++)
    {
        // Here, you can access the array data by index, using i and j. 
        // Ex, myArray1[i, j] will give you the value of 1 in the first iteration.
    }
}

Note: When you pass a value into the Array's GetLength method, it represents the dimension of the array. See http://msdn.microsoft.com/en-us/library/system.array.getlength.aspx

Upvotes: 0

Related Questions