Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Basic Programming II. Outline Standard Deviation Function (SD) Complex Guessing Game."— Presentation transcript:

1 Basic Programming II

2 Outline Standard Deviation Function (SD) Complex Guessing Game

3 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

4 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,

5 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 void main() { int a[10],n; for ( n=0 ; n<10 ; n++ ) { printf("Enter your data:"); scanf("%d", &a[n]); }

6 Task 2 Compute the average, Question 1. What happens when we do not define csum=0 ? Standard Deviation (SD) #include 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.; }

7 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)

8 Task3. Continued Store the sum in the variable, sq_sum... #include... float sq_sum=0;... for ( n=0 ; n<10 ; n++ ) { sq_sum = sq_sum + pow((a[n]-avg),2); }

9 Standard Deviation (SD) Task 4: Divide it by 10 and the take the square root to complete the SD.... #include... 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);

10 Standard Deviation (SD) SD #include 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); }

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

12 Guessing Game (Again) Previously on the Guessing game. #include 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"); }

13 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]:2 Greater than this. Guess a number[0,10]:6 Less than this. Guess a number[0,10]:5 You got it.

14 The random number (answer) is stored in variable n n The guess is stored in variable g g Guessing Game (Again)

15 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==ng>n display ‘Less than this.’display ‘Greater than this.’ display ‘You got it.’ NN YY

16 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"); }

17 #include 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"); }

18 Summary Using 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.


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

Similar presentations


Ads by Google