You are on page 1of 16

CHAPTER 2

Exercises 2.1 1. State if the following are valid function names. If they are valid, state if they are mnemonic names. (Recall that a mnemonic function name conveys some idea about the functions purpose). If they are invalid names, state why. a) b) c) d) e) f) g) h) i) j) k) l) m) n) o) p) q) r) s) t) u) v) w) x) power valid , mnemonic total valid , mnemonic volts$ - invalid, violates Rule 2; contains a special character. cosine valid , mnemonic density valid , mnemonic tangent valid , mnemonic a2B3 valid , not mnemonic speed valid , mnemonic m1234 valid , not mnemonic absval valid , mnemonic while - invalid, violates Rule 3; is a keyword. netdistance valid , mnemonic newamp valid , mnemonic computed valid , not mnemonic b/c it doesnt indicate what is computed. minval valid , mnemonic sum valid , mnemonic 1234 - invalid, violates Rule 1; starts w/a number. b34a valid , not mnemonic sine valid , mnemonic return - invalid, violates Rule 3; is a keyword. abcd valid , not mnemonic 34ab - invalid, violates Rule 1; starts w/a number. $sine - invalid, violates Rule 1; does not start w/a letter or underscore. Stack valid , could be mnemonic

2. Assume that the following functions have been written: getLength(), getWidth(), calcArea(), displayArea()

a) From the functions names, what do you think each function might do? These functions might be used to get the value for a rectangle length and width, calculate the value of a rectangle area and then display the area value on the screen. b) In what order do you think a main() function might execute these functions (based on their names)? The order of execution should be: i) ii) getLength() getWidth()

iii) calcArea() iv) displayArea()

Prepared by: Miss Azlina Din

3. Assume that the following functions have been written: speed(), distance(), acceleration()

From the functions names, what do you think each function might do? These functions might be used to determine the speed, distance, and acceleration achieved by some object, such as a car. 4. Determine names for functions that do the following: a) Find the average of a set of numbers. getNum(), calcTotal(), calcAve() or calcAve() only.

b) Find the area of a rectangle. getLength(), getWidth(), or calcArea() only.

calcArea()

c) Find the minimum value in a set of numbers. getNum(), detMinVal() or detMinVal() only.

d) Find the density of a steel door. getMass(), getVolume(), calcDensity() or calcDensity() only.

e) Sort a set of numbers from lowest to highest. getNum(), detLowVal(), changeLocLowToHigh() or sortNumLowToHigh() only.

5. Just as the keyword int is used to signify that a function will return an integer, the keywords void, char, float, and double are used to signify that a function will return no value, a character, a single-precision number, and a double-precision number, respectively. Using this information, write header lines for main() function that will receive no arguments but will return: a) no value void main() b) a character char main() c) a single-precision number float main() d) a double-precision number double main()

Prepared by: Miss Azlina Din

6. Using cout, write a C++ program that displays your name on one line, your street address on a second line, and your city, state, and zip code on a third line. Run the program you have written on a computer. #include <iostream> using namespace std; int main() { cout << Ali Bin Abu\n << No 1 Jln Delima II\n << Tampin, Negeri Sembilan, 12345\n; return 0; }

7. Write a C++ program to display the following: The cosecant of an angle is equal to one over the sine of the angle. Compile and run the program you have written on a computer. #include <iostream> using namespace std; int main() { cout << The cosecant of an angle\n << is equal to one over\n << the sine of the angle.\n; return 0; }

8. a)

How many cout statement would you use to display the following: Degrees 0 90 180 270 360 1 or 6 Radians 0.0000 1.5708 3.1416 4.7124 6.2832

b)

What is the minimum number of cout statements that could be used to print the table in Exercise 8a? 1

Prepared by: Miss Azlina Din

c)

Write a complete C++ program to produce the output illustrated in Exercise 8.a). #include <iostream> using namespace std; int main() { cout << << << << << <<

Degrees 0 90 180 270 360

Radians\n 0.0000\n 1.5708\n 3.1416\n 4.7124\n 6.2832\n;

return 0; }

d)

Run the program you have written for Exercise 8.c) on a computer.

9. Assuming a case-insensitive compiler, determine which of these program unit names are equivalent: AVERAG Total denSTY averag besseL MEAN MODE TeMp total BESSEL Densty mean Mode TEMP moDE

