Im trying to make a mean and standard deviation calculator on visual studio using windows form. It's for a class and I have to use windows form and you have to be able to input data manually or select .txt file wiht the data in it.
Its hard to explain what i need to do because I'm new to this. Basically i need to the what button1_Click does but do the same thing for button2_Click. The thing is, is that it reads a file rather than taking input from user. It stores (not sure if thats the correct word) the values to a list not an array I think. I don't know if i can convert it to an array or add the values to the list as an array or what. I'm really lost!
Here is the exact wording of my professor for this assignment:
"Write a program, using C#, that will find the mean and standard deviation of a number of data points. The program should allow the user to either enter data manually or via a text file."
Any help would be great I'm new to this and my teacher offers little to no help and leaves us to search the internet for answers.
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 BsweeneyCsharp3_1_
{
public partial class Form1 : Form
{
List<double> values;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
values = new List<double>();
}
void ShowValues()
{
listBox1.Items.Clear();
for (int i = 0; i < values.Count; i++)
listBox1.Items.Add(values[i]);
}
private void button1_Click(object sender, EventArgs e)
{
double value = 0.00;
double sum = 0.00, sumSquares = 0.00, squareSums;
double stdDev = 0.00, mean = 0.00;
if (textBox1.Text.Length == 0)
{
MessageBox.Show("You must enter a value.", "Standard Deviation");
return;
}
try
{
value = double.Parse(textBox1.Text);
values.Add(value);
ShowValues();
textBox1.Text = "";
textBox1.Focus();
}
catch (FormatException)
{
MessageBox.Show("The value you entered is invalid.",
"");
}
for (int i = 0; i < values.Count; i++)
{
sum += values[i];
}
mean = sum / values.Count;
squareSums = sum * sum;
for (int i = 0; i < values.Count; i++)
sumSquares += (values[i] * values[i]);
double numerator = values.Count * sumSquares - squareSums;
double denominator = values.Count * (values.Count - 1);
stdDev = Math.Sqrt(numerator / denominator);
textBox2.Text = mean.ToString();
textBox3.Text = stdDev.ToString("F");
}
private void button2_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "TXT|*.txt";
double mean = 0.00, stdDev = 0.00, sum = 0.00;
double sumSquares = 0.00, squareSums = 0.00;
int counter = 0;
if (ofd.ShowDialog() == DialogResult.OK)
{
textBox4.Text = ofd.FileName;
}
string line;
System.IO.StreamReader file = new System.IO.StreamReader(textBox4.Text);
List<string> list = new List<string>();
while ((line = file.ReadLine()) != null)
{
listBox2.Items.Add(line);
var dbl = Convert.ToDouble(line);
sum += dbl;
counter++;
}
if (counter > 0)
{
mean = sum / counter;
squareSums += sum * sum;
}
if(counter > 0)
{
sumSquares += Math.Pow((sum - mean), 2);
double numerator = counter * sumSquares - squareSums;
double denominator = counter * (counter - 1);
stdDev = Math.Sqrt(numerator / denominator);
textBox2.Text = mean.ToString();
textBox3.Text = numerator.ToString();
}
}
}
}
I did my best to interpret what you wrote. I took into consideration that you are a new programmer. I avoided major refactoring including adding a much needed method. Start learning about methods/functions which gives you code re-use. A method prevents the problem you have here. The problem is you are calculating the same thing in two different ways.
You stated that the "manual" method (not file read) works correctly so I took that as the source of truth.
relevant code changes to button2_Click
if (counter > 0)//you don't need the same check 2x
{
mean = sum / counter;
for (int i = 0; i < listBox2.Items.Count; i++)
sumSquares += (Convert.ToDouble(listBox2.Items[i]) * Convert.ToDouble(listBox2.Items[i]));
squareSums += sum * sum;
//sumSquares += Math.Pow((sum - mean), 2);//You changed the formula again here
double numerator = counter * sumSquares - squareSums;
double denominator = counter * (counter - 1);
stdDev = Math.Sqrt(numerator / denominator);
textBox2.Text = mean.ToString();
//textBox3.Text = numerator.ToString();//You changed how you did things again
textBox3.Text = stdDev.ToString("F");
}
FYI, I didn't check your math.