🗊 Презентация Chapter 4 Computation

Нажмите для полного просмотра!
Chapter 4 Computation, слайд №1 Chapter 4 Computation, слайд №2 Chapter 4 Computation, слайд №3 Chapter 4 Computation, слайд №4 Chapter 4 Computation, слайд №5 Chapter 4 Computation, слайд №6 Chapter 4 Computation, слайд №7 Chapter 4 Computation, слайд №8 Chapter 4 Computation, слайд №9 Chapter 4 Computation, слайд №10 Chapter 4 Computation, слайд №11 Chapter 4 Computation, слайд №12 Chapter 4 Computation, слайд №13 Chapter 4 Computation, слайд №14 Chapter 4 Computation, слайд №15 Chapter 4 Computation, слайд №16 Chapter 4 Computation, слайд №17 Chapter 4 Computation, слайд №18 Chapter 4 Computation, слайд №19 Chapter 4 Computation, слайд №20 Chapter 4 Computation, слайд №21 Chapter 4 Computation, слайд №22 Chapter 4 Computation, слайд №23 Chapter 4 Computation, слайд №24 Chapter 4 Computation, слайд №25 Chapter 4 Computation, слайд №26 Chapter 4 Computation, слайд №27 Chapter 4 Computation, слайд №28 Chapter 4 Computation, слайд №29 Chapter 4 Computation, слайд №30

Вы можете ознакомиться и скачать презентацию на тему Chapter 4 Computation. Доклад-сообщение содержит 30 слайдов. Презентации для любого класса можно скачать бесплатно. Если материал и наш сайт презентаций Mypresentation Вам понравились – поделитесь им с друзьями с помощью социальных кнопок и добавьте в закладки в своем браузере.

Слайды и текст этой презентации


Слайд 1


Chapter 4 Computation Bjarne Stroustrup
Описание слайда:
Chapter 4 Computation Bjarne Stroustrup

Слайд 2


Abstract Today, I’ll present the basics of computation. In particular, we’ll discuss expressions, how to iterate over a series of values...
Описание слайда:
Abstract Today, I’ll present the basics of computation. In particular, we’ll discuss expressions, how to iterate over a series of values (“iteration”), and select between two alternative actions (“selection”). I’ll also show how a particular sub-computation can be named and specified separately as a function. To be able to perform more realistic computations, I will introduce the vector type to hold sequences of values. Selection, Iteration, Function, Vector

Слайд 3


Overview Computation What is computable? How best to compute it? Abstractions, algorithms, heuristics, data structures Language constructs and ideas...
Описание слайда:
Overview Computation What is computable? How best to compute it? Abstractions, algorithms, heuristics, data structures Language constructs and ideas Sequential order of execution Expressions and Statements Selection Iteration Functions Vectors

Слайд 4


You already know most of this Note: You know how to do arithmetic d = a+b*c You know how to select “if this is true, do that; otherwise do something...
Описание слайда:
You already know most of this Note: You know how to do arithmetic d = a+b*c You know how to select “if this is true, do that; otherwise do something else ” You know how to “iterate” “do this until you are finished” “do that 100 times” You know how to do functions “go ask Joe and bring back the answer” “hey Joe, calculate this for me and send me the answer” What I will show you today is mostly just vocabulary and syntax for what you already know

Слайд 5


Computation Input: from keyboard, files, other input devices, other programs, other parts of a program Computation – what our program will do with...
Описание слайда:
Computation Input: from keyboard, files, other input devices, other programs, other parts of a program Computation – what our program will do with the input to produce the output. Output: to screen, files, other output devices, other programs, other parts of a program

Слайд 6


Computation Our job is to express computations Correctly Simply Efficiently One tool is called Divide and Conquer to break up big computations into...
Описание слайда:
Computation Our job is to express computations Correctly Simply Efficiently One tool is called Divide and Conquer to break up big computations into many little ones Another tool is Abstraction Provide a higher-level concept that hides detail Organization of data is often the key to good code Input/output formats Protocols Data structures Note the emphasis on structure and organization You don’t get good code just by writing a lot of statements

Слайд 7


Language features Each programming language feature exists to express a fundamental idea For example + : addition * : multiplication if (expression)...
Описание слайда:
Language features Each programming language feature exists to express a fundamental idea For example + : addition * : multiplication if (expression) statement else statement ; selection while (expression) statement ; iteration f(x); function/operation … We combine language features to create programs

Слайд 8


