Download presentation
Presentation is loading. Please wait.
Published byEvelyn Robinson Modified over 9 years ago
1
CSC 107 – Programming For Science
2
Final Exams Dec. 16, 8AM – 10AM in OM221 Exam for CSC107: Dec. 16, 8AM – 10AM in OM221 Will be done using paper & pencil in classroom Open-note & open-book so bring your cheatsheets Problem on exam will be similar to midterms Dec. 14, 9:00 – 10:00 Dec. 14, 10:15 – 11:15 Mastery Exam: Dec. 14, 9:00 – 10:00 (if lab on Thurs ) Dec. 14, 10:15 – 11:15 (if lab on Tues ) Given in OM115 and will be programming w/ Eclipse Open-note & open-book so bring your cheatsheets Halfway between labs & weekly assignments problems Will have chance to practice in lab next week
3
Today’s Goal Review effects of #define s statements How we can use these to create symbolic constants Use of symbolic constants & how differ from variables Discuss how to use #define for C++ macros Parameter meaning within macro & potential errors Within the macro what are the limits on code & use Discuss compact if-else ( ? : ) & see how it works Examine problems with it & why its rarely used Compare macros & functions within C++
4
Symbolic Constants Directive can be used to name a constant Any/all lines BELOW directive can use this constant Pre-processor replaces name with value Compiler sees value as if that were written there When reading the code, programmer sees name Makes code much easier to read, write, debug Names traditionally in all CAPITAL letters THIS IS NOT REQUIRED THIS IS NOT REQUIRED, but “good style”
5
Symbolic Constants Use Do not actually exist when program runs Symbolic constants not allocated in stack or heap Not just for constants: can be expressions or statements Often in header files since used for vital information Contents copy-and-pasted without investigating Starts copying after it skips space(s) until line’s end not Do not include equals sign ( = ) unless you want it copied no Semicolons copied also, so no semicolons needed
6
What You Write And Work With #define PI 3.1415 #define AVOGADRO 6.022E23 #define R_SQUARED r * r #define DUMB_EXAMPLE “Matthew Hertz” double area = PI * (R_SQUARED); cout << R_SQUARED; cout << DUMB_EXAMPLE;
7
What The Compiler Sees #define PI 3.1415 #define AVOGADRO 6.022E23 #define R_SQUARED r * r #define DUMB_EXAMPLE “Matthew Hertz” double area = PI * (R_SQUARED); cout << R_SQUARED; cout << DUMB_EXAMPLE;
8
What The Compiler Sees #define AVOGADRO 6.022E23 #define R_SQUARED r * r #define DUMB_EXAMPLE “Matthew Hertz” double area = 3.1415 * (R_SQUARED); cout << R_SQUARED; cout << DUMB_EXAMPLE;
9
What The Compiler Sees #define AVOGADRO 6.022E23 #define R_SQUARED r * r #define DUMB_EXAMPLE “Matthew Hertz” double area = 3.1415 * (R_SQUARED); cout << R_SQUARED; cout << DUMB_EXAMPLE;
10
What The Compiler Sees #define R_SQUARED r * r #define DUMB_EXAMPLE “Matthew Hertz” double area = 3.1415 * (R_SQUARED); cout << R_SQUARED; cout << DUMB_EXAMPLE;
11
What The Compiler Sees #define DUMB_EXAMPLE “Matthew Hertz” double area = 3.1415 * (r * r); cout << r * r; cout << DUMB_EXAMPLE;
12
What The Compiler Sees double area = 3.1415 * (r * r); cout << r * r; cout << “Matthew Hertz”;
13
What The Compiler Sees double area = 3.1415 * (r * r); cout << r * r; cout << “Matthew Hertz”;
14
Macro Basis C/C++ constructs similar to symbolic constants (Mostly) Copy-and-paste contents within program Within file can be used anywhere BELOW declaration Line begins with #define & goes through lines end But, unlike before, macros contain parameters Replaced by arguments when pasted into program Replacement performed by copy-and-pasting, also No types required, since no memory used by params caveat emptor Low-overhead, limited checks; caveat emptor
15
Writing Macros
16
Calling Macro From Program Called like function & must match # of params Can use any expression since will be copy-and-pasted Low overhead makes their usage much looser MUCH MUCH harder to find & fix bugs with this Compiler errors in macro will flagged on each call May not be visible, since error could be in macro Debugging also hard, since unclear where code exists
17
Coding With Macros #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if (IS_UPPER(rex[1])) { rex = NEXT_WORD(rex); } if (SQUARE(i) < 12) { cout << IS_UPPER(b) << SQUARE(rex); }
18
Coding With Macros #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if ((rex[1] >= ‘A’ && rex[1] <= ‘Z’)) { rex = NEXT_WORD(rex); } if (SQUARE(i) < 12) { cout << IS_UPPER(b) << SQUARE(rex); }
19
Coding With Macros #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if ((rex[1] >= ‘A’ && rex[1] <= ‘Z’)) { rex = strchr(rex, ‘ ‘) + 1; } if (SQUARE(i) < 12) { cout << IS_UPPER(b) << SQUARE(rex); }
20
Coding With Macros #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if ((rex[1] >= ‘A’ && rex[1] <= ‘Z’)) { rex = strchr(rex, ‘ ‘) + 1; } if ((i*i) < 12) { cout << IS_UPPER(b) << SQUARE(rex); }
21
Coding With Macros #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if ((rex[1] >= ‘A’ && rex[1] =‘A’&& b<=‘Z’) << (rex * rex); }
22
Coding With Macros #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if ((rex[1] >= ‘A’ && rex[1] =‘A’&& b<=‘Z’) << (rex * rex); }
23
Remember You See This Code… #define IS_UPPER(c) (c >= ‘A’ && \ c <= ‘Z’) #define NEXT_WORD(rex) strchr(rex, ‘ ‘)+1 #define SQUARE(longP) (longP * longP) double b = 23; char rex[100] = “Censored censored”; int i = 12; if (IS_UPPER(rex[1])) { rex = NEXT_WORD(rex); } if (SQUARE(i) < 12) { cout << IS_UPPER(b) << SQUARE(rex); }
24
Compact If-Else Operator Macros often want to “return” result of if - else Must compute in 1 expression; cannot use return Luckily, C++ includes a really ugly hack for this
25
Compact If-Else Operator Macros often want to “return” result of if - else Must compute in 1 expression; cannot use return Luckily, C++ includes a really ugly hack for this
26
Compact If-Else Operator ?: operator is if - else computing a value (boolean expression) ? value if true : value if false #define POSITIVE(x) (x > 0) ? true : false; #define ROOT(x, y) (y == 0) ? 0 : pow(x,-y); int p = (pow(k, 2) < 10) ? pow(j,2) : 3; Creates really hard to read & debug code Use only when necessary, but occasionally helpful
27
Oops... Care needed to protect arguments in macros Copy-and-pasted Copy-and-pasted, but by something with IQ of 0 Despite best of intentions, unseen bugs occur #define SUMSQUARE(a,b) a*a+b*b int x = 3, y = 4; cout << SUMSQUARE(2,4)<<endl; cout << SUMSQUARE(x,2)<<endl; cout << SUMSQUARE(4,y)<<endl; cout << SUMSQUARE(x,y)<<endl; cout << SUMSQUARE(x+1,y+3)<<endl; cout << SUMSQUARE(x++,++y)<<endl;
28
Protected Macros Parenthesis are your friends, use them liberally Use them to surround all uses of all params Consider order of operations & how they can change #define SQR_U5(i) (((i)*(i))<5) int j = 2; double dbl = -3.0; SQR_U5(j); SQR_U5(dbl); SQR_U5(j-2); SQR_U5(dbl+2); SQR_U5(j+dbl);
29
Steel Cage Match FunctionMacro Stands on own in program Fixed types cannot change Slight overhead per call Results apparent & clear Easy to find and debug Useful nearly always Must be pasted in function Params untyped & flexible No frame, so no cost to call Murky effects go unseen Often in header & impossible to fix tiny operations Good for tiny operations
30
Your Turn Get into your groups and try this assignment
31
For Next Lecture Read Section 16.6 on recursion for Wednesday What does it mean for function to be recursive? How does recursion work & drawing traces this code Why would we ever bother to use recursion in code? Angel has Weekly Assignment #14 due tomorrow Last project Last project due on Friday
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.