Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C# program: How would I write the if statement to check if each matrices can be

ID: 3745056 • Letter: C

Question

C# program: How would I write the if statement to check if each matrices can be added together. if it cant, then i would write the else as "Cannont add" ?

public static void MatAdd()

{

StreamWriter streamWriter = new StreamWriter("matrices.txt");

int[,] mat1 = new int[6, 5];

int rowCount = mat1.GetLength(0);

int colCount = mat1.GetLength(1);

int currCounter = 1;

//Matrix 1

streamWriter.Write("Matrix A: ");

for (int row = 0; row < rowCount; row++)

{

for (int col = 0; col < colCount; col++)

{

mat1[row, col] = currCounter++;

streamWriter.Write("{0} ", mat1[row, col]);

}

streamWriter.WriteLine(" ");

}

//Matrix 2

streamWriter.Write("Matrix B: ");

int[,] mat6 = new int[11, 14];

int currCounter2 = -7;

for (int row = 0; row < rowCount; row++)

{

for (int col = 0; col < colCount; col++)

{

mat6[row, col] = currCounter2++;

currCounter2 += 1;

streamWriter.Write("{0} ", mat6[row, col]);

}

streamWriter.WriteLine(" ");

}

//Matrix Sum

streamWriter.Write("Matrix C: (1 + 6) ");

int[,] matSum = new int[8, 7];

for (int row = 0; row < rowCount; row++)

{

for (int col = 0; col < colCount; col++)

{

if (mat1[row, col] == mat6[row, col])

{

matSum[row, col] = mat1[row, col] + mat6[row, col];

}

else { streamWriter.WriteLine("Cannot add these two matrices."); }

}

}

for (int row = 0; row < rowCount; row++)

{

for (int col = 0; col < colCount; col++)

{

streamWriter.Write("{0} ", matSum[row, col]);

}

streamWriter.WriteLine(" ");

}

streamWriter.Close();

}

Explanation / Answer


Hello ,

If you want to add two matrices it must be of same dimension this is the rule.
In your program you have created 1st matrix of 6*5 , 2nd 11*14 and the resultant matrix as 8*7
This is totaly wrong.

I changed your code so program can check wether two matrices have same dimension or not
also i changed the array size for matrices , I made all arrays of same size so we can do addition of matrices.

**note= If you want to check wether each matrices can be added together just change the any matrices array size , I have checked its row and column size in program.


Please find the code below.

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace matricesadd
{
class Program
{
static void Main(string[] args)
{
Program.MatAdd();
}

public static void MatAdd()
{
try
{

StreamWriter streamWriter = new StreamWriter("matrices.txt");
int[,] mat1 = new int[6, 5];
int mat1rowCount = mat1.GetLength(0);
int mat1colCount = mat1.GetLength(1);
int currCounter = 1;

//Matrix 1
streamWriter.Write("Matrix A: ");
streamWriter.WriteLine(" ");
for (int row = 0; row < mat1rowCount; row++)
{
for (int col = 0; col < mat1colCount; col++)
{
mat1[row, col] = currCounter++;
streamWriter.Write("{0} ", mat1[row, col]);
}
streamWriter.WriteLine(" ");
}

//Matrix 2
streamWriter.Write("Matrix B: ");
streamWriter.WriteLine(" ");
int[,] mat6 = new int[6, 5];
int mat2rowCount = mat6.GetLength(0);
int mat2colCount = mat6.GetLength(1);

int currCounter2 = -7;
for (int row = 0; row < mat2rowCount; row++)
{
for (int col = 0; col < mat2colCount; col++)
{
mat6[row, col] = currCounter2++;
currCounter2 += 1;
streamWriter.Write("{0} ", mat6[row, col]);
}
streamWriter.WriteLine(" ");
}

//Matrix Sum
if (mat1rowCount == mat2rowCount && mat1colCount == mat2colCount) // checked wether both matrices dimension is same or not
{
streamWriter.Write("Matrix C: (1 + 6)");
streamWriter.WriteLine(" ");
int[,] matSum = new int[6, 5];

for (int row = 0; row < mat1rowCount; row++)
{
for (int col = 0; col < mat1colCount; col++)
{
matSum[row, col] = mat1[row, col] + mat6[row, col];
streamWriter.Write("{0} ", matSum[row, col]); // written addition to file
}
streamWriter.WriteLine(" "); // to go to next row
}
}
else
{
streamWriter.WriteLine(" ");
streamWriter.WriteLine("you entered matrix1 as 6*5 length ");
streamWriter.WriteLine("you entered matrix1 as 11*14 length ");
streamWriter.WriteLine("Please enter matrices of same dimension for ex 2*2 or 3*3 etc.");
}
streamWriter.Close();
}
catch (Exception ex)
{
Console.WriteLine("Exception occured" + ex.Message.ToString());
}
}
}
}


If you LIKE my answer please do thumb up for the same.

Thank you.