Professional Documents
Culture Documents
LEC4
LEC4
– cout <<x;
– cout <<x+x;
– cout << “x=:”;
– cout << “x= “<<x;
– cout <<x+y<<“=“<<“x+y”;
– z=x+y;
– cin >>x>>y;
– //cout <<“x+y =“<<x+y;
– cout<<“\n;”;
applications
Given the algebraic equation y=ax3 + 7,
which of the following if any, are correct
C++ statements for this equations
– y=a*x*x*x+7
– y=a*x*x*(x+7);
– y=(a*x)*x*(x+7);
– y=(a*x)*x*x+7;
– y=a*(x*x*x)+7;
– y=a*x*(x*x+7);
Applications
Write a program that inputs 3 integers from
the keyboard and prints the sum, average,
product, smallest and largest of these
numbers. The screen dialogue should
appear as follows:
Input three different integers: 13 27 14
Sum is 54
Average is 18
Product is 4914
Smallest is 3
Largest is 27
Applications
Write a program that reads in the radius of a
circle and prints the circle’s diameter,
circumference and area. Use the constant
value 3.14159 for П. Do these calculations
in output statements. (note use only integer
values for the radius for the moment)
What does the following code print?
– cout <<“*\n**\n***\n****\n*****\n”;
Write a program that reads an integer and
determines and prints whether it is odd or
even.
Write a program that reads in two integers
and determines and prints if the first is a
multiple of the second or not.
Using only the techniques we learned until
now, write a program that calculates the
squares and the cubes of the numbers from
0 to 6 and uses tabs to print the following.
Number square cube
0 0 0
1 1 1
2 4 8
3 9 27 Try to use 1 var only
4 16 64
5 25 125
6 36 216
Chapter 2
Control Structures
Writing a program understand the
problem
– Actions to be executed
– Order of actions
– Algorithm: Set of
actions + logical order
Pseudocode
Artificial, informal language for developing
algorithms
Not a PL
Pseudocodes not executables
Transfer of control
– Sequence structure
– Selection structure
– Repetition structure
Sequence structure
Built in C++
C++ statements one after another (unless
told otherwise)
Example of a sequence structure
if selection structure
if/else structure
Do/While
For
true
Grade>=60 Print “Passed”
false
Diamond: Decision symbol
Similar to:
False true
Grade >= 60
Example
int product = 2;
while ( product <= 1000 )
product = 2 * product;
The while Repetition Structure
Flowchart of while loop
true
product <= 1000 product = 2 * product
false
Values of product: 4, 8, 16, 32, 64, 128,
256, 512, 1024.
9 executions, 10 evaluations.