Top Banner
LECTURE 41: COURSE REVIEW CSC 107 – Programming For Science
36

CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Mar 31, 2015

Download

Documents

Leticia Wick
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

LECTURE 41:COURSE REVIEW

CSC 107 – Programming For Science

Page 2: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Final Exam

Thurs., Dec. 10th from 8AM – 10AM in OM 221

For exam, plan on using full 2 hours If major problem, come talk to me ASAP

Exam covers material from entire semester Open-book & open-note so bring what

you’ve got My handouts, solutions, & computers are

not allowed Cannot collaborate with a neighbor on the

exam Problems will be in a similar style to 2

midterms

Page 3: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Positional Notation

To convert dn...d3d2d1d0 into decimal:

From base-b

d0 * b0

d1 * b1

d2 * b2

d3 * b3

+ dn * bn

Page 4: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Converting Decimal To Base-b General way to convert from decimal

to base-b:While decimal number ≠ 0

Divide decimal number by bMove remainder to left end of

answerReplace decimal number with

quotient

Page 5: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

NOT Gate

Simplest gate: computes opposite of input Output false when input true; Output true when input false;

Written in C++ as !a a is gate’s input !a is functions outputa !a

true

false

a !a

Page 6: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

OR Gate

Equivalent to addition in Boolean algebra If either input is true is going to be checked True when either a OR b are true; false

otherwise Written in C++ as a || b

a & b are inputs; a || b is outputa b a || b

false falsefalse truetrue falsetrue true

a

ba || b

Page 7: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

AND Gate

Equivalent to multiplication in Boolean algebra If both inputs are true is going to be

checked True when a AND b are true; false otherwise

Written in C++ as a && b a & b are inputs; a && b is output

a b a && b

false falsefalse truetrue falsetrue true

a

ba && b

Page 8: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Monkeys!

Page 9: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Programming Using cin

Used to read one or more values at once:

cin >> variable;cin >> variable1 >> variable2;

Reads where last cin stopped reading input Automatically skips past whitespace

Data type of variable determines what is read Stops reading at first non-usable value in

input If input is not usable, will set variable equal

to 0

Page 10: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Using cout to Print

Already seen how to print text using cout

cout << “Hello World” << endl; Prints out whatever is placed between

quotes endl goes to next line and prints out

immediately

Can format output in variety of ways Print numbers to preset level of precision Use fixed format versus which ever makes

sense

Page 11: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Priority of Operations

Equations can become very complex 4 + 5 * 6 * 9 - 2 + 1 = …?

Very strict order of operations used by computer ( ) Solve from inner- to

outermost + (positive) & - (negative) Solve from right to left * & % & / (division) Solve from left to right + (addition) & - (subtraction) Solve from left to

right

My suggestion: use lots of parentheses

Page 12: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Compound Assignment Operators Short simple operators that allow us to

be lazy Save some typing for several common

actions Lowest priority operation; expression

evaluated first

Operator Equivalent C++ Expression

a += 2; a = a + 2;b -= d; b = b – d;c *= 4 + 5.6; c = c * (4 + 5.6);d /= 0.3 * e; d = d / (0.3 * e);

Page 13: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Mathematical Functions

Add #include <cmath> at top of file All these functions return a value

Will NOT change argument’s valuesin(x), cos(x), tan(x), asin(x), atan(x) ,log10(x), sqrt(x), log(x), exp(x) ,pow(x,y) , floor(x) , ceil(x)

Page 14: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Relational Operators

< (less than) > (greater than) <= (less than of equal to) >= (greater than of equal to) != (inequality ≠) == (equality – if two things have same

value) NOT the same as assignment (=)

Page 15: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

if (…) statement

First evaluates expression in parenthesis

Add opening brace ({) after closing parenthesis Can now write all statements to execute

Add closing brace (}) to show where if ends If expression false, execution restarts at

that point If expression is true, executes code in

block Skips over block, when expression is false

Page 16: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

if – else if – else Usage

Must begin with if statement at the start This is required; what would we be saying else to?

Only required part of this entire process Can then have zero or more else ifs

Tests can be anything; do not have to be related

Until one is true, will be examined one-by-one

Execute 1st clause where true expression is found

Only at the very end can have else clause If nothing else matches then else is

executed

Page 17: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Executing switch Statement

1. Evaluates expression2. Finds matching case or default (if it

exists) If no default, may not have match - skips switch

3. Execution starts at 1st matching label Execution will continue until break; found Will continue into next case if break; is not

hit

4. Restarts running after switch once break hit

May reach end of switch without a break Continues running code after switch

Page 18: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

while Loop

while (expression) { statement; ...}

Evaluates expression to find its value If true, executes entire loop body

