# 3. Central Processing Unit (CPU), Main Memory(MM), and Input/Output Devices

Chapter 1

1. a. false; b. false; c. true; d. false; e. false; f; false; g. false; h. true; i. true; j. false; k. true; l. false

2. The basic commands that a computer performs are input (get data), output (display result), storage, and performance of arithmetic and logical operations

3. Central processing unit (CPU), main memory(MM), and input/output devices.

4. Secondary storage permanently stores programs and data.

5.An operating system monitors the overall activity of the computer and provides services. Some of these services include memory management, input/output activities, and storage management.

6. The two types of programs are system programs and application programs.

7.In machine language the programs are written using the binary codes while in high-level language the program are closer to the natural language. For execution, a high-level language program is translated into the machine language while a machine language need not be translated into any other language.

8.A program written in a high-level language is called a source program.

9. Because the computer cannot directly execute instructions written in a high-level language, a compiler is needed to translate a program written in high-level language into machine code.

10.A compiler reports syntax errors.

11. Every computer directly understands its own machine language. Therefore, for the computer to execute a program written in a high-level language, the high-level language program must be translated into the computer’s machine language.

12. Instructions in a high-level language are closer to a natural language, such as English, and therefore are easier to understand and learn than machine language.

13.In linking an object program is combined with other programs in the library, used in the program, to create the executable code.

14. A well-analyzed problem leads to a well-designed algorithm. Moreover, a program that is well analyzed is easier to modify as well as spot and fix errors.

15. To find the weighted average of the four test scores, first you need to know each test score and its weight. Next, you multiply each test score with its weight, and then add these numbers to get the average. Therefore,

- Get testScore1, weightTestScore1
- Get testScore2, weightTestScore2
- Get testScore3, weightTestScore3
- Get testScore4, weightTestScore4
- weightedAverage = testScore1 * weightTestScore1 +

testScore2 * weightTestScore2 +

testScore3 * weightTestScore3 +

testScore4 * weightTestScore4;

16. a. Get quarters

b. Get dimes

c. Get nickels

d. Get pennies

e. changeInPennies =quarters * 25 + dimes * 10 + nickels * 5

+ pennies

17. To find the price per square inch, first we need to find the area of the pizza. Then we divide the price of the pizza by the area of the pizza. Letradius denote the radius and area denote the area of the circle, and price denote the price of pizza. Also, let pricePerSquareInch denote the price per square inch.

a. Get radius

b. area = π * radius * radius

c. Get price

d. pricePerSquareInch = price / area

18. To calculate the selling price of an item, we need to know the original price (the price the store pays to buy) of the item. We can then the use the following formula to find the selling price:

sellingPrice = (originalPrice + originalPrice × 0.80) × 0.90

The algorithm is as follows:

- Get originalPrice
- Calculate the sellingPrice using the formula:

sellingPrice = (originalPrice + originalPrice × 0.80) × 0.90

The information needed to calculate the selling price is the original price and the marked-up percentage.

19.To calculate the area of a triangle using the given formula, we need to know the lengths of the sides―a, b, and c―of the triangle. Next, we calculate s using the formula:

s = (1/2)(a + b + c)

and then calculate the area using the formula:

area = sqrt(s(s-a)(s-b)(s-c))

where sqrt denotes the square root.

The algorithm, therefore, is:

a.Get a, b, c

b.s = (1/2)(a + b + c)

c.area = sqrt(s(s-a)(s-b)(s-c))

The information needed to calculate the area of the triangle is the lengths of the sides of the triangle.

20. Suppose that billingAmount denotes the total billing amount, numOfItemsOrdered denotes the number of items ordered, shippingAndHandlingFee denotes the shipping and handling fee, and price denotes the price of an item. The following algorithm computes and outputs the billing amount.

a. Enter the number of items bought.

b. Get numOfItemsOrdered

c. billingAmount = 0.0;

d. shippingAndHandlingFee = 0.0;

e. Repeat the following for each item bought.

i. Enter the price of the item

ii. Get price

iii. billingAmount = billingAmount + price;

f. if billingAmount < 200

shippingAndHandlingFee = 10 * numOfItemsOrdered;

g. billingAmount = billingAmount + shippingAndHandlingFee

i. Print billingAmount

21.Suppose that numOfPages denoes the number of pages to be faxed and billingAmount denotes the total charges for the pages faxed. To calculate the total charges, you need to know the number of pages faxed.

If numOfPages is less than or equal to ten, the billing amount is services charges plus (numOfPages × 0.20); otherwise, billing amount is service charges plus 10 × 0.20 plus (numOfPages - 10) × 0.10. That is,

You can now write the algorithm as follows:

a. Get numOfPages.

b.Calculate billing amount using the formula:

if (numOfPagesis less than or equal to 10)

billingAmount = 3.00 + (numOfPages × 0.20);

otherwise

billingAmount = 3.00 + 10 × 0.20 + (numOfPages - 10) × 0.10;

