Upload
neil-lane
View
216
Download
0
Tags:
Embed Size (px)
Citation preview
LECTURE 4:ASSIGNMENTS
PHY 107 – Programming For Science
Announcements
Memorization is not important, but… … you will all still be responsible for
information Instead use your resources: notes, books,
slides, me, ... Via D2L can now get weekly assignment
#2
The Week’s Goal
At end of this week’s lecture, you will be able to
Write (small, useless) C programs
Variable Declarations
Variables must be declared before can be used Way of getting computer to make space for
variable States how to interpret memory in future
uses Allows the compiler to check if uses are
legal
Variables, Constants, & More
General Cases Examples
Variable DeclarationdataType name;dataType name = value;dataType name, anotherName;dataType name = value, anotherName;
int count;bool monkey = true;char help,letter;char a=‘a’,letter;
Constant Declarationconst dataType NAME= value; const double PI=3.1;
Symbolic Constant #define NAME value #define AGE 34
Data Types
Each variable also has data type How program treats variable’s value
defined by this Single true or false value held by bool C/C++ defines 7 numeric data types
Integer types: short, int, long, long long Decimal types: float, double, long double
char data type used to store a character
Variable Names
Begin with letter or underscore (_) Then use any letters, numbers, or
underscore Unique name* needed for each variable
Computer wouldn't know which of 1,000 bobs to use
Reserved words are… reserved and can't be used Reserved words includes void, unsigned, class
Today’s Excitement!
Spent entire day on it, but why create variables? Want to do something with variable
declared C/C++ has large variety of statements to
do this First statement covered in today’s lecture
Assignments
Assignments
Variable declaration creates “box” to store data Box can get values placed in it using
assignments General form of assignment is
variable = expression; Computer works by first evaluating
expression Single value must result from this
expression Once computed, variable’s value is that
result
What Is The Expression?
Simplest expressions are literal values
Examples:double doe;int re;char me;doe = 6;re = 7;doe = -7;doe = 34.5691;me = 'a';me = '0';
12 56 12.345 -56 ‘a’
Data Types
Assignments are legal only if always safe C/C++ defines ordering of legal
assignments
long doubledoublefloatlongintshortchar
Lega
l to
assi
gn to
hig
her
type
What Is The Expression?
Examples of other simple expressionsdouble fa;int so;char la; so = 6;so = 7;fa = -so;so = fa;fa = 34.5691;so = fa;la = 48;so = la;la = so;
What Is The Expression?
Examples of other simple expressionsdouble fa;int so;char la; so = 6;so = 7;fa = -so;so = fa;fa = 34.5691;so = fa; la = 48; so = la;la = so;
What Is The Expression?
Examples of other simple expressionsdouble fa;int so;char la; so = 6;so = 7;fa = -so;so = fa;fa = 34.5691;so = fa;la = 48; so = la;la = so;
What Is The Expression?
Examples of other simple expressionsdouble fa;int so;char la; so = 6;so = 7;fa = -so;so = fa;fa = 34.5691;so = fa;la = 48; // ASCII 48 = '0' so = la;la = so;
What Is The Expression?
Can also include basic arithmetic operators Addition + i = 4 + 6; Subtraction - d = i – 2.3; Multiplication * i = 120 * 8; Division / d = 4.0 / i; Modulus % i = 120 % 8;
Modulus computes remainder between two integers:4 % 5 equals 45 % 4 equals 19 % 3 equals 012823 % 812 equals 643
Tracing A Program
Important for understanding & debugging code Step-by-step execution of program shown To see what is happening, done via pencil-
and-paper Execute each line of program like
computer does Within trace, add row whenever variable
declared Update variable’s value each time it is
assigned Off to side, show output from printf
statements
Program Trace
int main() { int x = 4 + 2; int y = 8 * 1; double z = y – 3; x = x + 1; y = 7 % x; z = y + 1.0 / 2.0; z = 8.0 / 4 + x * x; y = (x – 3) * (y + 2); y = x / 4; return 0;
}
Integer Division
Dividing two integers computes an integer Literals or variables does not matter, only
their type Important to remember, 12 is integer & 12.0 is not
C/C++ ignores result after decimal to get integer2 / 5 equals 0 (the .4 was thrown away) 5 / 2 equals 2 (the .5 was thrown away)16 / 4 equals 4 -5 / 2 equals -2 (the .5 was thrown away)2.0 / 5 equals 0.4 (2.0 is not an integer!)2 / 5.0 equals 0.4 (5.0 is not an integer!)
Floating Point Arithmetic
Operations using decimal has decimal result Even if whole number is result of the
operation For example, all these assignments to i are
illegal:int i;double d = i;i = 6.0 / 3.0;i = 2.0 * d;i = d + 1;i = 4 * 2.0;i = (d * 1) + 5;i = 8 + (9 * 3) – (2 / 1.0) * 4 + 2;
Priority of Operations
Equations can become very complex 4 + 5 * 6 * 9 - 2 + 1 = …?
Very strict order of operations used by computer ( ) Solve from inner- to
outermost + (positive) & - (negative) Solve from right to left * & % & / (division) Solve from left to right + (addition) & - (subtraction) Solve from left to
right
My suggestion: use lots of parentheses
Compound Assignment Operators Short simple operators that allow us to
be lazy Save some typing for several common
actions Lowest priority operation; expression
evaluated first
Operator Equivalent C/C++ Expression
a += 2; a = a + 2;b -= d; b = b – d;c *= 4 + 5.6; c = c * (4 + 5.6);d /= 0.3 * e; d = d / (0.3 * e);
How To Shoot Yourself in Foot Also increment (++) & decrement (--)
operators Use with variables only; no exceptions
possible Used anywhere to save typing an additional
line Two different ways these operators
applied
v = ++b % c; b = b + 1;v = b % c;
c = f * --h; h = h – 1;C = f * h;
a = b++ * c; a = b * c;b = b + 1;
Your Turn
Work on activity in groups until 11:45 Each group will submit 1 copy at end of
class Professor chooses the copy; so must work
together
For Next Lecture
Read pages 69 - 86 for Friday What is this printf thing, anyway? How is it
used? Can we read in input from the keyboard?
How?
Week #2 weekly assignment already posted Next assignment will be due on Tuesday at
5PM Problem #1 not from today; only time this
happens