Re-check after each pass through loop, only Continue with loop while expression

checks true

Page 19: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

for Loop

for (initialization; expression; update) { statement; ...}

initialization run once before loop start

Loop termination condition is expression Boolean expression checked at start of

each pass Re-executes entire loop body if expression is

true Once expression found to be false, loop is

over

Page 20: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Function Definition

List prototypes for functions near top of file Similar, but prototypes end with semi-colon

Return type, name, & parameters listed at start After these are listed, need an open brace Function goes until closing bracefloat squareNumber(float x){…}double computeAvg(int x, int y){…}int promptAndReadInput(){…}void noReturnAndNoParams(){…}

Page 21: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Function ends when return is executed Any and all code after return will be

ignored Calling function will resume its execution There are no errors or warnings for this

common bug

return Statement

Page 22: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

More Monkeys

Page 23: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Declaring Arrays

With the array, have entries of given type Each entry like variable & can be used as

such Each entry is independent of all other

entries Type, name, & size needed for array

declarationint planetsWeight[NOT_PLUTO];float armada[MAX_SHIPS];double annualIncomes[MAX_LIFETIME];char responses[17];long timeToWait[35];

Page 24: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

String Theory!

cString type does not exist Just an array of char made to look

fancy One slight wrinkle: cString ends with null

character Since C++ does not know array’s size, but

we need it Null character is char with value of 0

Written as ‘\0’ if used in a program Unprintable, only used to mark end of

cString

Page 25: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Program Basics For Files

All built-in file I/O code means adding to header

#include <fstream> Place with other #includes to use files in

program Even if no files are used, no cost to adding

this line Must specify namespace of file I/O

code, also If you really want, this can be done

individual but…

using namespace std; much easier and probably habit by now

anyway

Page 26: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Opening a File

Within program, may use file in 2 possible ways To read file, ifstream variables will be

needed Need variable of type ofstream to write to

file Open ofstream 2 different ways depending

on use

ofstream nukeIt("byebye.txt");ofstream begone;begone.open("erasedOld.dat");

ofstream keepIt("saved", ios::app);ofstream faithAlone;faithAlone.open("preserve", ios::app);

3

Page 27: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Read File W/ifstream Variable Used to read one or more values at

once:ifstream myFile;myFile >> variable;

myFile >> variable1 >> variable2;

Starts where last read stopped reading input

Automatically skips past whitespace Data type of variable determines what

is read Stops at first non-usable value found in the

input If input is not usable, will set variable equal

to 0

Page 28: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Print to File With ostream

Easy to output: output via ostream variable

ofstream outFile;outFile << “Hello World” << endl; Prints out whatever is placed between

quotes Value of variable printed if variable not in

quotes

Page 29: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Declaring an Pointer

Must declare pointer before use This should not be surprise, just like any

variable Type & name required (as with all

declarations) As with any other variable, typical name

rules apply Include asterisk before name within

declaration Variable is now a pointer to requested

type Initial value is unknownint * jennifer;char *pointerToChar;double* down;

Page 30: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

& and * Operators

& operator gets the address of a variable Used only with variables, including array

elements Types must match, no automatic promotion

possible Pointers to pointers okay, but needs to be

type** Follow the pointer to get value at

location with * Only used with pointers, as * could also be

multiply

double x, *y = &x;int *a, *b = &a;float *c = a, d = *a;

Page 31: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Pointers versus Arrays

Both types of variables store an address Can be assigned to one another if types

match To access value, can either use * or [index] *p same as p[0] - they are "synonyms" in

C++ Arithmetic works similarly - *(p+5) same as p[5]

Do not get carried away exploiting this idea Unlike arrays, memory not reserved for

pointer Arrays not used to alias other variables

(usually)

Page 32: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Using structs

Can assign struct variables to one another Variables must be of identical struct types Copies value of all fields but still remain

independent Locations will be same, since pointers &

arrays aliased In all other situation, must use fields

Cannot add, multiply, compare struct variables

For any legal use, individual fields always available

Arrays of structs can also be declared Within the array, each entry is struct

variable

Page 33: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

And Finally…

Page 34: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

And Finally Monkees

Page 35: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

“Subtle” Hint

Do NOT bother with

memorizationBe ready to lookup &use information quickly

Page 36: CSC 107 – Programming For Science. Final Exam Thurs., Dec. 10 th from 8AM – 10AM in OM 221 For exam, plan on using full 2 hours If major problem, come.

Final Exam Schedule

Lab Mastery Exam is:Tues., Dec. 8th from 9AM – 10AM in OM 115

Final Exam is: Thurs., Dec. 10th from 8AM – 10AM in OM 221