259201 Computer Programming for Engineers

18
Basic Programming II

description

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 - PowerPoint PPT Presentation

Transcript of 259201 Computer Programming for Engineers

Page 1: 259201 Computer Programming for Engineers

Basic Programming II

Page 2: 259201 Computer Programming for Engineers

OutlineStandard Deviation Function (SD)Complex Guessing Game

Page 3: 259201 Computer Programming for Engineers

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

Page 4: 259201 Computer Programming for Engineers

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

Page 5: 259201 Computer Programming for Engineers

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

Page 6: 259201 Computer Programming for Engineers

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

Page 7: 259201 Computer Programming for Engineers

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

Page 8: 259201 Computer Programming for Engineers

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

Page 9: 259201 Computer Programming for Engineers

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

Page 10: 259201 Computer Programming for Engineers

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

Page 11: 259201 Computer Programming for Engineers

Grade Assigning using SD

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

Page 12: 259201 Computer Programming for Engineers

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

Page 13: 259201 Computer Programming for Engineers

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.

Page 14: 259201 Computer Programming for Engineers

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

The guess is stored in variable g g

Guessing Game (Again)

Page 15: 259201 Computer Programming for Engineers

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

Page 16: 259201 Computer Programming for Engineers

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

Page 17: 259201 Computer Programming for Engineers

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

Page 18: 259201 Computer Programming for Engineers

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.