California State University, Northridge



|[pic] |College of Engineering and Computer Science |

| |Computer Science Department |

| |Computer Science 106 |

| |Computing in Engineering and Science |

| |Spring 2006 Class number: 11672 Instructor: Larry Caretto |

Homework Solutions – February 21, 2006

Page 98, Checkpoint 3.11 – Write C++ algebraic expressions for the following algebraic expressions.

The table below shows the algebra on the left and the C++ expression on the right. In some cases there is more than one possible way to compute the expression.

|[pic] |y = 6 * x; |

|[pic] |a = 2 * b + 4 * c; |

|[pic] |Y = x * x; or y = pow( x, 2 ) |

|[pic] |g = ( x + 2 ) / ( z * z ); or |

| |g = ( x + 2 ) / pow( z, 2 ); |

|[pic] |Y = x * x / ( z * z ) ; or y = x / z * x / z; or y = pow( x, 2 ) / pow( z, 2 ) ; or y = pow( x|

| |/ z, 2 ) |

Page 106, Checkpoint 3.14 – Assume the following variable definitions:

int a = 5, b = 12;

float x = 3.4, z = 9.1;

What are the values of the following expressions?

The text below shows the expressions from the problem statement in bold, on the left, intermediate results in the center, and the answers (and some rationale) in bold red italics on the right. The modern C++ syntax, static_cast or static_cast, is abbreviated below by the older (and still valid) use of the new type name only. Note that float, like double, is a floating point data type, but with fewer significant digits than double.

A) b / a b / a = 12 / 5 = 2 (integer division)

B) x * a x * a = 3.4 * 5 = 17 (int a = 5 promoted to float)

C) float(b/a) float(12/5) = float(2) = 2 (integer divide before conversion)

D) float(b)/a float(12)/5 = 2.4 (int a = 5 promoted to float)

E) b/float(a) 12/float(5) = 2.4 (int b = 12 promoted to float)

F) float(b)/float(a) float(12)/float(5) = 2.4

G) b / int(x) 12 / int(3.4) = 12 / 3 = 4 (conversion to int truncates)

H) int(x)*int(z) int(3.4)*int(9.1) = 3 * 9 = 27

I) int(x*z) int(3.4*9.1) = int(30.94) = 30

J) float(int(x)*int(z)) float(27) = 27 (see result from part H)

Page 153, Problem 8 – Complete the following table by writing the value of each expression in the Value column.

All numbers without a decimal point are an integer data type. This problem illustrates the rules for operator precedence.

Expression Value (Intermediate result)

28 / 4 – 2 5 7 - 2

6 + 12 * 2 - 8 22 6 + 24 - 8

4 + 8 * 2 20 4 + 16

6 + 17 % 3 - 2 6 6 + 2 - 2

2 + 22 * (9 – 7) 46 2 + 22 * 2 = 2 + 44

(8 + 7) * 2 30 15 * 2

(16 + 7) % 2 - 1 0 23 % 2 – 1 = 1 - 1

12 / (10 – 6) 3 12 / 4

(19 – 3) * (2 + 2) / 4 16 16 * 4 / 4 = 48 / 4

Page 162, Program 9 – A retail company must file a monthly sales tax report listing the sales for the month and the amount of sales tax collected. Write a program that asks for the month, the year, and the total amount collected at the cash register (that is, sales plus sales tax). Assume that the state sales tax is 4 percent and the county sales tax is 2 percent. If the total amount collected is known and the total sales tax is 6 percent, the amount of product sales may be calculated as S = T / 1.06, where S is the product sales and T is the total income (product sales plus sales tax). The program should print a report similar to

Month: October, 2006

--------------------

Total Collected: $ 26572.89

Sales: $ 25068.76

County Sales Tax: $ 501.38

State Sales Tax: $ 1002,75

Total Sales tax: $ 1504.13

The listing below shows one possible answer with the usual listing colors from Visual C++. The program uses the string library to enter the month as a character string. Although we mentioned this data type in class, we have not yet covered strings in detail in either the lecture or the laboratory.

#include

#include

using namespace std;

int main()

{

// use symbolic constants for tax rates

const double countyTaxRate = 0.02;

const double stateTaxRate = 0.06;

const double totalTaxRate = countyTaxRate + stateTaxRate;

// declare variables for program

double totalCollected; // Total amount collected (sales plus tax)

double sales; // Total sales income to store

int year; // Year for printing in output

string month; // Month for printing in output

// Get input from user

cout > month;

cout > year;

cout > totalCollected;

// Calculations and output; compute sales then output

// expressions for remaining calculated quantities

sales = totalCollected / ( 1 + totalTaxRate );

cout ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download