1 / 18

259201 Computer Programming for Engineers

259201 Computer Programming for Engineers. Basic Programming II. Outline. Standard 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

fineen
Download Presentation

259201 Computer Programming for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 259201Computer Programmingfor Engineers 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 <stdio.h> voidmain() { int a[10],n; for ( n=0 ; n<10 ; n++ ) { printf("Enter your data:"); scanf("%d", &a[n]); } }

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

  7. Standard Deviation (SD) • 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)

  8. Standard Deviation (SD) • Task3. Continued • Store the sum in the variable, sq_sum ... #include <math.h> ... 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 <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);

  10. 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); }

  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) #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"); } • Previously on the Guessing game.

  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. Guessing Game (Again) • The random number (answer) is stored in variable n • n • The guess is stored in variable g • g

  15. g==n g>n display ‘You got it.’ display ‘Less than this.’ display ‘Greater than this.’ 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} N N Y Y

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

  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.

More Related