BFMracing

General Category => General Board => Homework Haven => Topic started by: Dusaro on September 27, 2012, 11:54:54 PM

Title: 2003 NZ Computer Sciences Scholarship past exam
Post by: Dusaro on September 27, 2012, 11:54:54 PM
Okay, ive been enrolled in the Waikato University scholarship exams, im doing the past exams and i got the question:
Quote
2. Numbers and Statistics (Careful and Accurate Programming)
Your programming work in this question will be assessed on two criteria:
(a) Completeness and accuracy of the program.
(b) Good presentation. That is, it should make good use of programming language
facilities, be well organised, neatly laid out, and lightly commented.
Your task is to write a program to read in a series of numbers and compute some
statistics. The way in which you should handle input, and the statistics you should
calculate, are as illustrated in the sample run following (data input by the user is
underlined)
Please enter the number of values to be processed: 10
Enter value 1: 2.3
Enter value 2: 6
Enter value 3: 5
Enter value 4: 8
Enter value 5: 13.4
Enter value 6: 19
Enter value 7: 3
Enter value 8: 1.7
Enter value 9: 1.4
Enter value 10: 16
You entered 10 values.
Your values add to 75.8
The average of your values is 7.58
The largest value is 19
The smallest value is 1.4
The variance of your values is 40.68
Note: To calculate variance: sum the squares of the differences of each value from
the average and divide the sum by one less than the number of values. For
the example:
(2.3 – 7.58)2 + (6 – 7.58)2 + (5 – 7.58)2 + (8 – 7.58)2 +
(13.4 – 7.58)2 + (19 – 7.58)2 + (3 – 7.58)2 +
(1.7 – 7.58)2 + (1.4 – 7.58)2 + (16 – 7.58)2
all divided by 9

my code to solve it is:
Code: [Select]
            List<double> val = new List<double>();
            val.Add(2.3);
            val.Add(6);
            val.Add(5);
            val.Add(8);
            val.Add(13.4);
            val.Add(19);
            val.Add(3);
            val.Add(1.7);
            val.Add(1.4);
            val.Add(16);
            MessageBox.Show(val.Count.ToString());
            MessageBox.Show(val.Sum().ToString());
            double av = val.Average();
            double a = 0;
            int numvals = val.Count-1;
            foreach (double i in val)
            {
                //MessageBox.Show("("+i.ToString()+" - "+ av.ToString()+")^2");
                a = i - Math.Pow(av, 2);
            }
            a = a / numvals;
            MessageBox.Show(a.ToString());

Whats wrong with it?
the answer it gives is: -4.60626666666667

okay, i modified it a bit and now its returning: -55.4182222222222
Code: [Select]
List<double> val = new List<double>();
            val.Add(2.3);
            val.Add(6);
            val.Add(5);
            val.Add(8);
            val.Add(13.4);
            val.Add(19);
            val.Add(3);
            val.Add(1.7);
            val.Add(1.4);
            val.Add(16);
            MessageBox.Show(val.Count.ToString());
            MessageBox.Show(val.Sum().ToString());
            double av = val.Average();
            double a = 0;
            string b;
            int numvals = val.Count-1;
            MessageBox.Show(numvals.ToString());
            foreach (double i in val)
            {
                //MessageBox.Show("("+i.ToString()+" - "+ av.ToString()+")^2");
                a = a+(i - Math.Pow(av, 2));
            }
            MessageBox.Show(a.ToString());
            a = a / numvals;
            txtOutput.Text = a.ToString();

okay guys, i figured what i did wrong :/

Code: [Select]
            List<double> val = new List<double>();
            val.Add(2.3);
            val.Add(6);
            val.Add(5);
            val.Add(8);
            val.Add(13.4);
            val.Add(19);
            val.Add(3);
            val.Add(1.7);
            val.Add(1.4);
            val.Add(16);
            MessageBox.Show(val.Count.ToString());
            MessageBox.Show(val.Sum().ToString());
            double av = val.Average();
            double a = 0;
            string b;
            int numvals = val.Count-1;
            MessageBox.Show(numvals.ToString());
            foreach (double i in val)
            {
                //MessageBox.Show("("+i.ToString()+" - "+ av.ToString()+")^2");
                MessageBox.Show(a.ToString());
                a = a+Math.Pow((i - av),2);
            }
            MessageBox.Show(a.ToString());
            a = a / numvals;
            txtOutput.Text = a.ToString();

The correct answer is: 40.68
Title: Re: 2003 NZ Computer Sciences Scholarship past exam
Post by: jim360 on September 28, 2012, 06:46:05 AM
There you are, as I was tracking the code it looked in the first example that the Variance was written down wrong.

A couple of notes on presentation:

1 - Currently you have no comments, shove in a few here and there.

2 - The variable names are very unclear - why not just use "average" and "variance" for av and a?

3 -

I'm not good on telling languages apart, I only work in C++ (and when I say "work", I mean steal other people's code and make token changes to it, but anyway...), but it looks like you must be using an inbuilt average calculation because nowhere is there a hand-written code for calculating the average. I think if I were marking this program I'd expect people to write something like the following (though this is in C++ and so is just a guide):

Code: [Select]
double average=0;
double sum = 0;

 for (int i = ; i< numvals; i++)
{

sum = sum + val.add(i) ;

//This code probably would fail but I'd be trying here to add each value to the sum in turn.
//I've only used the val.add(i) thing so that it's clear which part of the program you wrote I'd be wanting to use here.

}

average = sum/(numvals+1); //Or just use val.count but you know what I mean.

//Also note the heavy use of line breaks to space the code out.

The other thing is that at the moment the code seems very inflexible - to calculate the mean and variance for a new set of values seems to involve inputting the fresh data into the code by hand. That's going to have to be changed, not sure how. Might need to call data from some file or other, that would be the easiest. Don't ask me how to do this!

Anyway good to see you have the correct output result for the Variance, at least that's the maths of the program sorted.
Title: Re: 2003 NZ Computer Sciences Scholarship past exam
Post by: Dusaro on September 28, 2012, 03:06:06 PM
In the paper they tell you to make the code as effective and accurate as possible, use the least amount of code you can. so I used an inbuilt function in C#.
Title: Re: 2003 NZ Computer Sciences Scholarship past exam
Post by: BFM_Fénix on September 28, 2012, 11:18:26 PM
I guess all you wanted help with (even tho you answered yourself) was the variance calculation, am I right?
Title: Re: 2003 NZ Computer Sciences Scholarship past exam
Post by: Dusaro on September 28, 2012, 11:56:52 PM
yeah