AVERAG = averag Total = total denSTY = Densty besseL = BESSEL MEAN = mean MODE = Mode = moDE TeMp = TEMP

Exercises 2.2 1. Will the following program work? #include <iostream> using namespace std; int main() {cout << Hello there world!; return 0;} Yes, however the good programming style is not applied.

2. Rewrite the following programs to conform to good programming practice and correct syntax. a) #include <iostream> int main( ){ cout << The time has come ; return 0;} #include <iostream> using namespace std; int main() { cout << The time has come; return 0; }

Prepared by: Miss Azlina Din

b) #include <iostream> using namespace std; int main ( ) {cout << Newark is a city\n; cout << In New Jersey\n; cout << It is also a city\n ; cout << In Delaware\n ; return 0;} #include <iostream> using namespace std; int main() { cout << cout << cout << cout <<

Newark is a city\n; In New Jersey\n; It is also a city\n; In Delaware\n;

return 0; }

c) #include <iostream> using namespace std; int main() {cout << Reading a program\n;cout << is much easier\n ; cout << if a standard form for main is used\n) ; cout << and each statement is written\n;cout << on a line by itself\n) ; return 0;} #include <iostream> using namespace std; int main() { cout << cout << cout << cout << cout <<

Reading a program\n; is much easier\n; if a standard form for main is used\n; and each statement is written\n; on a line by itself\n;

return 0; }

Prepared by: Miss Azlina Din

d) #include <iostream.h> using namespace std; int main ( ){cout << Every C++ program ;cout <<\nmust have one and only one ; cout << main function ; cout << \n the escape sequence of characters) ; cout << \nfor a newline can be placed anywhere ; cout << \n within the message passed to cout ; return 0;} #include <iostream> using namespace std; int main() { cout << cout << cout << cout << cout << cout <<

Every C++ program; \nmust have one and only one; main function; \n the escape sequence of characters; \nfor a newline can be placed anywhere; \n within the message passed to cout;

return 0; }

3. a)

When used in message, the backslash character alters the meaning of the character immediately following it. If we wanted to print the backslash character, we would have to tell cout to escape from the way it normally interprets the backslash. What character do you think is used to alter the way a single backslash character is interpreted? Character backslash \

b)

Using your answer to Exercise 3.a), write the escape sequence for printing a backslash.

#include <iostream> using namespace std; int main() { cout << \\; return 0; }

Prepared by: Miss Azlina Din

Exercises 2.3 1. Determine data types appropriate for the following data: a) the average of four grades double or float b) the number of days in a month int c) the length of the Penang Bridge double or float d) the numbers in a state lottery int e) the distance from Merlimau, Malacca to Arau, Perlis. double or float

2. Modify the following program to determine the storage used by your compiler for all C++s integer data types. #include <iostream> using namespace std; int main() { cout << \nData Type Bytes << \n--------- ----- << \nint <<sizeof(int) << \nchar <<sizeof(char) << \nbool <<sizeof(bool) << \n; return 0; } #include <iostream> using namespace std; int main() { cout << "\nData Type << "\n--------<< "\nint << "\nchar << "\nbool << "\nshort int << "\nlong int << "\nunsigned short int << "\nunsigned int << "\nunsigned char << "\nunsigned long int << '\n'; return 0; }

Bytes" -----" " <<sizeof(int) " <<sizeof(char) " <<sizeof(bool) " <<sizeof(short int) " <<sizeof(long int) " <<sizeof(unsigned short int) " <<sizeof(unsigned int) " <<sizeof(unsigned char) " <<sizeof(unsigned long int)

Prepared by: Miss Azlina Din

3. Show how the name KINGSLEY would be stored inside a computer that uses the ASCII code. That is, draw a figure similar to the following for the name KINGSLEY.

8 bytes of storage 01001011 01001001 01001110 01000111 01010011 01001100 01000101 01011001 -------- -------- -------- -------- -------- -------- -------- -------K I N G S L E Y

