Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
To get help:
Piazza 183help@umich.edu (when you reply, do Reply All)
Go to discussion sections
Go to office hours
CodeLab
"sort by deadline" upper-left-hand side Infinite number of submits possible
ML Dorf
Thinking Memory
Question
Answer
Processor
Question
Answ
er
Memory
(stores data and procedure program)
Computer
Memory
Data
Software
Code
Data
Program
Results
(computer)
A program is:
An implementation of an algorithm
An algorithm is:
A sequence of instructions used to
accomplish a goal.
Divisible By 2
Even
A Programming Language is: an artificial language designed to communicate instructions to a computer Convenient for people to write programs Convenient for computers to understand
Why not use Natural Languages ?
(like English)
Semantics (Meaning)
Examples: if = condition while = conditioned loop Etc.
No ambiguity
Algorithm
C++ Implementation
Algorithm
Python Implementation
http://en.wikipedia.org/wiki/List_of_programming_languages_by_t ype#Procedural_languages
Machine
Compiled
VS
Interpreted
Machine
High-level
High-Level
int x = 3; int y = 5; boolean flag; if(x+y > 7){ flag = true; }
Easy to read, write, & debug Employ powerful data and control primitives e.g. C++, Java, PHP, etc
Difficult to read, write Direct control Executes fast Hard to use Not portable
High-Level
int x = 3; int y = 5; boolean flag; if(x+y > 7){ flag = true; }
Compiler
Easy to read, write, & debug Employ powerful data and control primitives e.g. C++, Java, PHP, etc
Difficult to read, write Direct control Executes fast Hard to use Not portable
Compiled
Interpreted
Recently turned 30: Has aged well Makes it possible to create programs with
Fast execution Small amount of code Runs on a variety of computing environments (portable) Functionality Features
C++ 11
Standardized in 2011
Fast execution
Best asset
Software development more rapid
Execution speed
Has improved still slow comparatively
Memory was something you lost with age An application was for employment A program was a TV show A keyboard was a piano A web was a spider's home A virus was the flu A CD was a bank account A hard drive was a long trip on the road A mouse pad was where a mouse lived
First Program C++ Language Elements Standard Libraries Identifiers Comments Variables & Literals Operators Data Types Expressions Mixed Mode
Casting
Start
int main (void) { return 0; } Go back to where the function was called from (OS in case of 'main')
Return Type
int main (void) { return 0; }
Return Type
int main (void) { these MUST match in types return 0; } Return Value
#include <iostream> using namespace std; int main (void) { cout << "Hello World!" << endl; return 0; }
#include <iostream> using namespace std; int main (void) { cout << "Hello World!" << endl; return 0; }
A Statement. Prints "Hello World!" to the standard output stream
#include <iostream> using namespace std; int main (void) { cout << "Hello World!" << endl; return 0; }
object to send output to Basically, says to print to standard output stream
#include <iostream> using namespace std; int main (void) { cout << "Hello World!" << endl; return 0; }
int main (void) { cout << "Hello World!" << endl; return 0; }
#include <iostream> C++ using namespace std; this is a STATEMENT int main (void) { cout << "Hello World!" << endl; return 0; }
int main (void) { cout << "Hello World!" << endl; return 0; }
I/O operations require including the I/O standard file from the C++ standard library.
int main (void) { cout << "Hello World!" << endl; return 0; }
A directive that tells the preprocessor to include code of iostream library before compilation
int main (void) { cout << "Hello World!" << endl; return 0; }
All files in the C++ standard library declare all of its entities within the std namespace.
int main (void) { cout << "Hello World!" << endl; if __name__=='__main__': return 0; main(sys.argv) }
int main (void) { cout << "Hello World!" << endl; if __name__=='__main__': return 0; main(sys.argv) }
int main (void) { cout << "Hello World!" << endl; if __name__=='__main__': return 0; main(sys.argv) }
A) Make reading the code easier B) Meant to confuse students C) Group statements together D) Show where main begins and ends
Always keep your code in a working state Write some code, compile it and test it Then write some more code, compile it and test it
Etc.
Always keep your code in a working state Write some code, compile it and test it Then write some more code, compile it and test it Etc. You dont want to write a lot of code and then discover that something is wrong! Why???
First Program C++ Language Elements Standard Libraries Identifiers Comments Variables & Literals Operators Data Types Expressions Mixed Mode
Casting
input and output needs to be specifically included to be used #include <iostream> using namespace std;
input and output needs to be specifically included to be used #include <iostream> Python: using namespace std; import
First Program C++ Language Elements Standard Libraries Identifiers Comments Variables & Literals Operators Data Types Expressions Mixed Mode
Casting
// line comment
/* ... block comment */
// line comment
/* ... block comment */
import sys
def main (argv): print 'Hello World!'
First Program C++ Language Elements Standard Libraries Identifiers Comments Variables & Literals Operators Data Types Expressions Mixed Mode
Casting
Values
Also knows as literals
cout << 1 + 2; //1 2 are integer literals cout << "Hello World!"; //string literal
Common "operators" or symbols to do calculations are: Add 2 numbers Subtract 2 numbers Multiply 2 numbers Divide one number by another Remainder with INTEGER division (MOD)
+ * / %
+3
-5 * 7
10 % 6
First Program C++ Language Elements Standard Libraries Identifiers Comments Variables & Literals Operators Data Types Expressions Mixed Mode
Casting
Expressions are sequences of operators and operands. Operands can be literals, variables, or other expressions
Examples:
5 + (3 / 2) * 10 -5 + 3
Precedence 1 2 3 4 5
Operator
() + - (unary) * / % + - (binary) =
Grouping Left to right Right to left Left to right Left to right Right to left
See a full list http://en.cppreference.com/w/cpp/language/operator_precedence Grouping defines the precedence order when several operators of the same precedence level are in an expression.
Grouping Left to right Right to left Left to right Left to right Right to left
cout << 10 / 5 - 3 + 2 * 6;
A) B) C) D) E) 6 11 17 60 None of the above
A) B) C) D) E)
A) B) C) D) E)
What does the following statement print? cout << "24 / 4 * 2";
A) B) C) D)
5 % 2
2 % 3
A) 0 B) 1 C) 2 D) 3 E) Dont Know
5 % 2
2 % 3
A) 0 B) 1 C) 2 D) 3 E) Dont Know
5 % 2
2 % 3
4 % 2
A) 0 B) 1 C) 2 D) 3 E) Dont Know
Given pCode an int variable that holds postal codes MI has 5-digit postal codes that start with 48 or 49
48109 for BBB Building on Hayward To get the 1st 2 digits of pCode:
A) B) C) D) pCode pCode pCode pCode % / % / 10000 10000 1000 1000
First Program C++ Language Elements Standard Libraries Identifiers Comments Variables & Literals Operators Data Types Expressions Mixed Mode
Casting
A variable is a name for something Sets up a location in memory Stores a value into that location
Give it a name that describes its purpose Specify the type of info it will hold Examples: Memory
int playerScore; int numZombiesDefeated;
playerScore
numZombiesDefeated
float
hold over from years ago computers had limited memory
Examples: 'A'
'$'
'5' 'e'
Range
-128 to 127 (signed) 0 to 255
(unsigned)
Dec 0 1 2 3 4
Char NULL Start of Heading Start of Text End of Text End of Transmission
Dec 12 13 14 15 16
Char Form Feed (\f) Carriage Return (\r) Shift Out Shift In Data Link Escape
5
6 7 8 9
Enquiry
Acknowledge Bell (\a) Backspace (\b) Horizontal Tab (\t)
17
18 19 20 21
Device Cntrl 1
Device Cntrl 2 Device Cntrl 3 Device Cntrl 4 Negative Acknowledge
29
30 31
Group Separator
Record Separator Unit Separator
10 11
22 23
Dec 32 33 34 35 36
Char Dec
space
Char Dec . / 0 1 2 60 61 62 63 64
Char Dec J K L M N 88 89 90 91 92
Char t u v w x
46 47 48 49 50
! " # $
37
38 39 40 41 42 43 44 45
%
& ' ( ) * + , -
51
52 53 54 55 56 57 58 59
3
4 5 6 7 8 9 : ;
65
66 67 68 69 70 71 72 73
A
B C D E F G H I
79
80 81 82 83 84 85 86 87
O
P Q R S T U V W
93
94 95 96 97 98 99 100 101
]
^ _ ` a b c d e
107
108 109 110 111 112 113 114 115
k
l m n o p q r s
121
122 123 124 125 126
y
z { | } ~
Dec 32 33 34 35 36
Char Dec
space
Char Dec . / 0 1 2 60 61 62 63 64
Char Dec J K L M N 88 89 90 91 92
Char t u v w x
46 47 48 49 50
! " # $
37
38 39 40 41 42 43 44 45
%
& ' ( ) * + , -
51
52 53 54 55 56 57 58 59
3
4 5 6 7 8 9 : ;
65
66 67 68 69 70 71 72 73
A
B C D E F G H I
79
80 81 82 83 84 85 86 87
O
P Q R S T U V W
93
94 95 96 97 98 99 100 101
]
^ _ ` a b c d e
107
108 109 110 111 112 113 114 115
k
l m n o p q r s
121
122 123 124 125 126
y
z { | } ~
Range:
Signed: -2147483468 to 2147483467 (32-bit) Unsigned: 0 to 4294967295
Whole numbers
Range:
-2147483468 to 2147483467 (32-bit)
How about: remember 10 digits and 1st digit is a 2 (or just "2 billion+")
Be Careful
double
Size: 8 bytes
Range: 2.22507e-308 to 1.79769e+308
Examples:
1.32e2 3.0E4 5E-3
0.06 represented as
6e-2 6E-2 6.0e-2 .6e-1
Size: 4 bytes Range: 1.17549e-38 to 3.40282e+38 original form for Real Numbers
boolean values normally are the result of comparing two values more on these later
Name char
Size*
int
Integer
Range* signed: -128 to 127 1byte unsigned: 0 to 255 signed: -2147483648 to 2147483647 4bytes unsigned: 0 to 4294967295 (10 digits)
Boolean value. It can bool take one of two 1byte true or false values: true or false Double precision +/- 1.7e +/- 308 double 8bytes floating point number (~15 digits)
variables must have a type variables must have a name variable type cant be changed once declared Examples:
int value; double pi; int a, b, c; char oneChar;
1) Start with a letter or underscore ('_') 2) After the first character, any number of letters, underscores, or digits 3) Cant be reserved word (also known as reserved words)
Max size 31 chars (most systems) C++ is case-sensitive (int != Int) System identifiers usually start with '_'
alignas alignof and and_eq asm auto(1) bitand bitor bool break case catch char char16_t char32_t
class compl const constexpr const_cast continue decltype default delete do double dynamic_cast else
enum explicit export extern false float for friend goto if inline int long mutable
namespace new noexcept not not_eq nullptr operator or or_eq private protected public register reinterpret_cast
return short signed sizeof static static_assert static_cast struct switch template this thread_local throw true
try typedef typeid typename union unsigned using virtual void volatile wchar_t while xor xor_eq
alignas alignof and and_eq asm auto(1) bitand bitor bool break case catch char char16_t char32_t
class compl const constexpr const_cast continue decltype default delete do double dynamic_cast else
enum explicit export extern false float for friend goto if inline int long mutable
namespace new noexcept not not_eq nullptr operator or or_eq private protected public register reinterpret_cast
return short signed sizeof static static_assert static_cast struct switch template this thread_local throw true
try typedef typeid typename union unsigned using virtual void volatile wchar_t while xor xor_eq
import sys
def main (argv): print 'Hello World!'
4 bytes
(int size)
value
4 bytes
(int size)
value
8 bytes
(double size)
pi
value
pi
value
variable = value
Assignment (not equality)
Example int x, y; x = 5; y = 3; x = y;
Example
Execution
int x, y; x = 5; y = 3; x = y;
Example int x, y; x = 5; y = 3; x = y;
5
Execution
Example int x, y; x = 5; y = 3; x = y;
5
Execution
Example int x, y; x = 5; y = 3; x = y;
3
Execution
variable = expression
the
Then the result is stored in the variable
Value
of the expression is computed first
Example
= 5 + 10
15
Equivalent
int a; a = 5;
int b, c; b = c = 3;
int d; d = -10;
#include <iostream> using namespace std; int main( void ) { int value, x, y; value = 4; x = 3; y = x * x; y = y / value; cout << y << endl; return 0; }
A) 2 B) 4 C) 6 D) none of the above
double x; x = 11 / 5; x = 11.0 / 5;
double x;
x = 11 / 5; x = 11.0 / 5; //x will have value 2.0 //x will have value 2.2
double variable
int value
12.0
value = 12;
int value
double variable
int variable
double value
value = 3.67;
double value
int variable
int main( void ) { double x = 6.4; int a = x; cout << a << endl; return 0; }
A) B) C) D)
Implicit Conversion
Happens automatically
Explicit Conversion
Using the casting operator: (type)
old form
new form
Examples
2.0
y x
int i = 1, j = 3;
double x = i / j;
// x 0.0
int i = 1, j = 3;
double x = i / j;
// x 0.0
int i = 1, j = 3;
double x = i / j;
//x 0.0
x = (double)i / (double)j;
// x 0.0
int i = 1, j = 3;
double x = i / j;
//x 0.0
int i = 1, j = 3;
double x = i / j;
//x 0.0
x = (double)i / (double)j; //x0.33 x = (double)i / j; //x0.33 x = i / (double)j; //x0.33 x = (double)(i / j); //x 0.0
double x; x = 1000 * 1000 * 1000 * 1000 * 0.5; cout << x << endl;
value = int(3.67)
x = float(4)
i = 5; j = 0; x = i / j;
// syntax error
// syntax error
i = 5; j = 0; x = i / j;
// run-time error
double x = 3.7;
i = (int) (x); i = (int) (x + 0.5); (rounds x) // i 3 // i 4
double x = 3.7;
i = (int) (x); i = (int) (x + 0.5); (rounds x) // i 3 // i 4
double x = 3.7;
i = (int) (x); i = (int) (x + 0.5); // i 3 // i 4
double x = 3.7;
i = (int) (x); i = (int) (x + 0.5); // i 3 // i 4
From the following list, circle the statements that would be legal if these lines were in a single main program
int x = 34.5; bool boo = x; int g = 17; int y = g; y = y + 10; int s; s = y; int b = 3; int v = b; int n = 12; v = n; double k = 1000 * 1000 * 1000 * 90 * .5; double y = 9.5; int p = 3 * g + y;
Across 3. Can't pin it down 4. Acronym for a chip 7. What's a prompt good for? 8. Just gotta have one 10. RUN 13. You're never going to change!!! 14. Could be called "Father" Down 1. Quite a crew of characters 2. Not an integer (or ____ your boat) 3. Nothing is there 5. Source code consumer 6. Acronym for your laptop's power 9. Announce a new variable 11. Number variable type 12. Department of LAN jockeys 13. Say something
202