SlideServe
Menu
Préférences
© SlideServe · v2026

Midterm 2 Review

natalie-brown avatar
18 diapositives · 430 Views · Nov 05, 2014

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

Midterm 2 Review - slide 1
01 / 18Midterm 2 Review
1 / 17

Télécharger la présentation

Midterm 2 Review
Une image / un lien ci-dessous est fourni (tel quel) pour télécharger la présentation

Politique de téléchargement : le contenu du site vous est fourni TEL QUEL pour votre information et votre usage personnel ; il ne peut être vendu, concédé sous licence ni partagé sur d’autres sites sans l’accord de son auteur.Le contenu vous est fourni TEL QUEL, pour votre information et votre usage personnel uniquement. Téléchargez la présentation en cliquant sur ce lien. Lors du téléchargement, si vous ne parvenez pas à télécharger une présentation, l’éditeur a peut-être supprimé le fichier de son serveur.Pendant le téléchargement, si vous ne parvenez pas à obtenir une présentation, le fichier a peut-être été supprimé par l’éditeur.

F I N

Plus de contenu associé

Transcription de la présentation
  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