hi, i need help on to receive input from user and stored it in array. i already tru to code but
it does not return result, and display result. can someone help me. thank you.
input: 3,4
8,2
6,7
listbox2 : place to insert input matrix
listbox1 : to display input matrix
btnAdd : to add input matrix and stored in nums array
btnCompute : to compute the average , variance result
here is my code;
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespaceMahalanobis_Distance
{
publicpartialclassForm1 : Form
{
privateint[,] nums;
privateint _count = 0;
int x, y;
publicForm1()
{
InitializeComponent();
}
privatevoidlistBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if (!Char.IsDigit(e.KeyChar) && e.KeyChar != Char.Parse(","))
e.Handled = true;
}
// Compute and display resultprivatevoidbtnCompute_Click(object sender, EventArgs e)
{
meanX1.Text = AverageX(nums).ToString();
variance.Text = varianceX(nums).ToString();
// stdDev.Text = stdDeviationOfX;
}
// Calculate standard Deviation ValueprivatedoublestdDeviation (double varianceX)
{
double stdDevX = Math.Sqrt(varianceX);
return stdDevX;
}
// Calculate Variance ValueprivatedoublevarianceX(int[,] nums)
{
int NumMatrix =0;
double varX =0;
double sumSquare=0;
// Get the Means@Average of X values double avgX = AverageX(nums);
// Declare the total Matrix
NumMatrix = nums.Length;
// Calculate the variance for (int i = 0; i <= NumMatrix; i++)
{
sumSquare += Math.Pow((double)nums[i, 0]- avgX, 2);
}
varX= sumSquare/NumMatrix;
return varX;
}
// Average value of X matrikprivatedoubleAverageX(int[,] nums)
{
double avgValueX = 0;
double sumValueX = 0;
for (int i = 0; i <=nums.GetUpperBound(0); i++)
{
// Total Value Matrix X
sumValueX += (double)nums[i, 0];
}
avgValueX = sumValueX / (nums.GetUpperBound(0) + 1);
return avgValueX;
}
// insert input matrix example (7,8) in listBox and stored in 2-dimensional arrayprivatevoidbtnAdd_Click(object sender, EventArgs e)
{
nums = newint[listBox2.Items.Count, 2];
for (int i = 0; i < this.listBox2.Items.Count; i++)
{
if (_count < 20 && this.listBox2.Items[i].ToString() != "" && this.listBox2.Items[i].ToString().Contains(","))
{
string t = this.listBox2.Items[i].ToString();
string sx = t.Substring(0, t.IndexOf(","));
Int32.TryParse(sx, out x);
t = t.Substring(t.IndexOf(",") + 1);
Int32.TryParse(t, out y);
this.nums[_count, 0] = x;
this.nums[_count, 1] = y;
_count++;
}
}
}
// Show input matrix in ListBox2privatevoidshowMatrixList(int[,] nums)
{
for (int i = 0; i < listBox2.Items.Count; i++)
{
this.listBox1.Items.Add("X:" + x.ToString() + "; Y:" + y.ToString());
}
}
}
}
really appreciate for the help
Answers
Know the answer? Post it. Somebody with the same question will find it here.
Answers
Know the answer? Post it. Somebody with the same question will find it here.