I'm having problems with my code in c#. Our prof didn't really asked for anything fancy or specified anything. He just told as our program should be able to add and subtract a 2x2 to a 5x5 matrix.

Upon opening the program, I created a form with a combo box so the user will choose what matrix size he/she wants (2x2, 3x3, etc.). Then it will open up a form where the user will input the elements per row separating the elements with a space. The 5x5 works fine, but when I created another form for the 4x4, 3x3, and 2x2.. it doesn't add or subtract. I just basically copy pasted the code I made for the 5x5 into another form, changed the Form3 to Form4 (etc) and trimmed the code accordingly. It runs, doesn't have build errors, it just doesn't show the answer except for the 5x5 one. I'll just paste the code inside the button click instance since that's where the adding, subtracting and output takes place.


part of the code for 5x5

private void button1_Click(object sender, EventArgs e)
        {   int i;
            string t1 = textBox1.Text; string t2 = textBox2.Text;
            string t3 = textBox3.Text; string t4 = textBox4.Text;
            string t5 = textBox5.Text; string t6 = textBox6.Text;
            string t7 = textBox7.Text; string t8 = textBox8.Text;
            string t9 = textBox9.Text; string t10 = textBox10.Text;
            string[] s1 = t1.Split(' '); string[] s2 = t2.Split(' ');
            string[] s3 = t3.Split(' '); string[] s4 = t4.Split(' ');
            string[] s5 = t5.Split(' '); string[] s6 = t6.Split(' ');
            string[] s7 = t7.Split(' '); string[] s8 = t8.Split(' ');
            string[] s9 = t9.Split(' '); string[] s10 = t10.Split(' ');
            int[] a1 = new int[10]; int[] a2 = new int[10];
            int[] a3 = new int[10]; int[] a4 = new int[10];
            int[] a5 = new int[10]; int[] a6 = new int[10];
            int[] a7 = new int[10]; int[] a8 = new int[10];
            int[] a9 = new int[10]; int[] a10 = new int[10];

            int[] i1 = new int[10];
            for (i = 0; i < s1.Length; i++)
            { i1[i] = Convert.ToInt32(s1[i]); }

            int[] i2 = new int[10];
            for (i = 0; i < s2.Length; i++)
            {

                { i2[i] = Convert.ToInt32(s2[i]); }

               }

            int[] i3 = new int[10];
            for (i = 0; i < s3.Length; i++)
            { i3[i] = Convert.ToInt32(s3[i]); }

            int[] i4 = new int[10];
            for (i = 0; i < s4.Length; i++)
            { i4[i] = Convert.ToInt32(s4[i]); }

            int[] i5 = new int[10];
            for (i = 0; i < s5.Length; i++)
            { i5[i] = Convert.ToInt32(s5[i]); }

            int[] i6 = new int[10];
            for (i = 0; i < s6.Length; i++)
            { i6[i] = Convert.ToInt32(s6[i]); }

            int[] i7 = new int[10];
            for (i = 0; i < s7.Length; i++)
            { i7[i] = Convert.ToInt32(s7[i]); }

            int[] i8 = new int[10];
            for (i = 0; i < s8.Length; i++)
            { i8[i] = Convert.ToInt32(s8[i]); }

            int[] i9 = new int[10];
            for (i = 0; i < s9.Length; i++)
            { i9[i] = Convert.ToInt32(s9[i]); }

            int[] i10 = new int[10];
            for (i = 0; i < s10.Length; i++)
            { i10[i] = Convert.ToInt32(s10[i]); }

            if (radioButton1.Checked)
            {
                for (i = 0; i < s1.Length; i++)
                {
                a1[i] = i1[i] + i6[i];
                s1[i] = Convert.ToString(a1[i]);
                }
                string result1 = ConvertStringArrayToString(s1);   
                label5.Text = result1; 

                for (i = 0; i < s2.Length; i++)
                { a2[i] = i2[i] + i7[i];
                s2[i] = Convert.ToString(a2[i]);
                }
                string result2 = ConvertStringArrayToString(s2);
                label6.Text = result2; 

                for (i = 0; i < s3.Length; i++)
                { a3[i] = i3[i] + i8[i];
                s3[i] = Convert.ToString(a3[i]);
                }
                string result3 = ConvertStringArrayToString(s3);
                label7.Text = result3; 

                for (i = 0; i < s4.Length; i++)
                { a4[i] = i4[i] + i9[i];
                s4[i] = Convert.ToString(a4[i]);
                }
                string result4 = ConvertStringArrayToString(s4);
                label8.Text = result4; 

                for (i = 0; i < s5.Length; i++)
                { a5[i] = i5[i] + i10[i];
                s5[i] = Convert.ToString(a5[i]);
                label9.Text = s1[i];
                }
                string result5 = ConvertStringArrayToString(s5);
                label9.Text = result5; 
            }
            if (radioButton2.Checked)
            {
                for (i = 0; i < s1.Length; i++)
                {
                    a1[i] = i1[i] - i6[i];
                    s1[i] = Convert.ToString(a1[i]);
                }
                string result1 = ConvertStringArrayToString(s1);
                label5.Text = result1;

                for (i = 0; i < s2.Length; i++)
                {
                    a2[i] = i2[i] - i7[i];
                    s2[i] = Convert.ToString(a2[i]);
                }
                string result2 = ConvertStringArrayToString(s2);
                label6.Text = result2;

                for (i = 0; i < s3.Length; i++)
                {
                    a3[i] = i3[i] - i8[i];
                    s3[i] = Convert.ToString(a3[i]);
                }
                string result3 = ConvertStringArrayToString(s3);
                label7.Text = result3;

                for (i = 0; i < s4.Length; i++)
                {
                    a4[i] = i4[i] - i9[i];
                    s4[i] = Convert.ToString(a4[i]);
                }
                string result4 = ConvertStringArrayToString(s4);
                label8.Text = result4;

                for (i = 0; i < s5.Length; i++)
                {
                    a5[i] = i5[i] - i10[i];
                    s5[i] = Convert.ToString(a5[i]);
                    label9.Text = s1[i];
                }
                string result5 = ConvertStringArrayToString(s5);
                label9.Text = result5;
            }
}

