Solution Manual for Introduction to Programming with C++, 3E Y. Daniel Liang

Page 1


Name:_______________________

(50 minutes) (Chapters 1-8)

CSCI 2490 C++ Programming

Armstrong Atlantic State University

Instructor: Dr. Y. Daniel Liang

If you want to buy this or Any Other Test Bank or Solution Manual contact us At. Premiumbiz379@gmail.com

Part I:

(10 pts)

1. Write a function that finds the number of occurrences of a specified character in the string using the following header:

int count(const char s[], char a)

For example, count("Welcome", 'e') returns 2. Write a test program that reads a string and a character and displays the number of occurrences of the character in the string. Here is a sample run of the program:

<Output>

Enter a string: Welcome to C++

Enter a character: o o appears in Welcome to C++ 2 times <End Output>

(10 pts)

2 Two two-dimensional arrays m1 and m2 are strictly identical if their corresponding elements are equal. Write a function that returns true if m1 and m2 are strictly identical, using the following header:

#define SIZE 3

bool equals(const int m1[][SIZE], const int m2[][SIZE])

Write a test program that prompts the user to enter two arrays of integers and displays whether the two are strictly identical. Here are the sample runs.

<Output>

Two arrays are strictly identical <End Output>

<Output>

Two arrays are not strictly identical <End Output>

Name:_______________________

Part II: Multiple Choice Questions: (1 pts each)

(Please circle your answers on paper first. After you finish the entire test, enter your choices online to LiveLab. Log in and click Take Instructor Assigned Quiz. Choose Quiz1. You have 5 minutes to enter and submit the answers.)

1. What is the printout of the following switch statement?

char ch = 'a'; switch (ch) { case 'a': case 'A': cout << ch << endl; break; case 'b': case 'B': cout << ch << endl; break; case 'c': case 'C': cout << ch << endl; break; case 'd': case 'D': cout << ch << endl; }

a. ab b. a c. abcd d. aa

2. What is the printout of the following code?

#include <iostream> using namespace std; void f(int &p1, int p2) { p1++; p2++; } int main() { int x1 = 1; int x2 = 1; f(x1, x2); cout << "x1 is " << x1 << " x2 is " << x2; }

a. x1 is 2 x2 is 1

b. x1 is 2 x2 is 2

c. x1 is 1 x2 is 1

d. x1 is 1 x2 is 2

3. The following code displays ______________.

#include <iostream> using namespace std; void maxValue(int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; }

int main() { int max = 0; maxValue(1, 2, max); cout << "max is " << max << endl; return 0; }

a. max is b. max is 1

c. max is 0 d. max is 2

4. What is the output of the following code?

#include <iostream> using namespace std; void f(double &p) { p += 2; } int main() { double x = 1; double y = 1; f(x); f(y);

cout << "x is " << x; cout << " y is " << y << endl;

return 0;

}

a. x is 2 y is 1

b. x is 3 y is 3

c. x is 2 y is 2

d. x is 1 y is 1

e. x is 1 y is 2

5. The following program invokes p() three times. What is the printout from the last call of p()? #include <iostream> using namespace std;

int j = 40; void p() { int i = 5; static int j = 5; i++; j++; cout << "i is " << i << " j is " << j << endl; }

int main() { p(); p(); p(); }

a. i is 6 j is 8

b. i is 6 j is 6

c. i is 6 j is 9

d. i is 6 j is 7

6. Given the following two arrays:

char s1[] = {'a', 'b', 'c'}; char s2[] = "abc";

Which of the following statements is correct?

a. s1 has three characters

b. s2 has three characters

c. s2 has four characters

d. s1 has four characters

7. Analyze the following code: int main() { int x[5];

int i; for (i = 0; i < 5; i++) x[i] = i; cout << x[i] << " "; return 0; }

a. The program may have a runtime error because the last statement in the main function has the out of bound index for the array.

b. The program displays 4.

c. The program has a compile error because i is not defined in the last statement in the main function.

d. The program displays 0 1 2 3 4.

8. Show the output of the following code:

#include <iostream> using namespace std;

void increase(int x[], int size)

{ for (int i = 0; i < size; i++) x[i]++; }

void increase(int y) { y++; }

int main() { int x[] = { 1, 2, 3, 4, 5 }; increase(x, 5); int y[] = { 1, 2, 3, 4, 5 };

increase(y[0]); cout << x[0] << " " << y[0]; }

a. 2 2

b. 1 1

c. 1 2

d. 0 0

e. 2 1

9. Suppose you declare

char city[7] = "Dallas";

How many characters are stored in city?

a. 7

b. 5

c. 8

d. 6

10. What is the printout of the following code?

char s2[7] = "Dallas"; char s1[14] = "Dallas"; strcat(s1, s2); cout << s1;

a. Dallas

b. DD

c. DallasDallas

d. D

11. When you pass an array to a function, the function receives __________.

a. a copy of the array

b. the reference of the array

c. the length of the array

d. a copy of the first element

12. What is the output of the following code?

#include <iostream> using namespace std;

int main() { int matrix[4][4] = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}};

int sum = 0; for (int i = 0; i < 4; i++) cout << matrix[i][1] << " ";

return 0; }

a. 1 3 8 12

b. 3 6 10 14

c. 4 5 6 7

d. 1 2 3 4

e. 2 5 9 13

Please double check your answer before clicking the Submit button. Whatever submitted to LiveLab is FINAL and counted for your grade.

Please write down the number of correct answers: ______

Solutions:

#include <iostream>

#include <cstring> using namespace std;

int count(const char str[], char a)

