I am trying to do a problem that i have previously done but then i am trying to add two more buttons to it, I have to add a A==B radio button and a "Make B Copy of A" Button. I will post my current code that i have on here. I also need to use a Matrix class which should contain:
In the Matrix class:
matrix is a 2D array of double
Cols is a read-only property that returns the number of columns in the matrix
Rows is a read-only property that returns the number of rows in the matrix
this is an indexer for a matrix. Note that for a matrix you need this[int i, int j] for both dimensions.
Matrix is a constructor the instantiates the matrix of desired size.
The add, subtract and multiply methods perform their respective op on the A and B matrices, returning a C result matrix. Note that they are called as C = A.add(B);
The colsEqual, rowsEqual and dimsEqual methods compare the number of columns, rows and dimensions between two matrices for equality. Note bool b = A.dimsEqual(B); returns true if the matrices have the same dimensions.
Equals is an override method that compares two matrices for equality. They are equal if both are null, or both have the same numeric values in the same element positions.
GetHashCode is an override method that is implemented as:
// Overrides the GetHashCode method
public override int GetHashCode()
{
// Use sum for hash code
return sum().GetHashCode(); // Note that double overrides this too
}
sum calculates the sum of all elements in the matrix.
makeId makes an n x n identity matrix.
clone returns a copy of this matrix.
copy accepts a matrix and copied its elements to this.
populateRand populates this matrix with random doubles.
populateOrd populates this matrix with sequential doubles from 1.0 in the first element to d in the last, as traversed by a nested loop.
ToString
The operator methods overload the operators to work with matrices.
This is what the program should look like in C# (This is what i have)
---------------------------------------------------------------------------------------------
namespace MatrixOpsProblem { public partial class frmMatrixOps2 : Form { double[,] MatrixA; double[,] MatrixB; public frmMatrixOps2() { InitializeComponent(); }
private void btnExit_Click(object sender, EventArgs e) { this.Close(); }
private void btnClearAll_Click(object sender, EventArgs e) { txtColsA.Text = ""; txtRowA.Text = ""; txtColsB.Text = ""; txtRowB.Text = ""; txtMatrixA.Text = ""; txtMatrixB.Text = ""; txtMatrixC.Text = ""; }
private void btnCalculate_Click(object sender, EventArgs e) { if(rbnAdd.Checked == true) { if (!(txtRowA.Text.Equals(txtRowB.Text))) MessageBox.Show("Dimensions must be equal for rows A and B.", "Entry Error"); if (!(txtColsA.Text.Equals(txtColsB.Text))) MessageBox.Show("Dimensions must be equal for Columns A and B.", "Entry Error"); else { if (txtMatrixA.Text.Equals("")) { btnMakeA.PerformClick(); RadioAdd(); } if (txtMatrixB.Text.Equals("")) { btnMakeB.PerformClick(); RadioAdd(); } if (!(txtMatrixA.Text.Equals("") && txtMatrixB.Text.Equals(""))) { RadioAdd(); } } } else if (rbnSub.Checked == true) { if (!(txtRowA.Text.Equals(txtRowB.Text))) MessageBox.Show("Dimensions must be equal for rows A and B.", "Entry Error"); if (!(txtColsA.Text.Equals(txtColsB.Text))) MessageBox.Show("Dimensions must be equal for Columns A and B.", "Entry Error"); else { if (txtMatrixA.Text.Equals("")) { btnMakeA.PerformClick(); RadioSubtract(); } if (txtMatrixB.Text.Equals("")) { btnMakeB.PerformClick(); RadioSubtract(); } if(!(txtMatrixA.Text.Equals("") && txtMatrixB.Text.Equals(""))) { RadioSubtract(); } } } else if (rbnMult.Checked == true) { if (!(txtColsA.Text.Equals(txtRowB.Text))) MessageBox.Show("Dimensions for Matris A column should be equal to Dimensions of Matrix B row.", "Entry Error"); else { if (txtMatrixA.Text.Equals("")) { btnMakeA.PerformClick(); RadioMultiply(); } if (txtMatrixB.Text.Equals("")) { btnMakeB.PerformClick(); RadioMultiply(); } if (!(txtMatrixA.Text.Equals("") && txtMatrixB.Text.Equals(""))) { RadioMultiply(); } } } } private void btnMakeA_Click(object sender, EventArgs e) { try { txtRowA.Focus(); int RowA = Convert.ToInt32(txtRowA.Text); if (RowA 10) { MessageBox.Show("Row A must be atleast 1 and less than 10"); txtRowA.Focus(); } int ColA = Convert.ToInt32(txtColsA.Text); if (ColA 10) { MessageBox.Show("Column A must be atleast 1 and less than 10"); txtColsA.Focus(); } MatrixA = CreateMatrix(RowA, ColA); txtMatrixA.Text = ConvertMatrixString(RowA, ColA, MatrixA); } catch (Exception) { if (txtRowA.Text == "") { MessageBox.Show("Row A is required.", "Entry Error"); txtRowA.Focus(); } if (txtColsA.Text == "") { MessageBox.Show("Column A is required.", "Entry Error"); txtColsA.Focus(); } } }
private void btnMakeB_Click(object sender, EventArgs e) { try { int RowB = Convert.ToInt32(txtRowB.Text); if (RowB 10) { MessageBox.Show("Row B must be atleast 1 and less than 10"); txtRowA.Focus(); } int ColB = Convert.ToInt32(txtColsB.Text); if (ColB 10) { MessageBox.Show("Column B must be atleast 1 and less than 10"); txtColsA.Focus(); } MatrixB = CreateMatrix(RowB, ColB); txtMatrixB.Text = ConvertMatrixString(RowB, ColB, MatrixB); } catch (Exception) { if(txtRowB.Text == "") { MessageBox.Show("Row B is required.", "Entry Error"); txtRowA.Focus(); } if (txtColsB.Text == "") { MessageBox.Show("Column B is required.", "Entry Error"); txtColsA.Focus(); } } } private double[,] CreateMatrix(int row, int col) {
double[,] matrix = new double[row, col];
Random autorand = new Random();
for (int i = 0; i
private double [,] CreateIMatrix(int row, int col) { double[,] matrix = new double[row, col]; Random autorand = new Random(); for (int i = 0; i
StringBuilder stringBulider = new StringBuilder();
for (int i = 0; i
private void RadioAdd() { double finalMatrixA = MatrixA[0, 1]; double finalMatrixB = MatrixB[0, 1];
int row = Convert.ToInt32(txtRowA.Text); int col = Convert.ToInt32(txtColsA.Text);
double[,] matrixC = new double[row, col];
for (int i = 0; i
private void RadioSubtract() { double finalMatrixA = MatrixA[0, 1]; double finalMatrixB = MatrixB[0, 1];
int row = Convert.ToInt32(txtRowA.Text); int col = Convert.ToInt32(txtColsA.Text);
double[,] matrixC = new double[row, col];
for (int i = 0; i
private void RadioMultiply() { double finalMatrixA = MatrixA[0, 1]; double finalMatrixB = MatrixB[0, 1];
int row = Convert.ToInt32(txtRowA.Text); int col = Convert.ToInt32(txtColsA.Text);
double[,] matrixC = new double[row, col];
for (int i = 0; i
private void btnMakeBId_Click(object sender, EventArgs e) { try { int RowB = Convert.ToInt32(txtRowB.Text); if (RowB 10) { MessageBox.Show("Row B must be atleast 1 and less than 10"); txtRowA.Focus(); } int ColB = Convert.ToInt32(txtColsB.Text); if (ColB 10) { MessageBox.Show("Column B must be atleast 1 and less than 10"); txtColsA.Focus(); } MatrixB = CreateIMatrix(RowB, ColB); txtMatrixB.Text = ConvertMatrixString(RowB, ColB, MatrixB); } catch (Exception) { if (txtRowB.Text == "") { MessageBox.Show("Row B is a required field.", "Entry Error"); txtRowA.Focus(); } if (txtColsB.Text == "") { MessageBox.Show("Column B is a required field.", "Entry Error"); txtColsA.Focus(); } } }
private void frmMatrixOpsProblem_Load(object sender, EventArgs e) { txtColsA.Text = "3"; txtColsB.Text = "3"; txtRowA.Text = "3"; txtRowB.Text = "3"; }
private void btnMakeBCopyA_Click(object sender, EventArgs e) { if (!Equals(MatrixA, null)) { MatrixB = MatrixA.Clone(); txtMatrixB.Text = ""; txtMatrixB.Text = MatrixB.ToString(); } else { MatrixB = null; txtMatrixB.Text = ""; } } } }
----------------------------------------------------------------------------------
namespace MatrixOpsProblem {
partial class frmMatrixOps2 { /// /// Required designer variable. /// private System.ComponentModel.IContainer components = null;
/// /// Clean up any resources being used. /// /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); }
#region Windows Form Designer generated code
/// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.gbxSelectOp = new System.Windows.Forms.GroupBox(); this.rbnSub = new System.Windows.Forms.RadioButton(); this.rbnAdd = new System.Windows.Forms.RadioButton(); this.rbnMult = new System.Windows.Forms.RadioButton(); this.txtColsB = new System.Windows.Forms.TextBox(); this.txtColsA = new System.Windows.Forms.TextBox(); this.txtRowB = new System.Windows.Forms.TextBox(); this.txtRowA = new System.Windows.Forms.TextBox(); this.lblCols = new System.Windows.Forms.Label(); this.lblRows = new System.Windows.Forms.Label(); this.btnExit = new System.Windows.Forms.Button(); this.btnClearAll = new System.Windows.Forms.Button(); this.btnCalculate = new System.Windows.Forms.Button(); this.btnMakeBId = new System.Windows.Forms.Button(); this.btnMakeB = new System.Windows.Forms.Button(); this.btnMakeA = new System.Windows.Forms.Button(); this.lblMatrixC = new System.Windows.Forms.Label(); this.lblMatrixB = new System.Windows.Forms.Label(); this.lblMatrixA = new System.Windows.Forms.Label(); this.txtMatrixC = new System.Windows.Forms.TextBox(); this.txtMatrixB = new System.Windows.Forms.TextBox(); this.txtMatrixA = new System.Windows.Forms.TextBox(); this.btnMakeBCopyA = new System.Windows.Forms.Button(); this.rbnAequalB = new System.Windows.Forms.RadioButton(); this.gbxSelectOp.SuspendLayout(); this.SuspendLayout(); // // gbxSelectOp // this.gbxSelectOp.Controls.Add(this.rbnAequalB); this.gbxSelectOp.Controls.Add(this.rbnSub); this.gbxSelectOp.Controls.Add(this.rbnAdd); this.gbxSelectOp.Controls.Add(this.rbnMult); this.gbxSelectOp.Location = new System.Drawing.Point(14, 283); this.gbxSelectOp.Name = "gbxSelectOp"; this.gbxSelectOp.Size = new System.Drawing.Size(280, 50); this.gbxSelectOp.TabIndex = 39; this.gbxSelectOp.TabStop = false; this.gbxSelectOp.Text = "Select Operation"; // // rbnSub // this.rbnSub.AutoSize = true; this.rbnSub.Location = new System.Drawing.Point(140, 27); this.rbnSub.Name = "rbnSub"; this.rbnSub.Size = new System.Drawing.Size(65, 17); this.rbnSub.TabIndex = 2; this.rbnSub.TabStop = true; this.rbnSub.Text = "Subtract"; this.rbnSub.UseVisualStyleBackColor = true; // // rbnAdd // this.rbnAdd.AutoSize = true; this.rbnAdd.Location = new System.Drawing.Point(81, 27); this.rbnAdd.Name = "rbnAdd"; this.rbnAdd.Size = new System.Drawing.Size(44, 17); this.rbnAdd.TabIndex = 1; this.rbnAdd.TabStop = true; this.rbnAdd.Text = "Add"; this.rbnAdd.UseVisualStyleBackColor = true; // // rbnMult // this.rbnMult.AutoSize = true; this.rbnMult.Location = new System.Drawing.Point(15, 27); this.rbnMult.Name = "rbnMult"; this.rbnMult.Size = new System.Drawing.Size(60, 17); this.rbnMult.TabIndex = 0; this.rbnMult.TabStop = true; this.rbnMult.Text = "Multiply"; this.rbnMult.UseVisualStyleBackColor = true; // // txtColsB // this.txtColsB.Location = new System.Drawing.Point(237, 246); this.txtColsB.Name = "txtColsB"; this.txtColsB.Size = new System.Drawing.Size(100, 20); this.txtColsB.TabIndex = 38; // // txtColsA // this.txtColsA.Location = new System.Drawing.Point(237, 216); this.txtColsA.Name = "txtColsA"; this.txtColsA.Size = new System.Drawing.Size(100, 20); this.txtColsA.TabIndex = 37; // // txtRowB // this.txtRowB.Location = new System.Drawing.Point(131, 246); this.txtRowB.Name = "txtRowB"; this.txtRowB.Size = new System.Drawing.Size(100, 20); this.txtRowB.TabIndex = 36; // // txtRowA // this.txtRowA.Location = new System.Drawing.Point(131, 216); this.txtRowA.Name = "txtRowA"; this.txtRowA.Size = new System.Drawing.Size(100, 20); this.txtRowA.TabIndex = 35; // // lblCols // this.lblCols.AutoSize = true; this.lblCols.Location = new System.Drawing.Point(247, 200); this.lblCols.Name = "lblCols"; this.lblCols.Size = new System.Drawing.Size(27, 13); this.lblCols.TabIndex = 34; this.lblCols.Text = "Cols"; // // lblRows // this.lblRows.AutoSize = true; this.lblRows.Location = new System.Drawing.Point(131, 200); this.lblRows.Name = "lblRows"; this.lblRows.Size = new System.Drawing.Size(34, 13); this.lblRows.TabIndex = 33; this.lblRows.Text = "Rows"; // // btnExit // this.btnExit.Location = new System.Drawing.Point(384, 351); this.btnExit.Name = "btnExit"; this.btnExit.Size = new System.Drawing.Size(75, 23); this.btnExit.TabIndex = 32; this.btnExit.Text = "&Exit"; this.btnExit.UseVisualStyleBackColor = true; this.btnExit.Click += new System.EventHandler(this.btnExit_Click); // // btnClearAll // this.btnClearAll.Location = new System.Drawing.Point(265, 351); this.btnClearAll.Name = "btnClearAll"; this.btnClearAll.Size = new System.Drawing.Size(113, 23); this.btnClearAll.TabIndex = 31; this.btnClearAll.Text = "Clear &Matrices"; this.btnClearAll.UseVisualStyleBackColor = true; this.btnClearAll.Click += new System.EventHandler(this.btnClearAll_Click); // // btnCalculate // this.btnCalculate.Location = new System.Drawing.Point(14, 351); this.btnCalculate.Name = "btnCalculate"; this.btnCalculate.Size = new System.Drawing.Size(75, 23); this.btnCalculate.TabIndex = 30; this.btnCalculate.Text = "&Calculate"; this.btnCalculate.UseVisualStyleBackColor = true; this.btnCalculate.Click += new System.EventHandler(this.btnCalculate_Click); // // btnMakeBId // this.btnMakeBId.Location = new System.Drawing.Point(343, 244); this.btnMakeBId.Name = "btnMakeBId"; this.btnMakeBId.Size = new System.Drawing.Size(128, 23); this.btnMakeBId.TabIndex = 29; this.btnMakeBId.Text = "Make B identity"; this.btnMakeBId.UseVisualStyleBackColor = true; this.btnMakeBId.Click += new System.EventHandler(this.btnMakeBId_Click); // // btnMakeB // this.btnMakeB.Location = new System.Drawing.Point(14, 244); this.btnMakeB.Name = "btnMakeB"; this.btnMakeB.Size = new System.Drawing.Size(111, 23); this.btnMakeB.TabIndex = 27; this.btnMakeB.Text = "Make Matrix B"; this.btnMakeB.UseVisualStyleBackColor = true; this.btnMakeB.Click += new System.EventHandler(this.btnMakeB_Click); // // btnMakeA // this.btnMakeA.Location = new System.Drawing.Point(14, 214); this.btnMakeA.Name = "btnMakeA"; this.btnMakeA.Size = new System.Drawing.Size(111, 23); this.btnMakeA.TabIndex = 26; this.btnMakeA.Text = "Make Matrix A"; this.btnMakeA.UseVisualStyleBackColor = true; this.btnMakeA.Click += new System.EventHandler(this.btnMakeA_Click); // // lblMatrixC // this.lblMatrixC.AutoSize = true; this.lblMatrixC.Location = new System.Drawing.Point(333, 9); this.lblMatrixC.Name = "lblMatrixC"; this.lblMatrixC.Size = new System.Drawing.Size(45, 13); this.lblMatrixC.TabIndex = 22; this.lblMatrixC.Text = "Matrix C"; // // lblMatrixB // this.lblMatrixB.AutoSize = true; this.lblMatrixB.Location = new System.Drawing.Point(174, 9); this.lblMatrixB.Name = "lblMatrixB"; this.lblMatrixB.Size = new System.Drawing.Size(45, 13); this.lblMatrixB.TabIndex = 21; this.lblMatrixB.Text = "Matrix B"; // // lblMatrixA // this.lblMatrixA.AutoSize = true; this.lblMatrixA.Location = new System.Drawing.Point(26, 9); this.lblMatrixA.Name = "lblMatrixA"; this.lblMatrixA.Size = new System.Drawing.Size(45, 13); this.lblMatrixA.TabIndex = 20; this.lblMatrixA.Text = "Matrix A"; // // txtMatrixC // this.txtMatrixC.Location = new System.Drawing.Point(321, 34); this.txtMatrixC.Multiline = true; this.txtMatrixC.Name = "txtMatrixC"; this.txtMatrixC.ReadOnly = true; this.txtMatrixC.Size = new System.Drawing.Size(150, 150); this.txtMatrixC.TabIndex = 25; // // txtMatrixB // this.txtMatrixB.Location = new System.Drawing.Point(168, 34); this.txtMatrixB.Multiline = true; this.txtMatrixB.Name = "txtMatrixB"; this.txtMatrixB.ReadOnly = true; this.txtMatrixB.Size = new System.Drawing.Size(150, 150); this.txtMatrixB.TabIndex = 24; // // txtMatrixA // this.txtMatrixA.Location = new System.Drawing.Point(14, 34); this.txtMatrixA.Multiline = true; this.txtMatrixA.Name = "txtMatrixA"; this.txtMatrixA.ReadOnly = true; this.txtMatrixA.Size = new System.Drawing.Size(150, 150); this.txtMatrixA.TabIndex = 23; // // btnMakeBCopyA // this.btnMakeBCopyA.Location = new System.Drawing.Point(343, 214); this.btnMakeBCopyA.Name = "btnMakeBCopyA"; this.btnMakeBCopyA.Size = new System.Drawing.Size(128, 23); this.btnMakeBCopyA.TabIndex = 40; this.btnMakeBCopyA.Text = "Make B Copy of A"; this.btnMakeBCopyA.UseVisualStyleBackColor = true; this.btnMakeBCopyA.Click += new System.EventHandler(this.btnMakeBCopyA_Click); // // rbnAequalB // this.rbnAequalB.AutoSize = true; this.rbnAequalB.Location = new System.Drawing.Point(217, 27); this.rbnAequalB.Name = "rbnAequalB"; this.rbnAequalB.Size = new System.Drawing.Size(57, 17); this.rbnAequalB.TabIndex = 3; this.rbnAequalB.TabStop = true; this.rbnAequalB.Text = "A == B"; this.rbnAequalB.UseVisualStyleBackColor = true; // // frmMatrixOps2 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(489, 391); this.Controls.Add(this.btnMakeBCopyA); this.Controls.Add(this.gbxSelectOp); this.Controls.Add(this.txtColsB); this.Controls.Add(this.txtColsA); this.Controls.Add(this.txtRowB); this.Controls.Add(this.txtRowA); this.Controls.Add(this.lblCols); this.Controls.Add(this.lblRows); this.Controls.Add(this.btnExit); this.Controls.Add(this.btnClearAll); this.Controls.Add(this.btnCalculate); this.Controls.Add(this.btnMakeBId); this.Controls.Add(this.btnMakeB); this.Controls.Add(this.btnMakeA); this.Controls.Add(this.txtMatrixC); this.Controls.Add(this.txtMatrixB); this.Controls.Add(this.txtMatrixA); this.Controls.Add(this.lblMatrixC); this.Controls.Add(this.lblMatrixB); this.Controls.Add(this.lblMatrixA); this.Name = "frmMatrixOps2"; this.Text = "Matrix Ops"; this.Load += new System.EventHandler(this.frmMatrixOpsProblem_Load); this.gbxSelectOp.ResumeLayout(false); this.gbxSelectOp.PerformLayout(); this.ResumeLayout(false); this.PerformLayout();
}
#endregion
private System.Windows.Forms.GroupBox gbxSelectOp; private System.Windows.Forms.RadioButton rbnSub; private System.Windows.Forms.RadioButton rbnAdd; private System.Windows.Forms.RadioButton rbnMult; private System.Windows.Forms.TextBox txtColsB; private System.Windows.Forms.TextBox txtColsA; private System.Windows.Forms.TextBox txtRowB; private System.Windows.Forms.TextBox txtRowA; private System.Windows.Forms.Label lblCols; private System.Windows.Forms.Label lblRows; private System.Windows.Forms.Button btnExit; private System.Windows.Forms.Button btnClearAll; private System.Windows.Forms.Button btnCalculate; private System.Windows.Forms.Button btnMakeBId; private System.Windows.Forms.Button btnMakeB; private System.Windows.Forms.Button btnMakeA; private System.Windows.Forms.Label lblMatrixC; private System.Windows.Forms.Label lblMatrixB; private System.Windows.Forms.Label lblMatrixA; private System.Windows.Forms.TextBox txtMatrixC; private System.Windows.Forms.TextBox txtMatrixB; private System.Windows.Forms.TextBox txtMatrixA; private System.Windows.Forms.RadioButton rbnAequalB; private System.Windows.Forms.Button btnMakeBCopyA; } }
Matrix Ops with objects Matrix A Matrix B Matrix C Cols Rows Make Matrix A 3 13 Make B Copy of A Make Matrix B Make B Identity Select Operation o Multiply O Add O Subtract O A B Calculate Clear Matrices