Assignment 5: Ch 7

7.1 Fill in the blanks in each of the following statements:

a) All programs can be written in terms of three types of control structures: sequence, selection and repetition.
b) The if...else double-selection statement is used to execute one action when a condition is true and another action when that condition is false.
c) Repeating a set of instructions a specific number of times is called a counter-controlled repetition.
d) When it is not known in advance how many times a set of statements will be repeated, a sentinel, signal, flag or dummy value can be used to terminate the repetition.

7.2 Write four JavaScript statements that each add 1 to variable x, which contains a number.

x = x + 1;
x += 1;
++x;
x==;

7.3 Write JavaScript statements to accomplish each of the following tasks:

  1. Assign the sum of x and y to z, and increment the value of x by 1 after the calculation. Use only one statement.
    z = x++ + y;
  2. Test whether the value of the variable count is greater than 10. If it is, print "Count is greater than 10".
    if (count > 10)
       document.writeln("Count is greater than 10");
  3. Decrement the variable x by 1, then subtract it from the variable total. Use only one statement.
    total -= --x;
  4. Calculate the remainder after q is divided by divisor, and assign the result to q. Write this statement in tow different ways.
    q %= divisor;
    q = q % divisor;

7.4 Write a JavaScript statement to accomplish each of the following tasks:

  1. Declare variables sum and x.
    var sum, x;
  2. Assign 1 to variable x.
    x = 1
  3. Assign 0 to the variable sum.
    sum = 0;
  4. Add variable x to variable sum, and assign the results to variable sum.
    sum = sum + x;
  5. Print "The sum is: ", followed by the value of variable sum.
    document.writeln("The sum is: " + sum);

7.5 Combine the statements that you wrote in Exercise 7.4 into a JavaScript program that calculates and prints the sum of the integers from 1 to 10. Use the while statement to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11.

7.6 Determine the value of each variable after the calculation is performed. Assume that, when each statement begins executing, all variables have the integer value 5.

  1. product *= x++; product = 25, x = 6
  2. quotient /= ++x; quotient = 0.833, x = 6