4. Although we have concentrated on operations involving integer and floating-point numbers, C++ allows characters and integers to be added or subtracted. This can be done because a character is stored using an integer code (it is an integer data type.) Thus, characters and integers can be freely mixed in arithmetic expressions. For example, if your computer uses ASCII code, the expression a + 1 equals b, and z 1 equals y. Similarly, A + 1 is B, and Z 1 is Y. With this as background, determine the character results of the following expressions. (Assume that all characters are stored using the ASCII code.) a) m 5 = h b) m + 5 = r c) G + 6 = M d) G 6 = A e) b a = 1 or char (soh) f) g a + 1 = 7 or char (bel) g) G A + 1 = 7 or char (bel)

Exercises 2.4 1. Listed below are correct algebraic expressions and incorrect C++ expressions corresponding to them. Find the errors and write corrected C++ expressions. Algebra C++ Expression a) (2)(3) + (4)(5) (2)(3) + (4)(5) (2)*(3) + (4)*(5) b) 6 + 18 -----2 c) 4.5 ---------12.2 3.1 d) 4.6(3.0 + 14.9) 6 + 18 / 2 (6 + 18) / 2 4.5 / 12.2 3.1 4.5 / (12.2 3.1) 4.6(3.0 + 14.9) 4.6 * (3.0 + 14.9) e) (12.1 + 18.9)(15.3 3.8) (12.1 + 18.9)(15.3 3.8) (12.1 + 18.9)*(15.3 3.8)

Prepared by: Miss Azlina Din

2.

Determine the value of the following integer expressions: a) 3 + 4 * 6 3 + 24 27 b) 3 c) 2 * 4 / 6 + 6 12 / 6 + 6 2 + 6 8 * 6 0 0 0 3 / * / / 12 * 8 / 4 12 * 8 / 4 8 / 4 4

f) 20 2 / (6 + 3) 20 2 / 9 20 - 0 20 g) (20 2) / 6 + 3 18 / 6 + 3 3 + 3 6 h) (20 2) / (6 + 3) 18 / (6 + 3) 18 / 9 2 i) 50 % 20 10

d) 10 * (1 + 7 * 3) 10 * (1 + 21) 10 * 22 220 e) 20 2 / 6 + 3 20 0 + 3 20 + 3 23 3.

j) (10 + 3) % 4 13 % 4 1

Determine the value of the following floating-point expressions: a) 3.0 + 4.0 * 6.0 3.0 + 24.0 27.0 b) 3.0 * 4.0 / 6.0 + 6.0 12.0 / 6.0 + 6.0 2.0 + 6.0 8.0 c) 2.0 * 6.0 0.5 4.0 1.0 3.0 / 12.0 * 8.0 / 4.0 / 12.0 * 8.0 / 4.0 * 8.0 / 4.0 / 4.0

d) 10.0 * (1.0 + 7.0 * 3.0) 10.0 * (1.0 + 21.0) 10.0 * 22.0 220.0 e) 20.0 2.0 / 6.0 + 3.0 20.0 0.33 + 3.0 19.67 + 3.0 22.67

Prepared by: Miss Azlina Din

f) 20.0 2.0 / (6.0 + 3.0) 20.0 2.0 / 9.0 20.0 0.22 19.78 g) (20.0 2.0) / 6.0 + 3.0 18.0 / 6.0 + 3.0 3.0 + 3.0 6.0 h) (20.0 2.0) / (6.0 + 3.0) 18.0 / (6.0 + 3.0) 18.0 / 9.0 2.0

4.

Evaluate the following mixed-mode expressions and list the data type of the result. In evaluating the expressions, be aware of the data types of all intermediate calculations. a) 10.0 + 15 / 2 + 4.3 10.0 + 7 + 4.3 17.0 + 4.3 21.3 b) 10.0 + 15.0 / 2 + 4.3 10.0 + 7.5 + 4.3 17.5 + 4.3 21.8 c) 3.0 * 4 / 6 + 6 12.0 / 6 + 6 2.0 + 6 8.0 d) 3.0 * 4.0 / 6 + 6 12.0 / 6 + 6 2.0 + 6 8.0 e) 20.0 2 / 6 + 3 20.0 - 0 + 3 20.0 + 3 23.0 f) 10 + 17 * 3 + 4 10 + 51 + 4 61 + 4 65 g) 10 + 17 / 3. + 4 10 + 5.67 + 4 15.67 + 6 21.67 h) 3.0 * 4 % 6 + 6 12.0 % 6 + 6 0 + 6 6 i) 10 + 17 % 3 + 4. 10 + 2 + 4. 12 + 4. 16.0

