Title: Midterm Review
1Midterm Review
Feb. 26, 2008
2Test Logistics
- Take-home part of the exam will be available at
roughly 530pm, Tuesday, Feb. 26 - In-class exam will be during Thursday's lecture
time - Both parts of the exam will be due at 515pm on
Thursday (on Oncourse)? - Finish take-home part before the in-class part
starts! - Submit both parts of the test together
3Test Logistics
- If you have a laptop, then come here for the
in-class exam - Charge your battery beforehand!
- Bring a power strip!
- If you don't have a laptop, then go to room 221
in the Student Building (SB)?
4Test Rules
- All notes, books, and previous code are allowed
to be used on both parts of the exam - You may not discuss any of the exam problems with
anyone else - You may not copy source code from the Internet
5Preparation Tips
61. Finish ALL Labs
- Make corrections on graded labs
- Learn from your previous mistakes...don't repeat
them - Go back and do lab programs you didn't do
- You must know how to complete the labs
- Nothing else will have as large an effect on your
exam grade
72. Re-Do Labs from Scratch
- Don't look at any of your old code
- Don't look at any sample source code
- Don't even look at the text book
- If you can work out all labs from scratch
- You will almost surely get an A
83. Reading vs. Coding
- Reading is great for understanding theory and
concepts - The exam tests your ability to program
- My advice
- Spend your study time programming
- Reading is great if you are confident that you
can program all the labs and homeworks from
scratch
94. Have Examples Ready
- There are a lot of tricky things to learn about
programming - strings
- loops
- if-else, switch
- Have a simplified set of examples that show how
to do each of these tasks
105. Organize Your Old Code
- The exam is going to be challenging and it's
going to take time - Many students work until the last minute
- Don't waste time looking for old source code
files, sample code, examples, notes - Test out code from lecture slides and see what it
does and how it works - Change it around and see what happens
116. Concentrate on the Harder Topics
- Nearly every lab and homework built on those that
came before - The exam is absolutely cumulative!
- If you're re-working the labs
- Start at the last lab and work backwards
- If you can do strings, loops, and if-else/switch
logic, then you won't have any problem with
printf and scanf
127. Do the Easier Program First
- Quickly read both problems on the test
- Identify which you think will be easier or faster
- Finish that one completely first!
- Don't lose points for stuff you know because you
ran out of time
138. Don't Give Up!
- Absolutely do not leave any problems blank!
- You'll get a 0 for anything that is blank or not
submitted - Don't randomly copy code just so it isn't blank
- If you're really stumped, write the solution out
in English or pseudocode - Write out any code that you can (printf, scanf,
variables you know you will need, etc.)?
149. Ask for Clarification
- If you're not sure about a program requirement,
then ask for clarification - We can help you better understand what the
problem is, but we can't really help you on the
solution (in a general or specific way)?
1510. Start Take-Home Early
- Even if you think the take-home programs won't
take you very long, start them early anyway - Start them tonight! Think how happy you would be
if you were all done by tomorrow morning! - The amount of time it takes to write a program
correctly is very difficult to predict
1611. Sample Source Code
- Passwords
- spider
- zebra
- turtle
- elephant
- porcupine
- gorilla
- giraffe
- panther
17Topics
18printf, scanf
- Know when to use d, 5d, f, g, .2f, 5.3f,
etc. - Know when to use an '' in a scanf statement
- scanf( s, myString )
- What kind of string will this NOT get?
19scanf Return Value
int age 0 int numberScanned 0 printf(
Please enter your age ) numberScanned
scanf( d, age ) if( numberScanned 1
)? printf( Age d successfully scanned.\n,
age ) else printf( Illegal input\n
)
20scanf Return Value
numberScanned will be 1 if the scanf succeeded
and got an integer value for age. Otherwise,
numberScanned will be 0.
int age 0 int numberScanned 0 printf(
Please enter your age ) numberScanned
scanf( d, age ) if( numberScanned 1
)? printf( Age d successfully scanned.\n,
age ) else printf( Illegal input\n
)
21Using include and define
- include other libraries of functions
- stdio.h
- stlib.h
- ctype.h
- string.h
- math.h
- define constants in your program
- define MaxStringLength 64
22Simple Variables
- int, float, char, etc.
- Assignment operator
- x 15
- x y 10
- x z
- Using integers as counters in loops
- Built-in in for loops
- Manually added for while/do-while loops
23Type Casting
- int / int has a problem with loss of precision
(we lose the decimal part)? - 2 / 5 yields the value of 0
- We cast an int into a float to maintain full
accuracy - (float) 2 / 5 yields the value of .4
24Return Values
- A program ending normally with no errors returns
0 - return( 0 )
- A program that encounters an error (e.g. bad user
input) returns a non-zero error code (using the
value of 1 is fine)? - return( 1 )
25strings
- Declaring a new string variable
- char nameMaxStringLength
- char name Tracy
- Scanning and printing a string
- scanf vs. sgets
- Printing a single character
- printf( The third char is c\n, name2 )
26strings
- Changing the value of a character in a string
- name0 'X'
- name5 '\0'
- String functions
- strlen, strncpy, strlcat
27strings
- Looping over characters in a string
- for( i 0 i lt strlen( name ) i )?
-
- if( namei 'e' )?
-
- printf( Found an e!\n )
-
28Escape Characters
- How do you print out newlines and tabs?
- printf( Game\t0ver\n )
- How do you print out double quotes?
- printf( An important word, \rosebud\\n )
29Mathematical Operators
- Basic operators
- , -, , /
- Update operators
- , --, , -, , /
- Difference between and
30Math
- Functions in math.h
- pow, log, floor, ceil, sqrt, sin, cos, etc.
- Remainder operator,
- 5 2 gives you the remainder when you divide 5
by 2 (answer 1)? - 8 3 gives you the remainder when you divide 8
by 3 (answer 2)?
31if-else/switch Statements
- When to use if-else vs. switch
- Comparison operators
- , !, gt, lt, gt, lt
- Combining conditions
- ,
32switch Statements
- switch statements replace long if-else chains
- The value that is checked can be a variable or an
expression - switch( myLetter )?
- switch( x 2 )?
- Don't forget the required break statements
33switch Example
char initial ' ' printf( Please enter your
first initial ) scanf( c, initial
) switch( initial )? case 'm' case
'M' printf( We have the same first
initial!\n ) break default printf(
We don't share the same initial\n) break
34switch Example 2
int age 0 printf( Please enter your age
) scanf( d, age ) switch( age 5
)? case 0 printf( Your age is a
multiple of 5!\n ) break default print
f( Your age is NOT a multiple of
5\n) break
35bool Variables
- Boolean variables are declared using the keyword
bool - Can hold either value true or the value false,
nothing more - include stdbool.h
36Loops
- Since loops are vital in programming, loops will
be on the midterm extensively - 3 kinds of loops for, while, do-while
- Know when to use each kind
- Know how to convert between the different kinds
of loops
37Loops to Know
- Looping through a string, char by char
- Using a for loop is easiest
- Using a do-while loop to keep prompting the user
for input until correct input is entered - for loops forwards and backwards
- counting variable initialization
- i 0
- i some other value (e.g. 1)?
38Questions
- Any questions?
- Any examples you want to see?