Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?

Similar presentations


Presentation on theme: "CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?"— Presentation transcript:

1 CSC 107 – Programming For Science

2 Science Means Solving Problems  Physics – How does an atom work?

3 Science Means Solving Problems  Physics – How does an atom work?  Engineering – Will the bridge hold?

4 Science Means Solving Problems  Physics – How does an atom work?  Engineering – Will the bridge hold?  Biology – What is crawling up my leg?

5 Science Means Solving Problems

6 Solving Problems With Code  Simple trick to this issue: good programs work  Everything else is secondary and much less important  Must have focus on creating working solution

7 Solving Problems With Code good programs work  Simple trick to this issue: good programs work  Everything else is secondary and much less important  Must have focus on creating working solution

8 Solving Problems With Code

9

10

11 Problem Solving Method 1. Clearly define the problem Will splitting an atom cause unstoppable explosion? Must translate series of digits into number, how? What bet in Vegas gives me best odds of winning?  What are winning numbers in tonight's lottery?  Am I pretty enough to become Miss America?

12 Problem Solving Method 2. Describe input and output information Input: U 235 Output: Input: Degree of angle Output: Angle's cosine Input: Car design Output: Fuel efficiency Input: Models of monster Output: Hair movement

13 Problem Solving Method 3. Develop algorithm or flowchart of solution before Check algorithms before sitting down to code Better be careful, double-check algorithms work Triple-check algorithms work just to be certain Do this by following example of Manhattan Project

14 Problem Solving Method 3. Develop algorithm or flowchart of solution before Check algorithms before sitting down to code Better be careful, double-check algorithms work Triple-check algorithms work just to be certain Do this by following example of Manhattan Project

15 Brian Kernighan, Creator of C

16 Tracing a Solution  Flowchart or pseudocode “run” like computer  Can use any inputs you want – choose range of values  Double-check that its results match expectations  Tracing simplifies development by ignoring stuff  Only flowchart or pseudocode; syntax unimportant  Adds variables anytime & without using a type  Record values during run to enable fixing bugs  Eventually match run to trace to discover error

17 Starting a Memory Trace  Paper holds 3 areas separated by vertical lines  Program stack drawn on left part of page  Right side of page holds the program heap  Program output shown outside the stack & heap  Draw frame in stack labeled main to show start  Fancy name for box labeled with method name  Each variable & its value shown inside this box

18 Coding Style Example #include using namespace std; int main(void) { int dcScore = 27, hstScore = 30; if (dcScore > hstScore) { cout << "Prof. Hertz happy" << endl; cout << "His team won" << endl; } else { cout << "His team lost." << endl; cout << "He is sad"; dcScore += 100; if (dcScore < hstScore) { cout << "Ouch. That's bad." <<endl; } } cout << "And now code continues" << endl; return 0; }

19 Tracing switch Statement float temp = -40; float convert; char c; cin >> c; switch (toupper(c)) { case ‘K’: temp = temp – 273.15; case ‘C’: convert = ((temp / 5) * 9) + 32; break; case ‘F’: convert = ((temp - 32) / 9) * 5; break; default: cout << “Amoronsayswhat?” << endl; } cout <<“Converted temp: ” << convert << endl;

20 Problem Solving Method 4. Rewrite flowchart as comments in code in file 5. Write code solving first part of problem Test just this code to be certain free from bugs Frequently testing gives bugs fewer places to hide Bug MUST be in code – already tested algorithm! Debugging simple – look for mismatch w/flowchart 6. Once first part done, write second part of code Focus on reducing bugs to make process easier Grow by continue building slowly upon tested code

21 For Next Lecture  Read sections 8.1 – 8.3 for Thursday  What if we want to do something repeatedly?  How should we tell the machine to stop?  What is difference between while and do-while ?  Week #4 weekly assignment due today at 5PM  Also available on Angel is week #5 assignment


Download ppt "CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?"

Similar presentations


Ads by Google