Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup.

Similar presentations


Presentation on theme: "CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup."— Presentation transcript:

1 CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup and Jennifer Welch

2 CSCE 121:509-512 Set 4: Computation Input, Compute, Output 2 Data (information) comes into program via keyboard, reading files, other input devices, other programs Program does something to input data to produce the output Display on screen, write to file, send to other output devices or other programs Input Computation Output

3 CSCE 121:509-512 Set 4: Computation Tools for Designing Computation How to figure out what computations to do to accomplish task? Divide and Conquer: break up large computation into many smaller ones Abstraction: provide a higher-level concept that hides detail Emphasis on structure and organization! 3

4 CSCE 121:509-512 Set 4: Computation Expressions An expression is made out of operators and operands Operator: what is to be done Operand: what data the operator works on (remember type safety!) A single literal or variable name is an expression Build up larger expressions by combining smaller ones with operands 4

5 CSCE 121:509-512 Set 4: Computation Examples of Expressions 13 x x+y b1 == b2 b*b – 4*c*c b*(b-4)*a*c 5

6 CSCE 121:509-512 Set 4: Computation Assignment as an Operator a = b is an expression Assignment operator = has two operands, the left-hand-side (LHS) and the right-hand-side (RHS) Variable name on LHS (e.g., a) refers to a location (address) Variable name on RHS (e.g., b) refers to the value stored in the location with that name 6

7 CSCE 121:509-512 Set 4: Computation Statements An expression followed by a semicolon A declaration A compound statement (group together a sequence of statements with { } ) A control flow statement (if, while, return,…) 7

8 CSCE 121:509-512 Set 4: Computation Statement Examples a = b*3; int n; {a = b; c = a;} 8

9 CSCE 121:509-512 Set 4: Computation Selection: If 9 if (condition) // returns boolean statement-1 else statement-2 if (a < b) // no semi-colon max = b; else // no semi-colon max = a;

10 CSCE 121:509-512 Set 4: Computation Nested Ifs 10 if (choice == ‘a’) … else if (choice == ‘b’) … else if (choice == ‘c’) … Can get confusing; motivates next selection statement

11 CSCE 121:509-512 Set 4: Computation Selection: switch 11 switch (choice) { // choice must be int, char, or // enumeration type case ‘a’: // values in case labels must be // constant expressions … break; // if you forget break, execution will // continue to next case case ‘b’: … break; case ‘c’: … break; default: // what to do if there is no match … break; }

12 CSCE 121:509-512 Set 4: Computation Iteration: while 12 while (condition) // returns boolean statement int i = 0; while (i < 100) { cout << i; ++i; // shorthand for i = i+1 }

13 CSCE 121:509-512 Set 4: Computation Iteration: for 13 for (int i = 0; i < 100; ++i) { cout << i; } Puts control variable management all in one place. Variable i only exists inside the for loop (note declaration). No control variable (i) modification in body of loop!

14 CSCE 121:509-512 Set 4: Computation Vectors Way to keep a collection of data items, all of the same type (e.g., a bunch of ints) Automatically increases in size as you add data items to the collection 14

15 CSCE 121:509-512 Set 4: Computation Some Vector Details To declare a vector variable named v that contains objects of type T: vector v; To add a data item x to a vector (at the end): v.push_back(x); // function call, more soon To get the number of data items in vector v: v.size() // another function call 15

16 CSCE 121:509-512 Set 4: Computation Vector Example Obtain a sequence of temperatures from the user and then find the mean (average). See sample code (pg. 122). 16

17 CSCE 121:509-512 Set 4: Computation Sorting a vector Another useful function that works on vectors: sort the elements in the vector – Rearrange them in the vector to be in sorted order Let v be the name of the vector. sort(v.begin(),v.end()); (More detail about this will be given later in the semester; for now, just use it.) 17

18 CSCE 121:509-512 Set 4: Computation Exercise Write a C++ program that gets a list of words from the user and then prints them all out, but with NO DUPLICATES First figure out what your strategy will be Then convert into C++ code 18

19 CSCE 121:509-512 Set 4: Computation Acknowledgments Photo on slide 1: “Exercise…” by A Health Blog, licensed under CC BY-SA 2.0Exercise…A Health BlogCC BY-SA 2.0 Slides are based on those for the textbook: http://www.stroustrup.com/Programming/4_computation.pp t 19


Download ppt "CSCE 121:509-512 Introduction to Program Design and Concepts Dr. J. Michael Moore Spring 2015 Set 4: Computation 1 Based on slides created by Bjarne Stroustrup."

Similar presentations


Ads by Google