CSC 107 - Programming for Science Lecture 37 : Course Review.

18

Click here to load reader

description

Discover how computers can solve important scientific problems Learn basics of software development Have fun Objectives Met in CSC107 Discover how computers can solve important scientific problems Learn basics of software development Have fun

Transcript of CSC 107 - Programming for Science Lecture 37 : Course Review.

Page 1: CSC 107 - Programming for Science Lecture 37 : Course Review.

CSC 107 -Programming for Science

Lecture 37:Course Review

Page 2: CSC 107 - Programming for Science Lecture 37 : Course Review.

Final Exam

Tuesday, Dec. 12 from 1 - 3 in OM 208 Plan for exam to take full 2 hours Exam covers material from entire semester

Format will be like that of 2 midtermsStill open-book, open-note, closed-computer

Page 3: CSC 107 - Programming for Science Lecture 37 : Course Review.

Discover how computers can solve important scientific problems

Learn basics of software development Have fun

Objectives Met in CSC107

Discover how computers can solve important scientific problems

Learn basics of software development Have fun

Page 4: CSC 107 - Programming for Science Lecture 37 : Course Review.

Parts of a Program

Should include liberal dose of comments2 types to help document what is going on:// This comment continues to the line’s end/* This goes until it is closed */

Also begin with preprocessor directivesDefine what functions program can use#include <stdio.h>#include <stdlib.h>#include <math.h>#include <ctype.h>#include <string.h>

Page 5: CSC 107 - Programming for Science Lecture 37 : Course Review.

Symbolic Constants

Code “pre-processed” before compilationMakes code simpler & easier to read

Begins #define then the name and valueName traditionally in ALL CAPITAL lettersCan be any type and need not be literalMust be contained on own line#define NAME “bob”#define PI 3.141516#define INDIANA_PI (22.0/7.0)

Page 6: CSC 107 - Programming for Science Lecture 37 : Course Review.

Macros

Symbolic constant that takes a parameter#define SQUARED(x) (x * x)#define VELOCITY(init,a,t) (init+(a*t))#define RAND_X(x) (rand() % x)#define RAND_RNG(lo,hi) ((rand()%(hi-lo))+lo)#define MAX(x,y) ((x > y) ? x : y)

Parameter replaced by value passed inNeed to be very careful about this!i = MAX(i, random());

i = ((i > rand() ? i : rand());

Page 7: CSC 107 - Programming for Science Lecture 37 : Course Review.

Pointers

Pointer is another set of typesValue of pointer is another memory locationMake aliases of a variable

Pointers declared with asterisk ‘*’ before variable’s name

Value is a memory locationCan get variable’s memory location using &

Page 8: CSC 107 - Programming for Science Lecture 37 : Course Review.

Creating struct Declare struct outside of any function

Declared after #includes & #defines Each struct must be given a name Can have 1 or more fields

Each field has name & data typeFields can be listed in any orderFields declared as if it were a variableEach struct variable gets own copy of fields

Page 9: CSC 107 - Programming for Science Lecture 37 : Course Review.

Using structs

Each field has its own valueRefer to using variable.fieldFields set & used on their individuallyRequires actual variable to access

Each variable gets copy of every fieldNo relationship between fields in different

variablesNo relationship between different fields in one

variable

Page 10: CSC 107 - Programming for Science Lecture 37 : Course Review.

Data Types

Each variable also has data type Specifies how program treats variable’s value

C includes 8 built-in data types char, short, int, long, long long, float, double, long double

Ranges for each type NOT specified;may change from machine to machine

struct allows programmer-defined data type Can also have pointer or arrays of any data type

Page 11: CSC 107 - Programming for Science Lecture 37 : Course Review.

Variable Declarations

Functions begin with variable declarations Declarations must be at start of function Function use parameters & declared

variables Each declaration includes two parts

1. Type of data that variable contains2. Name of the variable

Page 12: CSC 107 - Programming for Science Lecture 37 : Course Review.

Names in C

Begin with letter or underscore (_)Then combine letters, numbers, & underscore

Names are case-sensitiveMass, mass, & masS treated as different

Function’s variables’ names must be uniqueCannot know which of my 1,000 “bob” variables

to use Cannot use C’s reserved words

(p. 38 should say “int”, not “ints”)

Page 13: CSC 107 - Programming for Science Lecture 37 : Course Review.

Declaring Array Variable

Declare array variable before useDeclaration must also include literal sizeVariable is an array of the requested typeLocations are variables of that typeint sampleArray[10];struct person class[30];

Only creates location between 0 & size-1Cannot find an array’s sizeWill not warn when accessing beyond size

Page 14: CSC 107 - Programming for Science Lecture 37 : Course Review.

Functions

At least one programmer-defined function:int main(void)

Must declare & define function to useBuilt-in functions automatically via “.h” files

Programmer-defined functions defined outside of each other

return_type function_name(parameters) {

declarations; statements;}

Page 15: CSC 107 - Programming for Science Lecture 37 : Course Review.

Return Type & Parameters

Function must declare a return typeCan be any data type or void

When not void, must return valuereturn expression; ends function

execution and returns result of expression Functions can have 0 or more parameters

0 parameters noted using void keywordElse list type & name of each parameterSeparate parameters in listing by comma

Page 16: CSC 107 - Programming for Science Lecture 37 : Course Review.

Hints for Studying

The exam will NOT require:Memorizing any functionsMemorizing any codeAll but most simplistic material on struct

The exam WILL require:Knowing what the built-in functions do and

how to use themHow to write and use all the C constructs we

discussed

Page 17: CSC 107 - Programming for Science Lecture 37 : Course Review.

Studying For the Exam

1. What does this do? Review examples and make sure you can

explain the results Generate traces to verify your understanding

2. How is it used? And why is it used?

3. When do we use it? Why is it written that way?

Page 18: CSC 107 - Programming for Science Lecture 37 : Course Review.

In Conclusion…

CSC107 Final Exam:Tues., Dec. 12 from 1:00–3:00 in OM 208

Bring 1 (or more) pencils for the final Do well on all your exams Have a good Christmas break Consider taking further CSC courses

We’d love to see all of you again!