Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

Problem 18

Write a program that prints a table of the binary, octal and hexadecimal equivalents of the decimal numbers in the range 1 through \(256 .\) If you are not familiar with these number systems, read Appendix D, Number Systems, first.

Problem 19

Calculate the value of \(\mathrm{p}\) from the infinite series \(\pi=4-\frac{4}{3}+\frac{4}{5}-\frac{4}{7}+\frac{4}{9}-\frac{4}{11}+\cdots\) Print a table that shows the approximate value of \(\mathrm{p}\) after each of the first 1,000 terms of this series.

Problem 20

(Pythagorean Triples) A right triangle can have sides that are all integers. A set of three integer values for the sides of a right triangle is called a Pythagorean triple. These three sides must satisfy the relationship that the sum of the squares of two of the sides is equal to the square of the hypotenuse. Find all Pythagorean triples for side1, side? and hypotenuse all no larger than \(500 .\) Use a triple-nested for loop that tries all possibilities. This is an example of brute force computing. You will learn in more advanced computer-science courses that there are many interesting problems for which there is no known algorithmic approach other than sheer brute force.

Problem 21

A company pays its employees as managers (who receive a fixed weekly salary), hourly workers (who receive a fixed hourly wage for up to the first 40 hours they work and "time-and-a-half" 1.5 times their hourly wagefor overtime hours worked), commission workers (who receive \(\$ 250\) plus 5.7 percent of their gross weekly sales), or pieceworkers (who receive a fixed amount of money per item for each of the items they produceeach pieceworker in this company works on only one type of item). Write a program to compute the weekly pay for each employee. You do not know the number of employees in advance. Each type of employee has its own pay code: Managers have code 1 , hourly workers have code 2 , commission workers have code 3 and pieceworkers have code \(4 .\) Use a switch to compute each employee's pay according to that employee's paycode. Within the switch, prompt the user (i.e., the payroll clerk) to enter the appropriate facts your program needs to calculate each employee's pay according to that employee's paycode.

Problem 22

(De Morgan's Laws) In this chapter, we discussed the logical operators 88,11 and \(1 .\) De Morgan's laws can sometimes make it more convenient for us to express a logical expression. These laws state that the expression 1 condition1 ss condition2 ) is logically equivalent to the expression 1 condition1 \(\|\) condition2 ). Also, the expression \(!\) ( condition1 ? I condition? is logically equivalent to the expression ( i condition1 ss ! condition2) . Use De Morgan's laws to write equivalent expressions for each of the following, then write a program to show that the original expression and the new expression in each case are equivalent: a. \(|(x<5)\) ss \(|(y>-7)\) b. \(1(a=b)|| 1(g |=5)\) C. \(1(x \in 8)\) ss \((y>4)\) d. \(1(\quad(i>4), \| \quad(j \in=6))\)

Problem 23

Write a program that prints the following diamond shape. You may use output statements that print either a single asterisk \((*)\) or a single blank. Maximize your use of repetition (with nested for statements) and minimize the number of output statements.

Problem 25

A criticism of the break and continue statements is that each is unstructured. Actually they statements can always be replaced by structured statements, although doing so can be awkward. Describe in general how you would remove any break statement from a loop in a program and replace it with some structured equivalent. [Hint: The break statement leaves a loop from within the body of the loop. Another way to leave is by failing the loopcontinuation test. Consider using in the loop-continuation test a second test that indicates "early exit because of a 'break' condition."] Use the technique you developed here to remove the break statement from the program of Fig 5.13

Problem 26

What does the following program segment do? 1 for ( int i = 1; i <= 5; i++ ) 2 { 3 for ( int j = 1; j <= 3; j++ ) 4 { 5 for ( int k = 1; k <= 4 ; k++ ) 6 cout << '*'; 7 8 cout << endl; 9 } // end inner for 10 11 cout << endl; 12 } // end outer for

Problem 28

("The Twelve Days of Christmas" Song) Write a program that uses repetition and switch statements to print the song "The Twelve Days of Christmas." One switch statement should be used to print the day (i.e., "First," "Second," etc.). A separate switch statement should be used to print the remainder of each verse. Visit the Web site www.12days.com/library/carols/12daysofxmas.htm for the complete lyrics to the song.

Access millions of textbook solutions in one place

  • Access over 3 million high quality textbook solutions
  • Access our popular flashcard, quiz, mock-exam and notes features
  • Access our smart AI features to upgrade your learning
Get Vaia Premium now
Access millions of textbook solutions in one place

Recommended explanations on Computer Science Textbooks