ENGR/CS 101 CS Session Lecture 17 Start Microsoft Visual Studio 2012

advertisement
ENGR/CS 101 CS Session
Lecture 17



Log into Windows (reboot if in Linux)
Start Microsoft Visual Studio 2012
Open Statistics project
Lecture 17
ENGR/CS 101 Computer Science Session
1
Outline



Computing average
Computing standard deviation
Submit project
Lecture 17
ENGR/CS 101 Computer Science Session
2
Computing Average


Double-click on the Average button. In this
handler, we write the code for computing the
average value of the numbers in the items
array.
The basic idea is add up all of the numbers,
then divide by the number of elements.
Lecture 17
ENGR/CS 101 Computer Science Session
3
Computing Average

Data that is needed



sum – a real number
average – a real number
Algorithm
1. Initialize sum to 0
2. For each element of the items array
2.1 Add the element to sum
3. Compute average = sum/number of items
4. Display average in the average textbox
Lecture 17
ENGR/CS 101 Computer Science Session
4
averageBtn_Click code
// declare vars and initialize sum
double average;
double sum = 0;
// for each element of items array
for (int i = 0; i < num_items; i++)
{ // add element to sum
sum = sum + items[i];
}
// compute average
average = sum/num_items;
// display the result
averageBox.Text = average.ToString();
Lecture 17
ENGR/CS 101 Computer Science Session
5
Computing Standard Deviation



Double-click on the Std Dev button.
Standard deviation is a statistic used to
describe the amount of variance a data set
has with respect to the average value.
The simplest formula for standard deviation is
𝑠𝑁 =
1
𝑁
𝑁
𝑖=1(𝑥𝑖
− 𝑥 )2
where N is the number of data items, and 𝑥
is the average value of the data set.
Lecture 17
ENGR/CS 101 Computer Science Session
6
Computing Standard Deviation



The summation indicates the need for a
counting for-loop
Exponentiation 𝑥 𝑦 can be computed using
method Math.Pow (x, y)
Square root 𝑥 can be computed using
method Math.Sqrt(x)
Lecture 17
ENGR/CS 101 Computer Science Session
7
Refactoring a
compute_average method


Although we have a handler that computes
the average value of the numbers in the
items array, we cannot reuse the code as is.
To reuse the code, we need to create a
separate method that will compute and return
the average value. We can do this by
refactoring.
Lecture 17
ENGR/CS 101 Computer Science Session
8
Refactoring a
compute_average method



Highlight the code that computes the
average.
Right-click on it, go to Refactor, and select
Extract Method
Name the new method "compute_average"
and click OK. This will create a method that
computes and returns the average value, and
replace the code in the handler to a call to
that method.
Lecture 17
ENGR/CS 101 Computer Science Session
9
Computing Standard Deviation

Data that is needed


sum, average, stddev – real numbers
Algorithm
1. Initialize average to average value, sum to 0
2. For each element of the items array
2.1 Add square of difference of the element and
the average to sum
3. Compute stddev using formula
4. Display stddev in the stddev textbox
Lecture 17
ENGR/CS 101 Computer Science Session
10
stddevBtn_Click code
// declare vars and initialize
double sum = 0;
double average = compute_average();
double stddev;
// for each element of items array
for (int i = 0; i < num_items; i++)
{ // add square of difference of element and average
sum = sum + Math.Pow(items[i]-average, 2);
}
// compute stddev, important to use 1.0
average = Math.Sqrt(1.0/num_items * sum);
// display the result
stddevBox.Text = stddev.ToString();
Lecture 17
ENGR/CS 101 Computer Science Session
11
Submitting the Project



In a web browser, go to
submission.evansville.edu
Reminder: Your login name is your ACENET
username (unless you also are in CS 210,
then your login name has "-101" added to it).
If you didn't change it, your password is your
student ID without the leading 0.
Pairs should submit under only one account.
Make sure both names are in the code file.
Lecture 17
ENGR/CS 101 Computer Science Session
12
Submitting the Project




Click on the Submit Solution link for the
Statistics project. Click on the Browse
button.
Browse to your project folder. (Do not click
into the project folder.)
Right-click on the project folder, select Send
to -> Compressed (zipped) folder
Double-click on the ZIP folder. Click on the
submit button.
Lecture 17
ENGR/CS 101 Computer Science Session
13
Related documents
Download