Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on a matrix problem in C# windows form application and i have gotten alot of the code to work but cant get

I am working on a matrix problem in C# windows form application and i have gotten alot of the code to work but cant get the calculate button to work. When i get the matrix to be generated and click multiply or add or subtract it automaticly calculates without me pressing the calculate button. Another thing that isnt working is the rows and cols text boxes should have a default value of 3 when the program is ran. Also i cant seem to figure out how to Make the matrix and Identiy. When i clik the Make B identity it should convert the matrix to an identity but cant seem to figure it out. could you please fix the code so i could understand what changes i should make in order for it to work.

image text in transcribed

This is the code that i have:

===================================================

namespace MatrixOpsProblem { partial class frmMatrixOpsProblem { ///

/// 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.gbxSelectOp.SuspendLayout(); this.SuspendLayout(); // // gbxSelectOp // 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(237, 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(163, 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; this.rbnSub.CheckedChanged += new System.EventHandler(this.rbnSub_CheckedChanged); // // rbnAdd // this.rbnAdd.AutoSize = true; this.rbnAdd.Location = new System.Drawing.Point(94, 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; this.rbnAdd.CheckedChanged += new System.EventHandler(this.rbnAdd_CheckedChanged); // // 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; this.rbnMult.CheckedChanged += new System.EventHandler(this.rbnMult_CheckedChanged); // // 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(116, 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; // // frmMatrixOpsProblem // 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.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 = "frmMatrixOpsProblem"; this.Text = "Form1"; 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; } }

=======================================================

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms;

namespace MatrixOpsProblem { public partial class frmMatrixOpsProblem : Form { double[,] MatrixA; double[,] MatrixB; public frmMatrixOpsProblem() { 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(Equals(rbnAdd)) { double fMatrixA = MatrixA[0, 1]; double fMatrixB = 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 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("Col A entry 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 a required field.", "Entry Error"); txtRowA.Focus(); } if (txtColsA.Text == "") { MessageBox.Show("Col A is a required field.", "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("Col B entry 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 a required field.", "Entry Error"); txtRowA.Focus(); } if (txtColsB.Text == "") { MessageBox.Show("Col B is a required field.", "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

StringBuilder stringBulider = new StringBuilder();

for (int i = 0; i

} return stringBulider.ToString(); }

private void rbnAdd_CheckedChanged(object sender, EventArgs e) { 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

txtMatrixC.Text = ConvertMatrixString(row, col, matrixC); }

private void rbnSub_CheckedChanged(object sender, EventArgs e) { 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

txtMatrixC.Text = ConvertMatrixString(row, col, matrixC); }

private void rbnMult_CheckedChanged(object sender, EventArgs e) { 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

txtMatrixC.Text = ConvertMatrixString(row, col, matrixC); }

private void btnMakeBId_Click(object sender, EventArgs e) {

} } }

The Select Operation GroupBox has 3 RadioButton objects to multiply matrices, add matrices and subtract matrices, respectively. The Multiply RadioButton should be checked when the form loads (Hint: double-click on the form-What event is created?). Make sure that only one RadioButton can be selected at a time. The calculate Button should perform the operation selected in the Select Operation GroupBox. Make sure that no operation can crash your application and that all operations can only execute correctly. You can use a validation approach or handle the exceptions. In any case, provide a clear message to the user what went wrong and how to correct the problem. Things to consider: 1. The A and B matrix are not nu 2. For multiplication, the rows of A match the columns of B and the columns of A match the rows of B 3. For addition and subtraction the A and B matrices should have the same dimensions. When calculate is clicked, the C matrix should be created with the proper dimensions to contain the result of the operation given the sizes of A and B. After selecting Multiply and clicking Calculate, the form should have the result in the Matrix C TextBox as shown below. Matrix ops A Marne B 3 Make Malik A Make Matt Make B identity Select Operaton Mutoly O Add O subtract

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database And Expert Systems Applications 19th International Conference Dexa 2008 Turin Italy September 2008 Proceedings Lncs 5181

Authors: Sourav S. Bhowmick ,Josef Kung ,Roland Wagner

2008th Edition

3540856536, 978-3540856535

More Books

Students also viewed these Databases questions