180 likes | 398 Views
Midterm 2 Review. Midterm 2. Midterm 2 on Wednesday, July 24th You can bring a two-sided 3x5 index card with course material for a cheat sheet This cheat sheet must be hand-written The cheat sheet will be submitted with the midterm
E N D
Midterm 2 • Midterm 2 on Wednesday, July 24th • You can bring a two-sided 3x5 index card with course material for a cheat sheet • This cheat sheet must be hand-written • The cheat sheet will be submitted with the midterm • Midterm 2 includes all material covered up to this point (up to ch4) • Emphasis is on loops and functions
Loop (repetition) statements • while statement • do while statement • for statement
while statement • while(expression) statement; • while(expression) { statement; statement; . } x=1; while (x<5) x = x + 1; x = 1; while (x<5) { x = x+1; printf(“X=“%d”,x); }
do while • do statement; while(expression); • do { statement1; statement2; . } while(expression); • note - the expression is tested after the statement(s) are executed, so statements are executed atleast once. x=1; do x = x + 1; while (x<5); x = 1; do { x = x+1; printf(“X=“%d”,x); } while (x<5);
for statement • for(initialization; test; increment/decrement) statement; • for(initialization; test; increment/decrement) { statement; statement; . } for (x=1; x<5; x++) printf(“x=%d\n”,x); for (x=1; x<5; x++) { printf(“X=“%d\n”,x); printf(“X^2 = %d\n”,x*x); }
for statement initalize test increment/ decrement true statement(s) statement(s)
Data Files Each data file must have a filepointer • file pointer must be defined • FILE *sensor1; • FILE *balloon; • file pointer must be associated with a specific file using the fopen function • sensor1 = fopen(“sensor1.dat”, “r”); • balloon = fopen(“balloon.dat”, “w”); Read information from file Write information to a file
I/O Statements • Input file - use fscanf instead of scanf • fscanf(sensor1, “%1f %lf”, &t, &motion); • Output file - use fprint instead of printf • fprintf(balloon, “%f %f %f\n”, time, height, velocity);
Reading Data Files • counter controlled loop • First line in file contains count • for loop • end of file controlled loop • When file is created EOF is inserted • while loop • feof(fileptr) > 0 when EOF reached
End of file controlled loop #include <stdio.h> int main() { FILE *scorefile; int score; scorefile = fopen("scores.txt","r"); while (feof(scorefile) <= 0) { fscanf(scorefile,"%d",&score); printf("%d\n",score); } fclose(scorefile); return(0); } Available on webpage as chapter3e11.c File 56 78 93 24 85 63 Available on webpage as scores.txt
Functions • Defined to • return a single value to the calling function • perform a task • change the value of the function arguments (call by reference)
Function Format return_type function_name (parameter_declarations) { declarations; statements; }
Example - factorial function n!=n*(n-1)*…*1, 0! = 1 by definition Function name Return Type int fact(int n) { int factres = 1; while(n>1) { factres = factres*n; n--; } return(factres); } Parameter Declarations Declarations Statements
Example - factorial function int main(void) { int t= 5,s; s=fact(t) return 0; } 5 t s
Example - factorial function int fact(int n) { int factres = 1; return(factres); } int main(void) { int t= 5,s; s=fact(t) return 0; } 5 5 n t 1 factres s
Example - factorial function int fact(int n) { int factres = 1; return(factres); } int main(void) { int t= 5,s; s=fact(t) return 0; } 5 5 n t 120 120 factres s
Example - factorial function int main(void) { int t= 5,s; s=fact(t) return 0; } 5 t 120 s