7.7 Identify and correct the errors in each of the following segments of code:

  1. while ( c <= 5) {
       product *= c;
       ++c;
  2. if ( gender == 1)
       document.writeln("Woman");
    else;
       document.writeln("Man");

7.8 What is wrong with the following while repetition statement?
while ( z >= 0)
   sum += z;

The value of the variable z is never changed in the body of the while statement. If the loop-continuation condition ( z>= 0) is true, an infinite loop is created. z must be decremented so that it eventually becomes less than 0.

7.9 Identify and correct the errors in each of the following segments of code:

  1. if ( age >= 65 )
       document.writeln( "Age greater than or equal to 65" );
    else
       document.writeln( "Age is less than 65" );
  2. var x = 1, total = 0;
    while ( x <= 10 )
    {
       total += x;
       ++x; }
  3. var x = 1;
    var total = 0;
    while ( x <= 100 )
    {
       total += x;
       ++x;
    }
  4. var y = 5
    while ( y < 0 )
    {
       document.writeln( y );
       --y;
    }

7.10 What does the following program print?

7.11 Develop a JavaScript program that will take as input the miles driven and gallons used (both as integers) for each tankful. The program should calculate and output XHTML text that displays the number of miles per gallon obtained for each tankful and prints the combined number of miles per gallon obtained for all tankfuls up to this point. Use prompt dialogs to obtain data from the user.

7.12 Develop a JavaScript program that will determine whether a department-store customer has exceeded the credit limit on a charge account.

7.13 Develop a program that inputs one salesperson's items sold for the last week, calculates the salesperson's earnings and outputs XHTML text that displays the salesperson's earnings.

7.14 Develop a JavaScript program that will determine the gross pay for each of three employees.

7.15 Write a pseudocode program and then a JavaScript program that inputs a series of 10 single-digit numbers as characters, determines the largest of the numbers and outputs a message that displays the largest number.

7.16 Write a JavaScript program that uses looping to print a table of values. Output the results in an XHTML table. Use CSS to center the data in each column.

7.17 Using an approach similar to that in Exercise 7.15, find the two largest values among the 10 digist entered.

7.18 Modify the program in Fig. 7.11 to validate its inputs. For every value input, if the value entered is other than 1 or 2, keep looping until the user enters a correct value.

7.19 What does the example program print?

7.20 What does the example program print?

7.21 a b Determine the output for each of the given segments of code when x is 9 and y is 11, and when x is 11 and y is 9.

7.22 a b c d Modify the given code to produce the output shown in each part of the problem.

7.23 Write a script that reads in the size of the side of a square and outputs XHTML text that displays a hollow square of that size constructed of asteriscks.

7.24 Write a script that reads in a five-digit integer and determines whether it is a palindrome.

7.25 Write a script that outputs XHTML text that displays a checkerboard pattern.

7.26 Write a script that outputs XHTML text that keeps displaying in the browser window the multiples of the integer 2. Your loop should not terminate.

   The script runs infinately and the page becomes unresponsive.

7.27 Write a program that will encript data transmitted as four-digit integers.

7.28 What does the example program print?

Assignment 5: Ch 8

8.1 State whether each of the following is true or false. If false, explain why.

  1. The default case is required in the switch selection statement.
       False. The default case is optional. If no default action is needed, then there is no need for a default case.
  2. The break statement is required in the last case of a switch selection statement.
       False. The break statement is used to exit the switch statement. The break statement is not required for the last case in a switch statement.
  3. The expression ( x > y && a < b ) is true if either x > y is true or a < b is true.
       False. Both of the relational expressions must be true in order for the entire expression to be true when using the && operator.
  4. An expression containing the || operator is true if either or both of its operands is true.
       True.

8.2 Write a JavaScript statement or a set of statements to accomplish each of the following tasks:

  1. Sum the odd integers between 1 and 99. Use a for statement. Assume that the variables sum and count have been declared.
       sum = 0;
       for ( count = 1; count <= 99; count += 2 )
          sum += count;
  2. Calculate the value of 2.5 raised to the power of 3. Use the pow method.
       Math.pow( 2.5, 3 );
  3. Print the integers from 1 to 20 by using a while loop and the counter variable x. Assume that the variable x has been declared, but not initialized. Print only five integers per line.
       x = 1;
       while ( x <= 20 )
       {
          document.write( x + " " );
          if ( x % 5 == 0 )
             document.write( "<br /> );
       }
  4. Repeat Exercise 8.2(c), but using a for statement.
       for ( x = 1; x <= 20; x++ )
       {
          document.write( x + " " );
          if ( x % 5 == 0 )
             document.write( "<br /> );
       }

8.3 Find the error in each of the following code statements, and explain how to correct it:

  1. Error: The semicolon after the while header causes an infinite loop, and there is a missing left brace. Correction: Replace the semicolon by a {, or remove both the ; and the }.
  2. Error: Using a floating-point number to control a for repetition statement may not work, because floating-point numbers are represented approximately by most computers. Correction: Use an integer, and perform the proper calculation to get the values you desire.
  3. Error: Missing break statement in the statements for the first case. Correction: Add a break statement at the end of the statements for the first case. Note that this missing statement is not necessarily an error if the programmer wants the statement of case 2: to execute every time the case 1: statement executes.
  4. Error: Improper relational operator used in the while continuation condition. Correction: Use >= rather than >, or change 10 to 11.

8.4 Find the error in each of the following segments of code.

  1. Error: The statement creates an infinite loop, should not contain commas, and "For" should not be capitalized.
  2. Error: The switch cases have no break statements.
  3. Error: The statement creates an infinite loop.
  4. Error: The statement should not have a capital w.

8.5 What does the following script do?

8.6 Write a script that finds the smallest of several non-negative integers. Assume that the first value read specifies the number of values to be input from the user.

8.7 Write a script that calculates the product of the odd integers from 1 to 15 then outputs XHTML text that displays the results.

8.8 Modify the compound interest program in Fig. 8.6 to repeat its steps for interest rates of 5, 6, 7, 8, 9 and 10 percent. Use a for statement to vary the interest rate. Use a separate table for each rate.

8.9 Write a script that outputs XHTML to display the given patterns separately, one below the other.

8.10 Write a script that reads five numbers between 1 and 30. For each number read, output XHTML text that displays a line containing the same number of adjacent asterisks.

8.11 Write a script that uses repetition and a switch structure to print the song "The Twelve Days of Christmas."

8.12 Write a script that reads a series of pairs of numbers as follows:

  1. Product number
  2. Quantity sold for the day

8.13 Assume that i = 1, j = 2, k = 3 and m = 2. What does each of the given statements print? Are the parentheses necessary in each case?

  1. document.writeln( i == 1 ); true - parentheses are necessary for writeln statement, not logical operations
  2. document.writeln( j == 3 ); false - parentheses are necessary for writeln statement, not logical operations
  3. document.writeln( i >= 1 && j < 4 ); true - parentheses are necessary for writeln statement, not logical operations
  4. document.writeln( m <= 99 && k < m ); false - parentheses are necessary for writeln statement, not logical operations
  5. document.writeln( j >= i || k == m ); true - parentheses are necessary for writeln statement, not logical operations
  6. document.writeln( k + m < j || 3 - j >= k ); false - parentheses are necessary for writeln statement, not logical operations
  7. document.writeln( !( k > m ) ); false - parentheses are necessary for writeln statement and logical operations

8.14 Modify Exercise 8.9 to combine your code from the four separate triangles of asterisks into a single script that prints all four patterns side by side, making clever use of nested for statements.

8.15 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 are equivalent in each case:

  1. !( x < 5 ) && !( y >= 7 ) Equivalent to: !( x < 5 || y >= 7 )
  2. !( a == b ) || !( g != 5 ) Equivalent to: !( a == b && g != 5 )
  3. !( ( x <= 8 ) && ( y > 4 ) ) Equivalent to: !( x <= 8 ) || !( y > 4 )
  4. !( ( i > 4 ) || ( j <= 6 ) ) Equivalent to: !( i > 4 ) && !( j <= 6 )

8.16 Write a script that prints a diamond shape:

8.17 Modify the program in Exercise 8.16 to read an odd number in the range 1 to 19. This number specifies the number of rows in the diamond. The program should then display a diamond shape of the approiate size.

8.18 Describe in general how you would remove any break statement from a loop in a program and replace it with some structured equivalent.

   The break condition could be inserted into the FOR loop.

8.19 What does the following script do?

Home