Preview

Week1 Exercises

Satisfactory Essays
Open Document
Open Document
499 Words
Grammar
Grammar
Plagiarism
Plagiarism
Writing
Writing
Score
Score
Week1 Exercises
Don Petersen
Week 1 Exercises
12) Write C++ statements that accomplish the following:
a) Declare int variables x and y. Initialize x to 25 and y to 18. int x, y; x = 25; y = 18;
b) Declare and initialize an int variable temp to 10 and a char variable ch to ‘A’. int temp = 10; char ch = ‘A’;
c) Update the value of an int variable x by adding 5 to it. x += 5;
d) Declare and initialize a double variable payRate to 12.50. double payRate = 12.50;
e) Copy the value of an int variable firstNum into an int variable tempNum. tempNum = firstNum;
f) Swap the contents of the in variables x and y. (Declare additional variables, if necessary.) int z; z = x; x = y; y = z;
g) Suppose x and y are double variables. Output the contents of x, y, and the expression x + 12 /y -18. cout << “x = “ << x << “y = “ << y << “x + 12 / y – 18 = “ << x + 12 / y -18 endl;
h) Declare a char variable grade and set the value of grade to ‘A’. char grade; grade = ‘A’;
i) Declare int variables to store four integers. int a, b, c, d;
j) Copy the value of a double variable z to the nearest integer into an int variable x. x = int(z);
14) Suppose x, y, z and w are int variables. What value is assigned to each of these variables after the last statement executes? x = 5, y = 2, z = 3, w = 9

16) Suppose x, y, and z are int variables and x = 2, y = 5, and z = 6. What is the output of each of the following statements?
a) cout << “x = “ << x << ”, y = “ << y << “, z = << z << endl; x = 2, y = 5, z = 6
b) cout << “x + y = “ << x + y , endl; x + y = 7
c) cout << “Sum of “ << x << “ and “ << z << “ is “ << x + z << endl; Sum of 2 and 6 is 8
d) cout << “z / x = “ << z / x << endl; z / x = 3
e) cout << “2 times “ << x << “ = “ << 2 * x << endl; 2 times 2

You May Also Find These Documents Helpful