{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Exam 2 Review

# Exam 2 Review - ENGR 101 Sec,on 100 Fall 10 Review...

This preview shows pages 1–7. Sign up to view the full content.

ENGR 101, Sec±on 100, Fall 10 11/2/10 1 Review: Programming and C++ ENGR 101: 3 Nov 10 Where We’ve Been Since Exam 1 Counting Loops counting patterns, for, nested loops Data Types data types, encoding numbers, integers, chars Names variables, constants, scope, lifetime Classes data abstraction, methods, generic routines and types Strings string class, array semantics Vectors vectors of vectors Recursion and Sorting

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENGR 101, Sec±on 100, Fall 10 11/2/10 2 Simple Counting Loops Many of the loops we have looked at so far are of the form: n = 0; while (n < N) { do something n = n + 1; } Q: What does this do? A: Counts from 0 to N –1 by 1. Q2: How to modify to count from different start, by different increment? A2: simple… for Statement C++ iteration construct for counting loops Takes the form: for ( initialization ; condition ; iteration_expr ) loop_body initialization and iteration_expr are expressions condition is an expression that evaluates to True or False loop_body is a (compound) statement Repeatedly executes the loop body and iteration statement as long as the condition holds.
ENGR 101, Sec±on 100, Fall 10 11/2/10 3 for and while for ( initialization ; condition ; iteration_expr ) loop_body has the same behavior as: initialization ; while ( condition ) { loop_body ; iteration_expr ; } Repeatedly executes the loop body and iteration statement as long as the condition holds. Using for Translate a counting loop: n = 0; while (n < N) { do something n++; } Q: What does this do? A: Counts from 0 to N –1 by 1. for ( n = 0 ; n < N ; n++ ) { do something } Initialization Continuation Counter

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENGR 101, Sec±on 100, Fall 10 11/2/10 4 loop inside a loop (aka nested loop ) prints a cell of the mult. table Multiplication Table Output 1 x 1 = 1 2 x 1 = 2 3 x 1 = 3 1 x 2 = 2 2 x 2 = 4 3 x 2 = 6 1 x 3 = 3 2 x 3 = 6 3 x 3 = 9 int a, b; for (b=1; b<=3; b++) { for (a=1; a<=3; a++) { cout << a << " x " << b << " = " << a * b << " "; } cout << endl; } Primality Testing Predicate bool prime( int n ) { int factor = 2; while ((factor < n) and ( n % factor != 0 )) factor++; return factor >= n; }
ENGR 101, Sec±on 100, Fall 10 11/2/10 5 Primality Testing (Version 2) bool prime( int n ) { int factor = 2; while (factor*factor <= n) { if ( n % factor == 0 ) return false; factor++; } return true; } Finding the Nth Prime Number int NthPrime( int n ) { int p = 1; // current candidate int m = 0; // primes found so far while ( m < n ) { p++; m = m + prime(p); } return p; } Why does this make sense?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
ENGR 101, Sec±on 100, Fall 10 11/2/10 6 Data Types Definition a set of possible values plus , a set of legal operations on those values for example ( int ): Integers within a given range Operations: addition ( + ), subtraction ( - ), etc. Encoding Capacity
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 30

Exam 2 Review - ENGR 101 Sec,on 100 Fall 10 Review...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online