csc 107 programming for science
Skip this Video
Download Presentation
Lecture 28: Course Review

Loading in 2 Seconds...

play fullscreen
1 / 32

Lecture 28: Course Review - PowerPoint PPT Presentation

  • Uploaded on

CSC 107 – Programming For Science. Lecture 28: Course Review. Final Exam. Thurs., Dec. 16 th from 8AM – 10AM in OM 201 For exam, plan on using full 2 hours If major problem , come talk to me ASAP Exam covers material from entire semester

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
Download Presentation

PowerPoint Slideshow about 'Lecture 28: Course Review' - phelan-christensen

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.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
final exam
Final Exam
  • Thurs., Dec. 16thfrom 8AM – 10AM in OM 201
  • 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 midterm
positional notation
Positional Notation
  • To convert dn...d3d2d1d0 intodecimal:
converting decimal to base b
Converting Decimal To Base-b
  • General way to convert from decimal to base-b:Whiledecimal number ≠ 0Divide decimal number by bMoveremainder to left end of answerReplacedecimal number with quotient
programming using cin
Programming Using cin
  • Used to read one or more values at once:

cin >> variable;

cin >> variable1 >> variable2;

    • Reads where last cinstopped 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
using cout to print
Using coutto Print
  • Already seen how to print text using cout

cout << “Hello World” << endl;

    • Prints out whatever is placed between quotes
    • endlgoes 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
priority of operations
Priority of Operations
  • Equations can become very complex
    • 4 + 5 * 6 * 9 - 2 + 1 = …?
  • Verystrict 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
compound assignment operators
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
mathematical functions
Mathematical Functions
  • Add #includeat top of file
  • All these functions return a value
    • Will NOT change argument’s value

sin(x), cos(x), tan(x), asin(x), atan(x) ,

log10(x), sqrt(x), log(x), exp(x) ,

pow(x,y), floor(x) , ceil(x)

relational operators
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 (=)
not gate


NOT Gate
  • Simplest gate: computes opposite of input
    • Output false when input true
    • Output true when input false
  • Written in C++ as !a("bang a")
    • a is gate’s input
    • x is gate’s output
or gate



OR Gate
  • Equivalent to addition in Boolean algebra
    • If either input istrue is going to be checked
    • true when either aORb aretrue; false otherwise
  • Written in C++ asa || b
    • a & b are inputs; x is output
and gate



AND Gate
  • Equivalent to multiplication in Boolean algebra
    • If both inputs are true is going to be checked
    • True when aANDb are true; false otherwise
  • Written in C++ asa && b
    • a & b are inputs; x is output
if statement
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 ifends
    • If expression false, execution restarts at that point
  • If expression is true, executes code in block
    • Skips over block, when expression is false
if else if else usage
if– else if– elseUsage
  • Must begin with ifstatement at the start
    • This is required; what would we be saying elseto?
    • Onlyrequired part of this entire process
  • Can then have zero or more elseifs
    • 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 elseis executed
executing switch statement
Executing switchStatement
  • Evaluates expression
  • Finds matching caseor default(if it exists)
    • If no default, may not have match - skips switch
  • Execution starts at 1stmatching label
    • Execution will continue until break; found
    • Will continue into next case if break; is not hit
  • Restarts running after switchonce breakhit
    • May reach end of switchwithout a break
    • Continues running code after switch
while loop

while (expression) { statement; ...}

  • Evaluates expressionto find its value
  • If true, executes entire loop body
    • Re-check after each pass through loop, only
  • Continue with loop while expression checks true
for loop

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

  • initializationrun 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
function definition
Function Definition
  • List prototypes for functions near top of file
    • Similar, but prototypes end with semi-colon
  • Return type, name, & parameterslisted at start
    • After these are listed, need an open brace
    • Function goes until closing bracefloat squareNumber(float x){…}double computeAvg(int x, int y){…}intpromptAndReadInput(){…}void noReturnAndNoParams(){…}
return statement
  • Function ends when returnis executed
    • Any and all code after returnwill be ignored
    • Calling function will resume its execution
    • There are no errors or warnings for this common bug
declaring arrays
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, & sizeneeded for array declaration

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

string theory
String Theory!
  • cString type does not exist
  • Just an array of charmade 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 charwith value of 0
    • Written as ‘\0’ if used in a program
    • Unprintable, only used to mark end of cString
program basics for files
Program Basics For Files
  • All built-in file I/O code means adding to header


    • 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
opening a file
Opening a File


  • Within program, may use file in 2 possible ways
    • To read file, ifstreamvariables will be needed
    • Need variable of type ofstreamto write to file
    • Open ofstream2 different ways depending on use

ofstreamnukeIt("byebye.txt");ofstreambegone;"erasedOld.dat");ofstreamkeepIt("saved", ios::app);ofstreamfaithAlone;"preserve", ios::app);

read file w ifstream variable
Read File W/ifstreamVariable
  • Used to read one or more values at once:

ifstreammyFile;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
print to file with ostream
Print to File With ostream
  • Easy to output: output via ostreamvariable

ofstreamoutFile;outFile << “Hello World” << endl;

    • Prints out whatever is placed between quotes
    • Value of variable printed if variable not in quotes
declaring an pointer
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 unknown


and operators
& 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;

pointers versus arrays
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)
using struct s
Using structs
  • Can assign structvariablesto 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 structvariables
    • For any legal use, individual fields always available
  • Arrays of structscan also be declared
    • Within the array, each entry is structvariable
subtle hint
“Subtle” Hint

Do NOT bother with


Be ready to lookup &use information quickly

final exam schedule
Final Exam Schedule
  • Lab Mastery Exam is:Tues., Dec. 14thfrom 10:15AM – 11:15AM in OM 115
  • Final Exam is: Thurs., Dec. 16thfrom 8AM – 10AM in OM 201