+ All Categories
Home > Documents > Basic Programming II. Outline Standard Deviation Function (SD) Complex Guessing Game.

Basic Programming II. Outline Standard Deviation Function (SD) Complex Guessing Game.

Date post: 17-Dec-2015
Category:
Upload: anthony-evans
View: 226 times
Download: 0 times
Share this document with a friend
18
Basic Programming II
Transcript

Basic Programming II

OutlineStandard Deviation Function (SD)Complex Guessing Game

Standard Deviation (SD)For the group of data, the standard deviation measures

the spread of the data.Given a set of data the standard deviation (SD) is calculated as

12

0( )

where is the data, a is the average of the data,

is the number of data

n

ii

i

a a

SDn

a

n

0 1 1, ,..., na a a

Standard Deviation (SD)the procedure to compute the SD

1. Obtain the data,

2. Compute the average,

2. Compute the sum ,

3. Compute the SD,

0 1 1... na a aa

n

1

2

0( )

n

ii

a a

12

0( )

n

iiSD

n

a a

0 1 1, ,..., na a a

Standard Deviation (SD): Accepting Data

Task1: Obtain the set of data Store 10 scores from the user in variable a[0],

a[1], ...,a[9]

#include <stdio.h>void main() { int a[10],n; for ( n=0 ; n<10 ; n++ ) { printf("Enter your data:"); scanf("%d", &a[n]); }}

0 1 1, ,..., na a a

Task 2 Compute the average,

Question1. What happens when we do not define csum=0 ?

Standard Deviation (SD)

#include <stdio.h>void main() { int a[10], n; float csum = 0, avg; for ( n=0 ; n<10 ; n++ ) { printf("Enter your data:"); scanf("%d", &a[n]); csum = csum + a[n]; } avg=csum/10.;}

0 1 1... na a aa

n

Task 3. Compute the sum of the square of the difference between the data and the average.

This can be done using pow function

pow((a[0]-avg),2)

Standard Deviation (SD)

12

0( )

n

ii

a a

Standard Deviation (SD)Task3. Continued

Store the sum in the variable, sq_sum

2)( xx

...#include <math.h>... float sq_sum=0;... for ( n=0 ; n<10 ; n++ ) { sq_sum = sq_sum + pow((a[n]-avg),2); }

Standard Deviation (SD)Task 4: Divide it by 10 and the take the square

root to complete the SD.

...#include <math.h> ... float sq_sum = 0, sd; ... for ( n=0 ; n<10 ; n++ ) { sq_sum = sq_sum + pow((a[n]-avg),2); } sd=sqrt(sq_sum/10);

12

0( )

n

iiSD

n

a a

Standard Deviation (SD)SD

#include <stdio.h>#include <math.h>

void main() { float csum=0, avg, sq_sum=0, sd; int a[10], n; //get data and compute avg for ( n=0 ; n<10 ; n++ ) { printf("Enter your data:"); scanf("%d", &a[n]); csum = csum + a[n]; } avg=csum/10; //compute SD for ( n=0 ; n<10 ; n++ ) { sq_sum = sq_sum + pow((a[n]-avg),2); } sd=sqrt(sq_sum/10); printf(“SD = %.2f\n”, sd);}

Grade Assigning using SD

Once the SD has been computed, it can be used to assign the grade for each of the score.

Guessing Game (Again)Previously on

the Guessing game.

#include <stdio.h>#include <stdlib.h>#include <time.h>

void main() { int n, g, i = 1; srand(time(NULL)); n = rand() % 11; // generate a random number 0-10 do { printf("Guess a number[0,10]:"); scanf("%d", &g); i++; } while ((g!=n)&&(i<=5)); if (g == n) printf(“You got it.\n”); else printf("You failed. Please try again.\n");}

Guessing Game (Again)Improve the guessing game so that it tells

if the right answer is greater than the guess or less than the guess.

Guess a number[0,10]:2Greater than this.Guess a number[0,10]:6Less than this.Guess a number[0,10]:5You got it.

The random number (answer) is stored in variable n n

The guess is stored in variable g g

Guessing Game (Again)

Guessing Game (Again)Flowchart showing the relationship between

the guess and the number.{g==n You got it, g>n less than this, g < n

greater than this} g==n g>n

display ‘Less than this.’ display ‘Greater than this.’display ‘You got it.’

NN

Y Y

Guessing Game (Again)According to the flowchart, the translation

into code is if (g==n) { printf("You got it.\n");}else if (g>n) { printf("Less than this.\n");}else { printf("Greater than this.\n");}

#include <stdio.h>#include <stdlib.h>#include <time.h>

void main() { int n, g, i = 1; srand(time(NULL)); n = rand() % 11; // generate a random number 0-10 do { printf("Guess a number[0,10]:"); scanf("%d", &g); if (g==n) { printf("You got it.\n"); } else if (g>n) { printf("Less than this.\n"); } else { printf("Greater than this.\n"); } i++; } while ((g!=n)&&(i<=5)); if (g != n) printf("You failed. Please try again.\n");}

SummaryUsing flowchart can help us on planning

the program;When you cannot see the solution right

away, break the problem into smaller pieces and solve one at a time.


Recommended