part of the code for the 4x4

private void button1_Click(object sender, EventArgs e)
        {   int i;
            string t1 = textBox1.Text; string t2 = textBox2.Text;
            string t3 = textBox3.Text; string t4 = textBox4.Text;
            string t5 = textBox5.Text; string t6 = textBox6.Text;
            string t7 = textBox7.Text; string t8 = textBox8.Text;
            string[] s1 = t1.Split(' '); string[] s2 = t2.Split(' ');
            string[] s3 = t3.Split(' '); string[] s4 = t4.Split(' ');
            string[] s5 = t5.Split(' '); string[] s6 = t6.Split(' ');
            string[] s7 = t7.Split(' '); string[] s8 = t8.Split(' ');
            int[] a1 = new int[10]; int[] a2 = new int[10];
            int[] a3 = new int[10]; int[] a4 = new int[10];
            int[] a5 = new int[10]; int[] a6 = new int[10];
            int[] a7 = new int[10]; int[] a8 = new int[10];

            int[] i1 = new int[10];
            for (i = 0; i < s1.Length; i++)
            { i1[i] = Convert.ToInt32(s1[i]); }

            int[] i2 = new int[10];
            for (i = 0; i < s2.Length; i++)
            {

                { i2[i] = Convert.ToInt32(s2[i]); }

            }

            int[] i3 = new int[10];
            for (i = 0; i < s3.Length; i++)
            { i3[i] = Convert.ToInt32(s3[i]); }

            int[] i4 = new int[10];
            for (i = 0; i < s4.Length; i++)
            { i4[i] = Convert.ToInt32(s4[i]); }

            int[] i5 = new int[10];
            for (i = 0; i < s5.Length; i++)
            { i5[i] = Convert.ToInt32(s5[i]); }

            int[] i6 = new int[10];
            for (i = 0; i < s6.Length; i++)
            { i6[i] = Convert.ToInt32(s6[i]); }

            int[] i7 = new int[10];
            for (i = 0; i < s7.Length; i++)
            { i7[i] = Convert.ToInt32(s7[i]); }

            int[] i8 = new int[10];
            for (i = 0; i < s8.Length; i++)
            { i8[i] = Convert.ToInt32(s8[i]); }


            if (radioButton1.Checked)
            {
                for (i = 0; i < s1.Length; i++)
                {
                    a1[i] = i1[i] + i5[i];
                    s1[i] = Convert.ToString(a1[i]);
                }
                string result1 = ConvertStringArrayToString(s1);
                label5.Text = result1;

                for (i = 0; i < s2.Length; i++)
                {
                    a2[i] = i2[i] + i6[i];
                    s2[i] = Convert.ToString(a2[i]);
                }
                string result2 = ConvertStringArrayToString(s2);
                label6.Text = result2;

                for (i = 0; i < s3.Length; i++)
                {
                    a3[i] = i3[i] + i7[i];
                    s3[i] = Convert.ToString(a3[i]);
                }
                string result3 = ConvertStringArrayToString(s3);
                label7.Text = result3;

                for (i = 0; i < s4.Length; i++)
                {
                    a4[i] = i4[i] + i8[i];
                    s4[i] = Convert.ToString(a4[i]);
                }
                string result4 = ConvertStringArrayToString(s4);
                label8.Text = result4;

            }
            if (radioButton2.Checked)
            {
                for (i = 0; i < s1.Length; i++)
                {


                    a1[i] = i1[i] - i5[i];
                    s1[i] = Convert.ToString(a1[i]);
                }
                string result1 = ConvertStringArrayToString(s1);
                label5.Text = result1;

                for (i = 0; i < s2.Length; i++)
                {
                    a2[i] = i2[i] - i6[i];
                    s2[i] = Convert.ToString(a2[i]);
                }
                string result2 = ConvertStringArrayToString(s2);
                label6.Text = result2;

                for (i = 0; i < s3.Length; i++)
                {
                    a3[i] = i3[i] - i7[i];
                    s3[i] = Convert.ToString(a3[i]);
                }
                string result3 = ConvertStringArrayToString(s3);
                label7.Text = result3;

                for (i = 0; i < s4.Length; i++)
                {
                    a4[i] = i4[i] - i8[i];
                    s4[i] = Convert.ToString(a4[i]);
                }
                string result4 = ConvertStringArrayToString(s4);
                label8.Text = result4;

            }
}