5.

Assume that amount stores the integer value 1, m stores the integer value 50, n stores the integer value 10, and p stores the integer value 5. Evaluate the following expressions: a) n / p + 3 10 / 5 + 3 2 + 3 5 e) 18 / p 18 / 5 3 f) p * n -5 * 10 -50

Prepared by: Miss Azlina Din

10

b) m / p 50 10 10 20 10 c) m 3 50 50 50 20 24

+ / + +

n 10 * amount 5 + 10 10 * 1 10 10 * 1 10 10 10

g) m / 20 -50 / 20 -2 h) (m + n) / (p + amount) (50 + 10) / (5 + 1) 60 / (5 + 1) 60 / 6 10 / + + + p + amount 10 / 5 + 1 2 + 1 1

* +

n + 4 * amount 3 * 10 + 4 * 1 30 + 4 * 1 30 + 4 4

d) amount / 5 1 / 5 0

i) m + n 50 50 52 53

6.

Repeat Exercise 5, assuming that amount stores the value 1.0, m stores the value 50.0, n stores the value 10.0, and p stores the value 5.0. a) n / p + 3 10.0 / 5.0 + 3.0 2.0 + 3.0 5.0 b) m c) m / p + n 10 * amount 50.0 / 5.0 + 10.0 10.0* 1.0 10.0 + 10.0 10.0 * 1.0 10.0 + 10.0 10.0 20.0 10.0 10.0 3 * n + 4 * amount 50.0 3.0 * 10.0 + 4.0 * 1.0 50.0 30.0 + 4.0 * 1.0 50.0 30.0 + 4.0 20.0 + 4.0 24.0 e) 18 / p 18.0 / 5.0 3.6 f) p * n -5.0 * 10.0 -50.0 g) m / 20 -50.0 / 20.0 -2.5 h) (m + n) / (p + amount) (50.0 + 10.0) / (5.0 + 1.0) 60.0 / (5.0 + 1.0) 60.0 / 6.0 10.0 p + + + + amount 10.0 / 5.0 + 1.0 2.0 + 1.0 1.0

d) amount / 5 1.0 / 5.0 0.2

i) m + n / 50.0 50.0 52.0 53.0

Prepared by: Miss Azlina Din

11

7.

Determine the output of the following program: #include <iostream> using namespace std; int main() //a program illustrating integer truncation { cout << answer1 is the integer << 9 / 4; cout << answer2 is the integer << 17 / 3; return 0; }

answer1 is the integer 2answer2 is the integer 5


8. Determine the output of the following program: #include <iostream> using namespace std; int main() //a program illustrating the % operator { cout << The remainder of 9 divided by 4 is << 9 % 4; cout << The remainder of 17 divided by 3 is << 17 % 3; return 0; } The remainder of 9 divided by 4 is 1The remainder of 17 divided by 3 is 2 9. Write a C++ program that displays the results of the expressions 3.0 * 5.0, 7.1 * 8.3 2.2, and 3.2 / (6.1 * 5). Calculate the value of these expressions manually to verify that the displayed values are correct.

#include <iostream> using namespace std; int main() { cout << The results of the expressions 3.0 * 5.0 is << 3.0 * 5.0; cout << \nThe results of the expressions 7.1 * 8.3 2.2 is << 7.1 * 8.3 2.2; cout << \nThe results of the expressions 3.2 / (6.1 * 5) is << 3.2 / (6.1 * 5); return 0; }
10. Write a C++ program that displays the results of the expressions 15 / 4, 15 % 4, and 15 * 3 - (6 * 4). Calculate the value of these expressions manually to verify that the displayed values are correct.

#include <iostream> using namespace std; int main() { cout << The results of the expressions 15 / 4 is << 15 / 4; cout << \nThe results of the expressions 15 % 4 is << 15 % 4; cout << \nThe results of the expressions 15 * 3 - (6 * 4) is << 15 * 3 - (6 * 4); return 0; } Prepared by: Miss Azlina Din 12

