Презентация Chapter 4 Computation онлайн
На нашем сайте вы можете скачать и просмотреть онлайн доклад-презентацию на тему Chapter 4 Computation абсолютно бесплатно. Урок-презентация на эту тему содержит всего 30 слайдов. Все материалы созданы в программе PowerPoint и имеют формат ppt или же pptx. Материалы и темы для презентаций взяты из открытых источников и загружены их авторами, за качество и достоверность информации в них администрация сайта не отвечает, все права принадлежат их создателям. Если вы нашли то, что искали, отблагодарите авторов - поделитесь ссылкой в социальных сетях, а наш сайт добавьте в закладки.
Презентации » Устройства и комплектующие » Chapter 4 Computation
Оцените!
Оцените презентацию от 1 до 5 баллов!
- Тип файла:ppt / pptx (powerpoint)
- Всего слайдов:30 слайдов
- Для класса:1,2,3,4,5,6,7,8,9,10,11
- Размер файла:579.50 kB
- Просмотров:62
- Скачиваний:0
- Автор:неизвестен
Слайды и текст к этой презентации:
№2 слайд
![Abstract Today, I ll present](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img1.jpg)
Содержание слайда: 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
№4 слайд
![You already know most of this](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img3.jpg)
Содержание слайда: 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
№6 слайд
![Computation Our job is to](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img5.jpg)
Содержание слайда: 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](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img6.jpg)
Содержание слайда: 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](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img7.jpg)
Содержание слайда: 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](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img8.jpg)
Содержание слайда: 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: < (less than), > (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](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img9.jpg)
Содержание слайда: 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](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img10.jpg)
Содержание слайда: 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](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img11.jpg)
Содержание слайда: 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<b) // Note: No semicolon here
max = b;
else // Note: No semicolon here
max = a;
The syntax is
if (condition)
statement-1 // if the condition is true, do statement-1
else
statement-2 // if not, do statement-2
№13 слайд
![Iteration while loop The](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img12.jpg)
Содержание слайда: 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<100) {
cout << i << '\t' << square(i) << '\n';
++i ; // increment i
}
}
// (No, it wasn’t actually written in C++ .)
№14 слайд
![Iteration while loop What it](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img13.jpg)
Содержание слайда: 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<100 is false, terminate
Increment the control variable; here: ++i
Something to do for each iteration; here: cout << …
int i = 0;
while (i<100) {
cout << i << '\t' << square(i) << '\n';
++i ; // increment i
}
№15 слайд
![Iteration for loop Another](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img14.jpg)
Содержание слайда: 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<100; ++i) {
cout << i << '\t' << square(i) << '\n';
}
That is,
for (initialize; condition ; increment )
controlled statement
Note: what is square(i)?
№16 слайд
![Functions But what was square](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img15.jpg)
Содержание слайда: 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
№19 слайд
![Another Example Earlier we](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img18.jpg)
Содержание слайда: 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<b)
return b;
else
return a;
}
int x = max(7, 9); // x becomes 9
int y = max(19, -27); // y becomes 19
int z = max(20, 20); // z becomes 20
№20 слайд
![Data for Iteration - Vector](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img19.jpg)
Содержание слайда: 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<double> 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”
№23 слайд
![Vectors Once you get your](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img22.jpg)
Содержание слайда: Vectors
Once you get your data into a vector you can easily manipulate it:
// compute mean (average) and median temperatures:
int main()
{
vector<double> 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 << "Mean temperature: " << sum/temps.size() << endl;
sort(temps.begin(),temps.end());
cout << "Median temperature: " << temps[temps.size()/2] << endl;
}
№24 слайд
![Combining Language Features](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img23.jpg)
Содержание слайда: 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)
!=, ==, =, +, -, +=, <, &&, ||, !
max( ), sort( ), cin>>, cout<<
if, for, while
You can write a lot of different programs with these language features! Let’s try to use them in a slightly different way…
№25 слайд
![Example Word List boilerplate](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img24.jpg)
Содержание слайда: Example – Word List
// “boilerplate” left out
vector<string> 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<words.size(); ++i)
cout<<words[i]<< "\n";
/*
read a bunch of strings into a vector of strings, sort
them into lexicographical order (alphabetical order),
and print the strings from the vector to see what we have.
*/
№26 слайд
![Word list Eliminate](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img25.jpg)
Содержание слайда: 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<string> words;
string s;
while (cin>>s && s!= "quit") words.push_back(s);
sort(words.begin(), words.end());
for (int i=1; i<words.size(); ++i)
if(words[i-1]==words[i])
“get rid of words[i]” // (pseudocode)
for (int i=0; i<words.size(); ++i) cout<<words[i]<< "\n";
// there are many ways to “get rid of words[i]”; many of them are messy
// (that’s typical). Our job as programmers is to choose a simple clean
// solution – given constraints – time, run-time, memory.
№27 слайд
![Example cont. Eliminate](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img26.jpg)
Содержание слайда: Example (cont.) Eliminate Words!
// Eliminate the duplicate words by copying only unique words:
vector<string> words;
string s;
while (cin>>s && s!= "quit") words.push_back(s);
sort(words.begin(), words.end());
vector<string>w2;
if (0<words.size()) { // Note style { }
w2.push_back(words[0]);
for (int i=1; i<words.size(); ++i)
if(words[i-1]!=words[i])
w2.push_back(words[i]);
}
cout<< "found " << words.size()-w2.size() << " duplicates\n";
for (int i=0; i<w2.size(); ++i) cout << w2[i] << "\n";
№28 слайд
![Algorithm We just used a](/documents_6/689fe8697aaaa2780ca19eeadb3fd75c/img27.jpg)
Содержание слайда: 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.
Скачать все slide презентации Chapter 4 Computation одним архивом:
Похожие презентации
-
Sql Chapter Two
-
Prolog. A general-purpose logic programming language associated with artificial intelligence and computational linguistics
-
Computational and Problem Solving(SDP1)
-
Intrusion Detection. Chapter 8. Computer Security: Principles and Practice
-
Malicious Software. Chapter 6. Computer Security: Principles and Practice
-
Programming Logic and Design. Chapter 7. File Handling and Applications
-
Programming logic and design seventh edition. Chapter 5. Looping
-
Programming Logic and Design Seventh Edition. Chapter 4. Making Decisions
-
Programming Logic and Design Seventh Edition. Chapter 1. An Overview of Computers and Programming
-
Operating systems. (Chapter 10)