Expressions // compute area: int length = 20; // the simplest expression: a literal (here, 20) // (here used to initialize a variable) int width =...
Описание слайда:
Expressions // compute area: int length = 20; // the simplest expression: a literal (here, 20) // (here used to initialize a variable) int width = 40; int area = length*width; // a multiplication int average = (length+width)/2; // addition and division The usual rules of precedence apply: a*b+c/d means (a*b)+(c/d) and not a*(b+c)/d. If in doubt, parenthesize. If complicated, parenthesize. Don’t write “absurdly complicated” expressions: a*b+c/d*(e-f/g)/h+7 // too complicated Choose meaningful names.

Слайд 9


Expressions Expressions are made out of operators and operands Operators specify what is to be done Operands specify the data for the operators to...
Описание слайда:
Expressions Expressions are made out of operators and operands Operators specify what is to be done Operands specify the data for the operators to work with Boolean type: bool (true and false) Equality operators: = = (equal), != (not equal) Logical operators: && (and), || (or), ! (not) Relational operators: (greater than), = Character type: char (e.g., 'a', '7', and '@') Integer types: short, int, long arithmetic operators: +, -, *, /, % (remainder) Floating-point types: e.g., float, double (e.g., 12.45 and 1.234e3) arithmetic operators: +, -, *, /

Слайд 10


Concise Operators For many binary operators, there are (roughly) equivalent more concise operators For example a += c means a = a+c a *= scale means...
Описание слайда:
Concise Operators For many binary operators, there are (roughly) equivalent more concise operators For example a += c means a = a+c a *= scale means a = a*scale ++a means a += 1 or a = a+1 “Concise operators” are generally better to use (clearer, express an idea more directly)

Слайд 11


Statements A statement is an expression followed by a semicolon, or a declaration, or a “control statement” that determines the flow of control For...
Описание слайда:
Statements A statement is an expression followed by a semicolon, or a declaration, or a “control statement” that determines the flow of control For example a = b; double d2 = 2.5; if (x == 2) y = 4; while (cin >> number) numbers.push_back(number); int average = (length+width)/2; return x; You may not understand all of these just now, but you will …

Слайд 12


