Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. State whether the following are true or false. If the answer is false, explain why. a) The default case is required in the switch

1. State whether the following are true or false. If the answer is false, explain why.

a) The default case is required in the switch selection statement.

b) The break statement is required in the default case of a switch selection statement to

exit the switch properly.

c) The expression ( x > y && a < b ) is true if either the expression x > y is true or the

expression a < b is true.

d) An expression containing the || operator is true if either or both of its operands are

true.

2. Write a C++ statement or a set of C++ statements to accomplish each of the following:

a) Sum the odd integers between 1 and 99 using a for statement. Assume the integer variables

sum and count have been declared.

b) Print the value 333.546372 in a 15-character field with precisions of 1, 2 and 3. Print each

number on the same line. Left-justify each number in its field. What three values print?

c) Calculate the value of 2.5 raised to the power 3 using function pow. Print the result with

a precision of 2 in a field width of 10 positions. What prints?

d) Print the integers from 1 to 20 using a while loop and the counter variable x. Assume

that the variable x has been declared, but not initialized. Print only 5 integers per line.

[Hint: When x % 5 is 0, print a newline character; otherwise, print a tab character.]

e) Repeat Exercise 2(d) using a for statement.

3 Find the errors in each of the following code segments and explain how to correct them.

a) x = 1;

while ( x <= 10 );

++x;

}

b) switch ( n )

{

case 1:

cout << "The number is 1" << endl;

case 2:

cout << "The number is 2" << endl;

break;

default:

cout << "The number is not 1 or 2" << endl;

break;

}

c) The following code should print the values 1 to 10.

n = 1;

while ( n < 10 )

cout << n++ << endl;

4 Find the error(s), if any, in each of the following:

a) For ( x = 100, x >= 1, ++x )

cout << x << endl;

b) The following code should print whether integer value is odd or even:

switch ( value % 2 )

{

case 0:

cout << "Even integer" << endl;

case 1:

cout << "Odd integer" << endl;

}

c) The following code should output the odd integers from 19 to 1:

for ( x = 19; x >= 1; x += 2 )

cout << x << endl;

d) The following code should output the even integers from 2 to 100:

counter = 2;

do

{

cout << counter << endl;

counter += 2;

} While ( counter < 100 );

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Introduction to Wireless and Mobile Systems

Authors: Dharma P. Agrawal, Qing An Zeng

4th edition

1305087135, 978-1305087132, 9781305259621, 1305259629, 9781305537910 , 978-130508713

More Books

Students also viewed these Programming questions