1 / 18

Midterm 2 Review

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

Télécharger la présentation

Midterm 2 Review

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. Midterm 2 Review

  2. 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

  3. Loop (repetition) statements • while statement • do while statement • for statement

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

  5. 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);

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

  7. for statement initalize test increment/ decrement true statement(s) statement(s)

  8. 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

  9. 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);

  10. 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

  11. 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

  12. Functions • Defined to • return a single value to the calling function • perform a task • change the value of the function arguments (call by reference)

  13. Function Format return_type function_name (parameter_declarations) { declarations; statements; }

  14. 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

  15. Example - factorial function int main(void) { int t= 5,s; s=fact(t) return 0; } 5 t s

  16. 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

  17. 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

  18. Example - factorial function int main(void) { int t= 5,s; s=fact(t) return 0; } 5 t 120 s

More Related