22.Suppose amountWithdrawn denotes the amount to be withdrawn, serviceCharge denotes the service charges, if any, and accountBalance denotes the total money in the account.

You can now write the algorithm as follows:

a. Get amountWithdrawn.

b.ifamountWithdrawn > 500

Print "The maximum amount that can be drawn is $500"

otherwise (if accountBalance <= 0)

Print "Account balance is <= 0. You cannot withdraw any money."

Otherwise

{

if (amountWithdrawn > accountBalance)

{

Print "Insufficient balance. If you withdraw, services charges

will be $25.00. Select Yes/No."

if (Yes)

{

if (amountWithdrawn > 300)

serviceCharge = (amountWithdrawn – 300) * 0.04;

otherwise

serviceCharge = 0;

accountBalance = accountBalance – amountWithdrawn

- serviceCharge – 25;

Print "Collect your money."

}

}

othwewise

{

if (amountWithdrawn > 300)

serviceCharge = (amountWithdrawn – 300) * 0.04;

otherwise

serviceCharge = 0;

accountBalance = accountBalance – amountWithdrawn

- serviceCharge;

Print "Collect your money."

}

23.Suppose averageTestScore denotes the average test score, highestScore denotes the highest test score, testScore denotes a test score, sum denote the sum of all the test scores, and count denotes the number of students in class, and studentName denotes the name of a student.

a. First you design an algorithm to find the average test score. To find the average test score, first you need to count the number of students in the class and add the test score of each student. You then divide the sum by count to find the average test score. The algorithm to find the average test score is as follows:

i. Set sum and count to0.

ii. Repeat the following for each student in class.

1.Get testScore

2.Increment count and update the value of sum by adding the current test score to sum.

iii. Use the following formula to find the average test score.

if (count is 0)

averageTestScore = 0;

otherwise

averageTestScore = sum / count;

b. The following algorithm determines and prints the names of all the students whose test score is below the average test score.

Repeat the following for each student in class:

i.Get studentName and testScore

ii.

if (testScore is less than averageTestScore)

print studentName

c. The following algorithm determines and highest test score

i.Get first student’s test score and call it highestTestScore.

ii. Repeat the following for each of the remaining student in class

1.Get testScore

2.

if (testScore is greater than highestTestScore)

highestTestScore = testScore;

d. To print the names of all the students whose test score is the same as thehighest test score, compare the test score of each student with the highest test score and if they are equal print the name. The following algorithm accomplishes this

Repeat the following for each student in class:

i.Get studentName and testScore

ii.

if (testScore is equal to highestTestScore)

print studentName

You can use the solutions of the subproblems obtained in parts a to d to design the main algorithm as follows:

1.Use the algorithm in part a to find the average test score.

2.Use the algorithm in part b to print the names of all the students whose score is below the average test score.

3.Use the algorithm in part c to find the highest test score.

4.Use the algorithm in part d to print the names of all the students whose test score is the same as thehighest test score

Chapter 2

1.a. false; b. false; c. false; d. true; e. true; f. false; g. true; h. true; i. false; j. true; k. false

2.a, b, d, e, j

3.b, d, e

4.A keyword is a reserved word and is defined by the system. A keyword cannot be redefined in a program. A user-defined identifier can be redefined.

5.The identifiers firstName and FirstName are not the same. C++ is case sensitive. The first letter offirstName is lowercase f while the first character of FirstName is uppercase F. So these identifiers are different

6 a. 7

b.3

c.3

d.-2

e.4.4

f.25.5

g.26

h.21.75

7.a. 3

b.Not possible. Both the operands of the operator % must be integers. Because the second operand, w, is a floating-point value, the expression is invalid.

c. Not possible. Both the operands of the operator % must be integers. Because the first operand, which is y + w, is a floating-point value, the expression is invalid .

d.38.5

e. 1

f.2

g. 2

h.420.0

8. a, b, c, e, i, j, and k are valid;

d, f, and g are invalid because the left side of an expression must be a variable. h is invalid because the operands of the mod operator must be integers.

9.7

10. Variable declarations in Lines 2, 4, 5 and 6 are correct.

Variable declaration in Line 1 is incorrect because the left side of the assignment operator must be a variable, and the data type of the variable must be specified. A correct declaration is:

int age = 55; //Line 1

The variable declaration in Line 3 is incorrect because strings are enclosed in double quotation marks, and the semicolon at the end of the statement is missing.

string message = "First test is on Monday"; //Line 3

11.a and c are valid

12. a. int x, y;

x = 25;

y = 18;

b.int temp = 10;

charch = 'A';

c.x = x + 5;

d.double payRate = 12.5;

e.tempNum = firstNum;

f.temp = x;

x = y;

y = temp;

g.cout < x < " " < y < " " < x + 12 / y - 18 < endl;

h.char grade = 'A';

i.int num1, num2, num3, num4;

j.x = static_castint>(z + 0.5);

13.a.32 * a + b

b.'8'

c."Julie Nelson"

d. (b * b – 4 * a * c) / (2 * a)

e.(a + b)/c * (e * f) – g * h

f. (–b + (b * b – 4 * a * c)) / (2 * a)

14. x = 6

y = 29

z = 3

w = -10

15.x = 28

y = 35

z = 1

w = 22.00

t = 6.5

16.a. x = 2, y = 5, z = 6

b.x + y = 7

c.Sum of 2 and 6 is 8

d.z / x = 3

e. 2 times 2 = 4

17.a. 0.50

b.24.50

c.37.6

d.8.3

e.10

f.38.75

18.a. cout < endl; or cout < "\n"; or cout < '\n';

b. cout < "\t";

c.cout < "\"";

19.a and c are correct

20. a. firstName

b. discountedPrice

c. numOfJuiceBottles

d. milesTravelled

e. highestTestScore

21. a. int num1;

int num2;

b. cout < "Enter two numbers separated by spaces." < endl;

c.cin > num1 > num2;

d.cout < "num1 = " < num1 < "num2 = " < num2

< "2 * num1 – num2 = " < 2 * num1 – num2 < endl;

22. A correct answer is:

#include <iostream>

usingnamespace std;

constint TOP_NUM = 753409;

constdouble PAY_RATE = 18.35;

int main()

{

int testScore, projectScore;

double temp;

double payCheck;

int newTemp;

int first;

double hoursWorked;

testScore = 88;

projectScore = 22;

cout < testScore < " " < projectScore < endl;

temp = 82;

newTemp = testScore + 2 * projectScore;

first = 2 * TOP_NUM;

cout < first < " " < TOP_NUM < endl;

cout < "Enter hours worked: ";

cin > hoursWorked;

cout < endl;

payCheck = hoursWorked * PAY_RATE;

cout < "Wages = " < payCheck < endl;

return 0;

}

23. A correct answer is:

#include <iostream>

using namespace std;

constcharSTAR = '*';

constint PRIME = 71;

int main()

{

int count, sum;

double x;

int newNum; //declare newNum

count = 1;

sum = count + PRIME;

x = 25.67; // x = 25.67;

newNum = count * 1 + 2; //newNum = count * ONE + 2;

sum = sum + count; //sum + count = sum;

x = x + sum * count; // x = x + sum * COUNT;

cout < "count = " < count < ", sum = " < sum

< ", PRIME = " < PRIME < endl;

return 0;

}

24. A correct answer is:

#include <iostream>

#include <string>

usingnamespace std;

int main()

{

int temperature; // int temp;

string first, last; // string first;

cout < "Enter first name: "; // cout < "Enter first name: ";

cin > first; // cin > first

cout < endl;

cout < "Enter last name: "; // cout < "Enter last name: ;

cin > last;

cout < endl;

cout < "Enter today’s temperature: ";

cin > temperature;

cout < endl;

// cout < first < " " < last < today’s temperature is: ";

// < temperature < endl;

cout < first < " " < last < "today’s temperature is: "

< temperature < endl;

return 0;

}

25. An identifier must be declared before it can be used.

26. b.

27.a. x *= 2;

b.x += y - 2;

c.sum += num;

d.z *= x + 2;

e.y /= x + 5;

28. a. x = x + 5 – z;

b.y = y * (2 * x + 5 – z);

c.w = w + 2 * z + 4;

d.x = x – (z + y – t);

e.sum = sum + num;

29.

a b c

a = (b++) + 3;9 7 und

c = 2 * a + (++b); 9 8 26

b = 2 * (++c) – (a++);10 45 27

30.

a bcsum

sum = a + b + c; 3 5 14.1 22

c /= a; 3 54.7 22

b += c – a; 3 64.7 22

a *= 2 * b + c; 50 64.7 22

31. (The user input is shaded.)

a = 25

Enter two integers: 2015

The numbers you entered are 20 and 15

z = 45.5

Your grade is A

The value of a = 65

32. (The user input is shaded.)

Enter last name: Miller

Enter a two digit number: 34

Enter a positive integer less than 1000: 340

Name: Miller

Id: 3417

Mystery number: 3689

33.

#include <iostream>

#include <string>

usingnamespace std;

constdouble X = 13.45;

constint Y = 34;

constchar BLANK = ' ';

int main()

{

string firstName, lastName;

int num;

double salary;

cout < "Enter first name: ";

cin > firstName;

cout < endl;

cout < "Enter last name: ";

cin > lastName;

cout < endl;

cout < "Enter a positive integer less than 70: ";

cin > num;

cout < endl;

salary = num * X;

cout < "Name: " < firstName < BLANK < lastName < endl;

cout < "Wages: $" < salary < endl;

cout < "X = " < X < endl;

cout < "X + Y = " < X + Y < endl;

return 0;

}

34. The program requires four inputs in the following order:

string decimal_number decimal_number integer