Hello, I'm having an issue with a windows forms application assignment, it's supposed to take a list of variables entered in the score and compute the average/variance of those scores when the buttons are pressed, I wasn't sure how to do the variance (button 4) but my Average function on button 3 is having an error that says "does not have a definition for Average" I'm not sure what I'm doing wrong, any help would be greatly appreciated.

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 Assignment_4_Forms_App
{

    public partial class Form1 : Form
    {
        struct Student
    {
        public string name;
        public List<double> scores;
        public Student(string name, List<double> scores)
            {
                this.name = name;
                this.scores = scores;
            }

     }
        Student MyStudent = new Student(" ", new List<double>());

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            TextBoxName.Clear();
            TextBoxName.ReadOnly = true;
            ButtonEnterName.Visible = !ButtonEnterName.Visible;
        }
        private void button1_VisibleChanged(object sender, EventArgs e)
        {

        }
        private void button2_Click(object sender, EventArgs e)
        {
            MyStudent.scores = new List<double>();
            MyStudent.scores.Add(int.Parse(TextBoxScore.Text));
        }  
        private void button3_Click(object sender, EventArgs e)
        {
            LabelAverage.Text = Convert.ToString(MyStudent.Average(MyStudent.scores));
        }
        private void button4_Click(object sender, EventArgs e)
        {

        }
        }
        }

Recommended Answers

All 4 Replies

I did some more work and I'm at this:

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 Assignment_4_Forms_App
{

    public partial class Form1 : Form
    {
        struct Student
    {
        public string name;
        public List<double> scores;
        public double Average(List<double> listValues)
        {
            double sum = 0;
            foreach (double value in listValues)
            {
                sum += value;
            }
            return (double)sum / listValues.Count;
        }

        public double Variance(List<double> listValues)
        {
            double mean = listValues.Average();
            double sos = 0;
            for (int i = 0; i < listValues.Count; i++)
            {
                sos += Math.Pow((listValues[i] - mean), 2);
            }
            return sos / (listValues.Count - 1);}
        public Student(string name, List<double> scores)
            {
                this.name = name;
                this.scores = scores;
            }

     }
        Student MyStudent = new Student(" ", new List<double>());
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            TextBoxName.Clear();
            TextBoxName.ReadOnly = true;
            ButtonEnterName.Visible = !ButtonEnterName.Visible;
        }
        private void button1_VisibleChanged(object sender, EventArgs e)
        {

        }
        private void button2_Click(object sender, EventArgs e)
        {
            MyStudent.scores = new List<double>();
            MyStudent.scores.Add(int.Parse(TextBoxScore.Text));
        }  
        private void button3_Click(object sender, EventArgs e)
        {
            LabelAverage.Text = Convert.ToString(MyStudent.Average(MyStudent.scores));
        }
        private void button4_Click(object sender, EventArgs e)
        {
            LabelVariance.Text = Convert.ToString(MyStudent.Variance(MyStudent.scores));
        }
        }
        }

My Variance/Average aren't working correctly, how might I fix this?

.

You say they aren't working but what do you mean? How are they not working? Crash? Wrong Result? Won't compile?

At a glance, I'd say because you're parsing your input into Integer format (line 69).

Surely if you're using the double type, you need to parse it into a double?

Yes. Ketsuekiame right,
You need to parse double value with trim() funcations.
other than that your code looks good there

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.