Exercises 2.5 1. State whether the following variable names are valid or not. If they are invalid, state the reason why. prod_a - valid newamp valid 9ab6 - Invalid, starts w/a number c1234 valid watts - valid sum.of - Invalid, contains a special character abcd - valid $total Invalid, begins w/a special character average - valid _c3 - valid new$al - Invalid, contains a special character volts1 - valid 12345 - Invalid, starts w/a number a1b2c3d4 - valid finvolt - valid

2. State whether the following variable names are valid or not. If they are invalid, state the reason why. Also indicate which of the valid variable names should not be used because they convey no information about the variable. current Valid harry Valid, but may not be very descriptive. maximum Valid 3sum - Invalid, starts w/a number a243 Valid, but may not be very descriptive. sue Valid, but may not be very descriptive. okay Valid, but may not be very descriptive. for Invalid, using keyword r2d2 Valid, but may not be very descriptive. c3p0 Valid, but may not be very descriptive. a Valid, but may not be very descriptive. tot.al - Invalid, contains a special character firstNum Valid total Valid

Prepared by: Miss Azlina Din

13

awesome Valid, but may not be very descriptive. c$five - Invalid, contains a special character cc_a1 Valid, but may not be very descriptive. sum Valid goforit Valid, but may not be very descriptive. netpower Valid

3.

a) Write a declaration statement to declare that the variable count will be used to store an integer. int count; b) Write a declaration statement to declare that the variable volt will be used to store a floating-point number. float volt; c) Write a declaration statement to declare that the variable power will be used to store a double-precision number. double power; d) Write a declaration statement to declare that the variable keychar will be used to store a character. char keychar;

4. Write declaration statements for the following variables: i) num1, num2, and num3 used to store integer numbers. int num1, num2, num3; j) amps1, amps2, amps3,and amps4 used to store double-precision numbers. double amps1, amps2, amps3, amps4; k) codeA, codeB, and codeC used to store character types. char codeA, codeB, codeC;

5. Rewrite each of these declaration statements as three individual declarations. a) int month, day = 30, year; int month; int day = 30; int year; b) double hours, volt, power = 15.62; double hours; double volt; double power = 15.62;

Prepared by: Miss Azlina Din

14

c) double price, amount, taxes; double price; double amount; double taxes; d) char inKey, ch, choice = f; char inKey; char ch; char choice = f;

6. Determine what each statement causes to happen in the following program and what is the output that will be printed when the program run. #include <iostream> using namespace std; int main() { int num1, num2, total; num1 = 25; num2 = 30; total = num1 + num2; cout << The total of << num1 << and << num2 << is << total << endl; return 0; } The total of 25 and 30 is 53

NOTE FOR EXERCISES 7 AND 8: Assume that a character requires one byte of storage, an integer four bytes, a single-precision number four bytes, a double-precision number eight bytes, and that variables are assigned storage in the order they are declared. Addresses 159 167 175 183 160 168 176 184 161 169 177 185 162 170 178 186 163 171 179 187 164 172 180 188 165 173 181 189 166 174 182 190

7. a) Using the above figure and assuming that the variable name rate is assigned to the byte having memory address 159, determine the addresses corresponding to each variable declared in the following statements. Also fill in the appropriate bytes with the initialization data included in the declaration statements. ( Use letters for the characters, not the computer codes that would actually be stored.)

Prepared by: Miss Azlina Din

15

float rate; char ch1 = M, ch2 = E, ch3 = L, ch4 = T; double taxes; int num, count = 0;

Addresses 159 160 161 162 163 M ch1 171 164 E ch2 172 165 L ch3 173 166 T
ch4

rate 167 168 taxes 175 176 num 183

169

170

174

177

178

184

185

186

179 180 0 count 187 188

181

182

189

190

b) Repeat exercise 7.a), but substitute the actual byte by patterns that a computer using the ASCII code would use to store the characters in the variables ch1, ch2, ch3, and ch4.

ch1 ch2 ch3 ch4

= = = =

01001101 01000101 01001100 01010100

8. Using the above figure and assuming that the variable name miles is assigned to the byte at memory address 159, determine the addresses corresponding to each variable declared in the following statements. float miles; int count, num; double dist, temp;

Addresses 159 160 161 162 163 164 165 166

miles 167 168 num 175

169

170

count 171 172 dist 179 180 temp 187 188

173

174

176

177

178

181

182

183

184

185

186

189

190

Prepared by: Miss Azlina Din

16

You might also like