Selection Sometimes we must select between alternatives For example, suppose we want to identify the larger of two values. We can do this with an if...
Описание слайда:
Selection Sometimes we must select between alternatives For example, suppose we want to identify the larger of two values. We can do this with an if statement if (a

Слайд 13


Iteration (while loop) The world’s first “real program” running on a stored-program computer (David Wheeler, Cambridge, May 6, 1949) // calculate and...
Описание слайда:
Iteration (while loop) The world’s first “real program” running on a stored-program computer (David Wheeler, Cambridge, May 6, 1949) // calculate and print a table of squares 0-99: int main() { int i = 0; while (i

Слайд 14


Iteration (while loop) What it takes A loop variable (control variable); here: i Initialize the control variable; here: int i = 0 A termination...
Описание слайда:
Iteration (while loop) What it takes A loop variable (control variable); here: i Initialize the control variable; here: int i = 0 A termination criterion; here: if i

Слайд 15


Iteration (for loop) Another iteration form: the for loop You can collect all the control information in one place, at the top, where it’s easy to...
Описание слайда:
Iteration (for loop) Another iteration form: the for loop You can collect all the control information in one place, at the top, where it’s easy to see for (int i = 0; i

Слайд 16


Functions But what was square(i)? A call of the function square() int square(int x) { return x*x; } We define a function when we want to separate a...
Описание слайда:
Functions But what was square(i)? A call of the function square() int square(int x) { return x*x; } We define a function when we want to separate a computation because it is logically separate makes the program text clearer (by naming the computation) is useful in more than one place in our program eases testing, distribution of labor, and maintenance

Слайд 17


Control Flow
Описание слайда:
Control Flow

Слайд 18


Functions Our function int square(int x) { return x*x; } is an example of Return_type function_name ( Parameter list ) // (type name, etc.) { // use...
Описание слайда:
Functions Our function int square(int x) { return x*x; } is an example of Return_type function_name ( Parameter list ) // (type name, etc.) { // use each parameter in code return some_value; // of Return_type }

Слайд 19


Another Example Earlier we looked at code to find the larger of two values. Here is a function that compares the two values and returns the larger...
Описание слайда:
Another Example Earlier we looked at code to find the larger of two values. Here is a function that compares the two values and returns the larger value. int max(int a, int b) // this function takes 2 parameters { if (a

Слайд 20


Data for Iteration - Vector To do just about anything of interest, we need a collection of data to work on. We can store this data in a vector. For...
Описание слайда:
Data for Iteration - Vector To do just about anything of interest, we need a collection of data to work on. We can store this data in a vector. For example: // read some temperatures into a vector: int main() { vector temps; // declare a vector of type double to store // temperatures – like 62.4 double temp; // a variable for a single temperature value while (cin>>temp) // cin reads a value and stores it in temp temps.push_back(temp); // store the value of temp in the vector // … do something … } // cin>>temp will return true until we reach the end of file or encounter // something that isn’t a double: like the word “end”

Слайд 21


Vector Vector is the most useful standard library data type a vector holds an sequence of values of type T Think of a vector this way A vector named...
Описание слайда:
Vector Vector is the most useful standard library data type a vector holds an sequence of values of type T Think of a vector this way A vector named v contains 5 elements: {1, 4, 2, 3, 5}:

Слайд 22


Vectors vector v; // start off empty v.push_back(1); // add an element with the value 1 v.push_back(4); // add an element with the value 4 at end...
Описание слайда:
Vectors vector v; // start off empty v.push_back(1); // add an element with the value 1 v.push_back(4); // add an element with the value 4 at end (“the back”) v.push_back(3); // add an element with the value 3 at end (“the back”) v[0] v[1] v[2]

Слайд 23


Vectors Once you get your data into a vector you can easily manipulate it: // compute mean (average) and median temperatures: int main() { vector...
Описание слайда:
Vectors Once you get your data into a vector you can easily manipulate it: // compute mean (average) and median temperatures: int main() { vector temps; // temperatures in Fahrenheit, e.g. 64.6 double temp; while (cin>>temp) temps.push_back(temp); // read and put into vector double sum = 0; for (int i = 0; i< temps.size(); ++i) sum += temps[i]; // sums temperatures cout

Слайд 24


Combining Language Features You can write many new programs by combining language features, built-in types, and user-defined types in new and...
Описание слайда:
Combining Language Features You can write many new programs by combining language features, built-in types, and user-defined types in new and interesting ways. So far, we have Variables and literals of types bool, char, int, double vector, push_back(), [ ] (subscripting) !=, ==, =, +, -, +=, >, cout

Слайд 25


Example – Word List // “boilerplate” left out vector words; string s; while (cin>>s && s != "quit") // && means AND words.push_back(s);...
Описание слайда:
Example – Word List // “boilerplate” left out vector words; string s; while (cin>>s && s != "quit") // && means AND words.push_back(s); sort(words.begin(), words.end()); // sort the words we read for (int i=0; i

Слайд 26


Word list – Eliminate Duplicates // Note that duplicate words were printed multiple times. For // example “the the the”. That’s tedious, let’s...
Описание слайда:
Word list – Eliminate Duplicates // Note that duplicate words were printed multiple times. For // example “the the the”. That’s tedious, let’s eliminate duplicates: vector words; string s; while (cin>>s && s!= "quit") words.push_back(s); sort(words.begin(), words.end()); for (int i=1; i

Слайд 27


Example (cont.) Eliminate Words! // Eliminate the duplicate words by copying only unique words: vector words; string s; while (cin>>s && s!=...
Описание слайда:
Example (cont.) Eliminate Words! // Eliminate the duplicate words by copying only unique words: vector words; string s; while (cin>>s && s!= "quit") words.push_back(s); sort(words.begin(), words.end()); vectorw2; if (0

Слайд 28


Algorithm We just used a simple algorithm An algorithm is (from Google search) “a logical arithmetical or computational procedure that, if correctly...
Описание слайда:
Algorithm We just used a simple algorithm An algorithm is (from Google search) “a logical arithmetical or computational procedure that, if correctly applied, ensures the solution of a problem.” – Harper Collins “a set of rules for solving a problem in a finite number of steps, as for finding the greatest common divisor.” – Random House “a detailed sequence of actions to perform or accomplish some task. Named after an Iranian mathematician, Al-Khawarizmi. Technically, an algorithm must reach a result after a finite number of steps, …The term is also used loosely for any sequence of actions (which may or may not terminate).” – Webster’s We eliminated the duplicates by first sorting the vector (so that duplicates are adjacent), and then copying only strings that differ from their predecessor into another vector.

Слайд 29


Ideal Basic language features and libraries should be usable in essentially arbitrary combinations. We are not too far from that ideal. If a...
Описание слайда:
Ideal Basic language features and libraries should be usable in essentially arbitrary combinations. We are not too far from that ideal. If a combination of features and types make sense, it will probably work. The compiler helps by rejecting some absurdities.

Слайд 30


The next lecture How to deal with errors
Описание слайда:
The next lecture How to deal with errors



Теги Chapter Computation
Похожие презентации
Mypresentation.ru
Загрузить презентацию