I've checked and rechecked if I trimmed part of the code too much but it seems fine. Help would really be appreciated, and thanks in advance.

Hi
Below suggestion is using a class "Matrix" containing the values to be added.
After creating those they are populated with some numbers.
Finally we are doing a row by row addition.

Hope this helps you

First create a form with a button on it

namespace MatrixAdd
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // Create some matrices to work with
            Matrix Matrix1 = new Matrix(3);
            Matrix Matrix2 = new Matrix(3);

            // Insert some values
            for( int i = 0; i < 3; i++)
                for (int j = 0; j < 3; j++)
                {
                    Matrix1[i, j] = (i + j + 12);
                    Matrix2[i, j] = (i+1) * (j+1);
                }

            // Calculate the result
            Matrix Result = Matrix1 + Matrix2;
        }
    }
}

Next define the matrix class used

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MatrixAdd
{
    class Matrix
    {
        int[,] _Matrix;
        static int _Size { get; set; }

        public Matrix(int size)
        {
            _Size = size;
            _Matrix = new int[size, size];
        }

        public static Matrix operator +(Matrix Matrix2Add, Matrix Matrix1Add)
        {
            Matrix resultMatrix = new Matrix(_Size);

            for (int i = 0; i < _Size; i++)
            { 
                for (int j = 0; j < _Size; j++)
                {
                    resultMatrix[i, j] = Matrix2Add[i, j] + Matrix1Add[i, j];
                }
            }
            return resultMatrix;
        }

        public int this[int i, int j]
        {
            get
            {
                return _Matrix[i, j];
            }

            set
            {
                _Matrix[i, j] = value;
            }
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.