COMP 218 - Fundamentals of Programming
Tutorial 2 Built in Data Types I
SOLUTION
Professor:
Nancy Acemian
Built in Data Types I Exercises 1
Built in Data Types I Exercises 2
Assignment and arithmetic operators:
Given the following declaration statement:
const double MAX = 100;
What type of error will the following statement cause and
why?
MAX = 200;
Answer:
Because MAX was declared as a constant. Once a
constant is declared and initialized, its value cannot be
changed.
Built in Data Types I Exercises 3
Assume the fillowing declaration:
int a, b, c;
Indicate the values of a, b and c after the following instructions:
instruction
value of a
value of b
value of c
a = 5;
b = 3;
c = a + b;
a = 7 + 3 * 7 / 2 - 1;
16
c = b - a;
16
-13
a = a + 2;
18
-13
b = 2 % 2 + 2 * 2 - 2 / 2;
18
-13
a = b = c + 10;
-3
-3
-13
Built in Data Types I Exercises 4
Arithmetic Operators:
To compute the sum S of the integers from 1 to n, we can
use the formula:
S = ((1+n)*n)/2
For instance, if n has value 10, then S is 55
(1 + 2 + + 9 + 10)
Assume the following declarations:
int s, n;
Answer:
S = (n*(n+1)) / 2;
Write a C++ statement to calculate S using the above
formula
Built in Data Types I Exercises 5
Solution:
1.
2.
3.
4.
5.
cout << The article is expensive !;
cout << qty;
cout << price << Thats expensive;
cout << cat << art;
cout << qty << articles of << art << $ cost << qty *
price << $;
6. cin >> qty >> price;