CSC 107 - Programming for Science Lecture 37 : Course Review.
Click here to load reader
-
Upload
roderick-charles -
Category
Documents
-
view
218 -
download
0
description
Transcript of CSC 107 - Programming for Science Lecture 37 : Course Review.
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
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
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>
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)
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());
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 &
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
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
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
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
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”)
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
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;}
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
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
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?
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!