- LearnGroup

Download Report

Transcript - LearnGroup

• Draw the Flowchart and write the Pseudocode
for calculating the volume of material in a
pipe.
• An old-style movie theater has a simple profit
function. Each customer pays 5 pounds per
ticket. Every performance costs the theater 20
pounds, plus 0.50 pound per attendee. Draw
the Flowchart and write the Pseudocode for
calculating how much income the attendees
produce.
Converting from Fahrenheit to Celsius
• The program prompts the user to enter a
Fahrenheit temperature; it then prints the
equivalent Celsius temperature.
• Draw the Flowchart and write the Pseudocode
for solving two simultaneous linear equations
in two unknowns. The equations to be solved
are:
• Draw the Flowchart and write the Pseudocode for a weather
forecasting program that writes the text shown in the rows and
columns of the following table, depending on values of temperature
and pressure supplied by the user. Define high temperatures to be
above 20.0°C and high pressures to be above 1000,0 millibar.
High temperature
Low temperature
High pressure
Clear skies and hot
Clear skies and cold
Low pressure
Warm with rain
Snow
A type-safety violation
(“implicit narrowing”)
// Beware: C++ does not prevent you from trying to put a large value
// into a small variable (though a compiler may warn)
int main()
{
a
int a = 20000;
char c = a;
int b = c;
if (a != b)
// != means “not equal”
cout << "oops!: " << a << "!=" << b << '\n';
else
cout << "Wow! We have large characters\n";
}
20000
• Try it to see what value b gets on your machine
c:
???
A technical detail
• In memory, everything is just bits; type is what gives meaning
to the bits
(bits/binary) 01100001 is the int 97 is the char 'a'
(bits/binary) 01000001 is the int 65 is the char 'A'
(bits/binary) 00110000 is the int 48 is the char '0'
char c = 'a';
cout << c; // print the value of character c, which is a
int i = c;
cout << i;
// print the integer value of the character c, which is 97
• This is just as in “the real world”:
– What does “42” mean?
– You don’t know until you know the unit used
• Meters? Feet? Degrees Celsius? $s? a street number? Height in inches? …
8
Precedence
• i + j * k is equivalent to i + (j * k)
• -i * - j is equivalent to (-i) * (-j)
• +i + j / k is equivalent to (+i ) + (j / k)
Expressions
• formulate the following expressions as
programs:
– ab – c
– (m + n) (x + y)
– (ab / c)
– 3n2 +2n + 1
– n2 + 10
– (1/2) · n2 + 20
– 2 - (1/n)
The following program illustrates the effect of
presence of parenthesis in expressions
main ()
{
float a, b, c, x, y, z;
a = 9;
b = 12;
c = 3;
x = a - b / 3 + c * 2 - 1;
y = a - b / (3 + c) * (2 - 1);
z = a - ( b / (3 + c) * 2) - 1;
cout << x << '\n' ;
cout << y << '\n' ;
cout << z << '\n' ;
}
Output x = 10.00
y = 7.00
z = 4.00
Assignment Operators
• i = 5;
• j = i;
• k =10*i+ j ;
•
•
•
•
int i;
float f;
i=72.99f;
f=136;
• i = j = k = 0; // (i = (j = (k = 0)))
•
•
•
•
int i;
float f;
f=i=33.3f;
i is assigned the value 33, then f is assigned
33.0 (not 33.3. as you might think).
• i=i+2;
• i+=2;
•
•
•
•
•
v +=e adds v to e, storing the result in v
v -= e subtracts e from v, storing the result in v
v*= e multiplies v by e, storing the result in v
v/= e divides v by , storing the result in v
v%= e computes the remainder when v is divided by e,
storing the result in v
• i+=j+=k; // i+=(j+=k);
• a = b += c++ - d + --e / -f
• (a= (b += (((c++) – d) + ((--e) / (-f)))))
• Draw a Flowchart and write a Pseudocode for
a program that calculates the taxi fee. The fee
below 1500 meters is 5 pounds and 2 pounds
for each 500 meters above 1500.
• Draw a Flowchart and write a Pseudocode for
a program that finds the intersection between
two lines: ax + by = c and dx+ey=f.
Relational Operators
• They produce 0 (false) or 1 (true) when used
in expressions.
• For example, the value of 10 < 11 is 1: the
value of 11 < 10 is 0.
• i<j<k is legal in C, but doesn't have the
meaning that you might expect. Since the <
operator is left associative, this expression is
equivalent to (i<j)<k
• In other words, the expression first tests
whether i is less than j; the 1 or 0 produced by
this comparison is then compared to k. The
expression doesn’t test whether j lies between
i and k.
Equality Operators
• i<j==j<k
// (i < j) ==(j < k)
Logical Operators
• (i != 0) && (j / i > 0)
• To find the value of this expression, we must first
evaluate (i ! = 0) . If i isn‘t equal to 0, then we' ll
need to evaluate (j / i > 0) to determine whether
the entire expression is true or false. However, if i
is equal to 0, then the entire expression must be
false, so there's no need to evaluate (j / i > 0) .
• The advantage of short circuit evaluation is
apparent-without it. evaluating the expression
would have caused a division by zero,
• i > 0 && ++j > 0
• Although j is apparently incremented as a side
effect of evaluating the expression, that isn't
always the case.
• If i > 0 is false, then ++j > 0 is not evaluated, so
j isn‘t incremented.
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;
int average = (length+width)/2;
// a multiplication
// 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.
Stroustrup/Programming
24
Calculating a Broker's Commission
• The minimum charge is $39. The program asks
the user to enter the amount of the trade,
then displays the amount of the commission.