{ int count = 0; for (int i = 0; i < strlen(str); i++) { if (str[i] == a) count++; } return count; }

int main()

{ // Prompt the user to enter a string cout << "Enter a string: "; char s[80]; cin.getline(s, 80);

// Prompt the user to enter a character cout << "Enter a character: "; char a; cin >> a;

cout << a << " appears in " << s << " " << count(s, a) << " times " << endl;

return 0; }

#include <iostream> using namespace std;

#define SIZE 3

bool equals(const int m1[][SIZE], const int m2[][SIZE]);

int main()

{ cout << "Enter m1 (a 3 by 3 matrix) row by row: "; int m1[SIZE][SIZE]; for (int i = 0; i < SIZE; i++)

for (int j = 0; j < SIZE; j++) cin >> m1[i][j];

cout << "Enter m2 (a 3 by 3 matrix) row by row: "; int m2[SIZE][SIZE];

for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) cin >> m2[i][j];

if (equals(m1, m2))

cout << "The two arrays are strictly equal" << endl; else cout << "The two arrays are not strictly equal" << endl;

return 0; }

bool equals(const int m1[][SIZE], const int m2[][SIZE]) { for (int i = 0; i < SIZE; i++) for (int j = 0; j < SIZE; j++) if (m1[i][j] != m2[i][j]) return false;

return true; }

1. What is the printout of the following switch statement?

char ch = 'a'; switch (ch) { case 'a': case 'A':

cout << ch << endl; break; case 'b': case 'B':

cout << ch << endl; break; case 'c': case 'C':

cout << ch << endl; break; case 'd': case 'D':

cout << ch << endl; }

ab

b. a c. abcd

d. aa

Key:b #

2. What is the printout of the following code?

#include <iostream> using namespace std;

void f(int &p1, int p2) { p1++; p2++; }

int main() { int x1 = 1; int x2 = 1; f(x1, x2); cout << "x1 is " << x1 << " x2 is " << x2; }

a. x1 is 2 x2 is 1

b. x1 is 2 x2 is 2

c. x1 is 1 x2 is 1

d. x1 is 1 x2 is 2

Key:a #

3. The following code displays ______________.

#include <iostream> using namespace std;

void maxValue(int value1, int value2, int max) { if (value1 > value2) max = value1; else max = value2; }

int main() { int max = 0; maxValue(1, 2, max); cout << "max is " << max << endl; return 0; }

a. max is

b. max is 1

c. max is 0

d. max is 2

Key:c #

4. What is the output of the following code?

#include <iostream> using namespace std;

void f(double &p) { p += 2; }

int main() { double x = 1; double y = 1; f(x); f(y);

cout << "x is " << x; cout << " y is " << y << endl; return 0; }

a. x is 2 y is 1

b. x is 3 y is 3

c. x is 2 y is 2

d. x is 1 y is 1

e. x is 1 y is 2

Key:b #

5. The following program invokes p() three times. What is the printout from the last call of p()?

#include <iostream> using namespace std;

int j = 40;

void p() { int i = 5; static int j = 5; i++; j++;

cout << "i is " << i << " j is " << j << endl; }

int main() { p(); p(); p(); }

a. i is 6 j is 8

b. i is 6 j is 6

c. i is 6 j is 9

d. i is 6 j is 7

Key:a

#

6. Given the following two arrays:

char s1[] = {'a', 'b', 'c'}; char s2[] = "abc";

Which of the following statements is correct?

a. s1 has three characters

b. s2 has three characters

c. s2 has four characters

d. s1 has four characters

Key:ac

#

7. Analyze the following code: int main() { int x[5]; int i; for (i = 0; i < 5; i++) x[i] = i; cout << x[i] << " "; }

a. The program may have a runtime error because the last statement in the main function has the out of bound index for the array.

b. The program displays 4.

c. The program has a compile error because i is not defined in the last statement in the main function.

d. The program displays 0 1 2 3 4.

Key:a

#

8. Show the output of the following code:

#include <iostream> using namespace std;

void increase(int x[], int size) { for (int i = 0; i < size; i++) x[i] ++; }

void increase(int y) { y++; } int main() { int x[] = { 1, 2, 3, 4, 5 };

increase(x, 5); int y[] = { 1, 2, 3, 4, 5 };

increase(y[0]); cout << x[0] << " " << y[0]; }

a. 2 2

b. 1 1

c. 1 2

d. 0 0

e. 2 1

Key:e # 9. Suppose you declare char city[7] = "Dallas";

How many characters are stored in city?

a. 7

b. 5

c. 8

d. 6

Key:a #

10. What is the printout of the following code?

char s2[7] = "Dallas"; char s1[14] = "Dallas"; strcat(s1, s2); cout << s1;

a. Dallas

b. DD

c. DallasDallas

d. D

Key:c #

11. When you pass an array to a function, the function receives __________.

a. a copy of the array

b. the reference of the array

c. the length of the array

d. a copy of the first element

Key:b

#

12. What is the output of the following code?

#include <iostream> using namespace std;

int main()

{ int matrix[4][4] = {{1, 2, 3, 4}, {4, 5, 6, 7}, {8, 9, 10, 11}, {12, 13, 14, 15}};

int sum = 0;

for (int i = 0; i < 4; i++) cout << matrix[i][1] << " "; return 0; }

a. 1 3 8 12

b. 3 6 10 14

c. 4 5 6 7

d. 1 2 3 4

e. 2 5 9 13

Key:e

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
Solution Manual for Introduction to Programming with C++, 3E Y. Daniel Liang by Examexperts - Issuu