Download presentation
Presentation is loading. Please wait.
1
3. Control structures and Data Files Structured Programming –Sequence structures –Selection structures –Repetition structures Pseudocode/Flowchart Data Files Linear Regression
2
3.1 Algorithm Development Decomposition outline 1. Read the new time value 2. Compute the corresponding velocity and acceleration values 3. Print the new velocity and acceleration
3
Top-Down Design –Divide and Conquer –Stepwise refinement –Pseudocode/Flowchart –Evaluation of alternative solutions –Error conditions –Test data generation –Program workthrough
4
Flowchart Pseudocode Main:read a,f_a; read c,f_c; read b; set f_b to f_a + (f_c-f_a) print f_b Sequence Structure b - a c - a Read a,f_a f_b=f_a + (f_c - f_a ) print f_b start main stop main Read c,f_c b - a c - a Read b
5
Is |denominator| < 0.0001 ? print fraction print “Denominator close to zero noyes Selection Structure If |denominator| < 0.0001 print “Denominator close to zero” else set fraction to numerator/denominator print fraction Flowchart Pseudocode
6
time = 0 Is time ≤ 10 ? Compute velocity print velocity Increment time by 1 yes no Repetition Structure Flowchart Pseudocode set time to 0 while time ≤ 10 compute velocity print velocity increment time by 1
7
Relational operators 3.2 Condition Expressions
8
Conditions a < b x + y >= 10.5 fabs(denominator) < 0.001 d =b > c; If (d) cnt ++
9
Logical operators and && or|| not!
10
b = 3; c = 5; ! ( b == c || b == 5.5 ) b = 3; c = !b; printf( “ b=%i c=%i “, b, c);
12
if statement if/else statement 3.3 Selection Statement If (a<50) { ++ count; sum += a; } If (a<50) { ++ count; sum += a; if (b>a) b = 0; } If ( fabs(denominator) < 0.0001 ) printf(“Denominator close to zero”); else { x = numerator / denominator; printf(“x= %f \n”,x); }
13
Indentation if ( x > y ) if ( y < z ) k ++; else m ++; else j++; if ( x > y ) if ( y < z ) k ++; else m ++; if ( x > y ) if ( y < z ) k ++; else j++; ?
14
if ( a < b ) ; else count++; if ( a >= b ) count++; else ; float a; if ( a == 10.5) ( fabs( a-10.5 ) < 10e-5) printf (“ a = %f”,a);
15
Conditional Operator If ( a < b ) count ++; else c = a + b; a < b ? count ++ : (c= a + b) ; c = a < b ? a : b ; c = a < b ? count++ : (c = a + b); a = 3.0 ; b = 4.0 ; count = 0 ; c count
16
Switch statement If ( code == 10 ) printf(“Too hot - turn equipment off \n”); else { If ( code == 11 ) printf(“Caution - recheck in 5 minutes \n”); else { If ( code == 13 ) printf(“Turn on circulation fan \n”); else printf(“Normal mode of operation \n”); }
17
Switch statement(cont.) switch( code ) { case 10 : printf(“Too hot - turn equipment off \n”); break; case 11 : printf(“Caution - recheck in 5 minutes \n”); break; case 13 : printf(“Turn on circulation fan \n”); break; default : printf(“Normal mode of operation \n”); break; }
18
3.4 Loop Structure while ( condition ) { statements; } do { statements; } while ( condition ); For(exp_1 ; exp_2 ; exp_3) { statements; }
20
Degree to Radians 00.000000 100.174533 200.349066 : :
25
for ( k =1, j=5 ; k<=10 ; k++, j++) { sum_1 += k ; sum_2 += j ; }
26
break and continue statement Sum = 0 for ( k=1 ; k <=20 ; k ++ ) { scanf ( “ %lf”, &x ) ; if ( x > 10.0 ) break; sum + = x; } printf ( “sum = &f \n “, sum); continue ;
27
3.5 Problem Solving Applied: Weather Balloons
28
1. PROBLEM STATEMENT Using the polynomials that represent the altitude and velocity for a weather balloons, print a table using units of meters and meters/second. Also find the maximum altitude (or height) and its corresponding time. Assume that the following polynomial represents the altitude or height in meters during the first 48 hours following the launch of a weather balloon: alt(t) = -0.12t 4 + 12t 3 -380t 2 + 4100t + 220 –where the units of t are hours. The corresponding polynomial model for the velocity in meters per hours of the weather balloon is as follows: v(t) = -0.48t 3 + 36t 2 - 760t + 4100
29
2. INPUT/OUTPUT DESCRIPTION INPUT : Starting time Time increment Ending time OUTPUT:The table of altitude and velocity values The Maximum altitude and its corresponding time
30
3. HAND EXAMPLE Assume that the starting time is 0 hours, the time increment is 1 hour, and the ending time is 5 hours. To obtain the correct units, we need to divide the velocity value in meters/hour by 3600 in order to get meters/second. Using our calculator, we can then compute the following values: We can also determine the maximum altitude for this table, which is 12,645.00 meters, it occurred at 5 hours TimeAltitude(m)Velocity(m/s) 0220.001.14 13,951.880.94 26,994.080.76 39,414.280.59 411,277.280.45 512,645.000.32
31
4. ALGORITHM DEVELOPMENT First step : Decomposition outline –breaks the solution into a series of sequential steps Decomposition Outline 1.Get user input to specify times for the table. 2.Generate and print conversion table and find maximum height and corresponding time. 3.Print maximum height and corresponding time.
32
4. ALGORITHM DEVELOPMENT(Cont.) Second step : Refinement in Pseudocode main:read initial, increment, final values from keyboard set max_height to zero set max_time to zero print table heading set time to initial while time<=final compute height and velocity print height and velocity if height>max_height set max_height to height set max_time to time add increment to time print max_time and max_height
33
Coding /*---------------------------------------------------*/ /* Program chapter3_4 */ /* */ /* This program prints a table of height and */ /* velocity values for a weather balloon. */ #include main(){ /* Declare and initialize variables. */ double initial, increment, final, time, height, velocity, max_time=0, max_height=0; /* Get user input. */ printf("Enter initial value for table (in hours) \n"); scanf("%lf",&initial); printf("Enter increment between lines (in hours) \n"); scanf("%lf",&increment); printf("Enter final value for table (in hours) \n"); scanf("%lf",&final); /* Print report heading. */ printf("\n\nWeather Balloon Information \n"); printf("Time Height Velocity \n"); printf("(hrs) (meters) (meters/s) \n");
34
Coding(Cont..) /* Compute and print report information. */ for (time=initial; time<=final; time+=increment) { height = -0.12*pow(time,4) + 12*pow(time,3) - 380*time*time +4100*time + 220; velocity = -0.48*pow(time,3) + 36*time*time - 760*time + 4100; printf("%6.2f %8.2f %7.2f \n", time,height,velocity/3600); if (height > max_height) { max_height = height; max_time = time; } /* Print maximum height and corresponding time. */ printf("\nMaximum balloon height was %8.2f meters \n", max_height); printf("and it occurred at %6.2f hours \n",max_time); /* Exit program. */ return EXIT_SUCCESS; }
35
5. TESTING If we use the data from the hand example, we have the following interaction with the program: Enter initial value for table (in hours) 0 Enter increment between lines (in hours) 1 Enter final value for table (in hours) 5 Weather Balloon Information Time Height Velocity (hrs) (meters) (meters/s) 0.00 220.00 1.14 1.00 3951.88 0.94 2.00 6994.08 0.76 3.00 9414.28 0.59 4.00 11277.28 0.45 5.00 12645.00 0.32 Maximum balloon height was 12645.00 meters and it occurred at 5.00 hours.
36
altitude and velocity of the balloon for a period of 48 hours Balloon Altitude Balloon Velocity
37
3.6 Data Files FILE *sensor1; sensor1 = fopen ( “ sensor1.dat”, “r”); fscanf ( sensor1, “ %lf %lf”, &t, &motion); FILE *balloon; balloon = fopen ( “ balloon.dat”, “w”); fprintf (balloon, “%f %f %f \n”, time, height, velocity); fclose (sensor1); fclose (balloon); #define FILENAME“sensor1.dat” ……. sensor1 = fopen (FILENAME, “r”);
38
Reading Data Files file name data types order of data the size of data number of records trailer signal(sentinel signal) end of file indicator
39
Input file : sensor1.dat 10 0.0132.5 0.1147.2 0.2148.3 0.3157.3 0.4163.2 0.5158.2 0.6169.3 0.7148.2 0.8137.6 0.9135.9 Output Number of sensor readings:10 Average reading: 149.77 Maximum reading:169.30 Minimum reading:132.50
42
Input file : sensor2.dat 0.0132.5 0.1147.2 0.2148.3 0.3157.3 0.4163.2 0.5158.2 0.6169.3 0.7148.2 0.8137.6 0.9135.9-99
45
Input file : sensor3.dat 0.0132.5 0.1147.2 0.2148.3 0.3157.3 0.4163.2 0.5158.2 0.6169.3 0.7148.2 0.8137.6 0.9135.9
48
Generating Data File
51
3.7 Numerical Technique: Linear Modeling Linear Representation Time(s)Temperature( ˚ F)0 120 260 368 477 5110 y = m x + b
53
Equations for the “best” linear fit in terms of least squares
54
Equations for the “best” linear fit in terms of least squares
55
3.8 Problem Solving Applied: Ozone Measurements
56
Back Ground Satellite sensors can be used to measure many different pieces of information that help us understand more about the atmosphere, which is composed of a number of layers around the earth. Starting at the earth's surface, the layers are the troposphere, stratosphere, mesosphere, thermosphere and exosphere. Consider a problem in which we have collected set of data measuring the ozone mixing ratio in parts per million volume (ppmv). Over small regions, these data are nearly linear, and thus we can use a linear model to estimate the ozone at altitudes other than ones for which we have specific data.
57
Atmospheric layers around the earth ( 대류권 ) ( 성층권 ) ( 중간권 ) ( 온도권 ) ( 외기권 )
58
1. PROBLEM STATEMENT Use the least-squares technique to determine a linear model for estimating the ozone mixing ratio at a specified altitude.
59
INOUT :Data file zone1.dat OUTPUT:Range of altitudes Linear model for ozone mixing ratio 2. INPUT/OUTPUT DESCRIPTION
60
3. HAND EXAMPLE Assume that the data consist of the following four data points: Using the equations, we can now compute the values of m and b: Altitude(km)Ozone Mixing Ratio (ppmv) 203 244 265 286 m = 0.37 b = -4.6
61
First step : Decomposition outline –breaks the solution into a series of sequential steps Decomposition Outline 1.Read data file values and compute corresponding sums and ranges. 2.Compute slope and y intercept. 3.Print range of altitude and linear model. 4. ALGORITHM DEVELOPMENT
62
Second step : Refinement in Pseudocode main: set count to zero set sumx, sumy, sumxy, sumx2 to zero while not at end-of-file read x,y increment count by 1 if count = 1 set first to x add x to sumx add y to sumy add x*x to sumx2 add x*y to sumxy set last to x compute slope and y intersect print first, last, slope, y intersect 4. ALGORITHM DEVELOPMENT(Cont.)
63
*-----------------------------------------------------------*/ /* Program chapter3_9 */ /* */ /* This program computes a linear model for a set */ /* of altitude and ozone mixing ratio values. */ #include #define FILENAME "zone1.dat" main() { /* Declare and initialize variables. */ int count=0; double x, y, first, last, sumx=0, sumy=0, sumx2=0, sumxy=0, denominator, m, b; FILE *zone1; /* Open input file. */ zone1 = fopen(FILENAME,"r"); /* While not at the end of the file, */ /* read and accumulate information. */ while ((fscanf(zone1,"%lf %lf",&x,&y)) == 2) { ++count; if (count == 1) first = x; sumx += x; sumy += y; sumx2 += x*x; sumxy += x*y; } last = x; Coding
64
Coding(Cont.) /* Compute slope and y-intercept. */ denominator = sumx*sumx - count*sumx2; m = (sumx*sumy - count*sumxy)/denominator; b = (sumx*sumxy - sumx2*sumy)/denominator; /* Print summary information. */ printf("Range of altitudes in km: \n"); printf("%.2f to %.2f \n\n",first,last); printf("Linear model: \n"); printf("ozone-mix-ratio = %.2f altitude + %.2f \n", m,b); /* Close file and exit program. */ fclose(zone1); return EXIT_SUCCESS; } /*---------------------------------------------------*/
65
5. TESTING Using the data from the hand example as the contents of the data file zone1.dat, we get the following program output: Range of altitude in km: 20.00 to 28.00 Linear model: ozone-mix-ratio = 0.37 alitutde + -4.60
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.