Exercises

E.6 Fill in the blanks in each of the following statements:

a) One-dimensional array p contains four elements. The names of those elements are _____________, _____________, _____________ and _____________.

b) Naming an array, stating its type and specifying the number of dimensions in the array is called _________ the array.

c) In a two-dimensional array, the first index identifies the _________ of an element and the second index identifies the _________ of an element.

d) An m-by-n array contains _________ rows, _________ columns and _________ elements.

e) The name of the element in row 3 and column 5 of array d is _________.

E.7 Determine whether each of the following is true or false. If false, explain why.

a) To refer to a particular location or element within an array, we specify the name of the array and the value of the particular element.

b) An array declaration reserves space for the array.

c) To indicate that 100 locations should be reserved for integer array p, you write the declaration

p[ 100 ];

d) An application that initializes the elements of a 15-element array to zero must contain at least one for statement.

e) An application that totals the elements of a two-dimensional array must contain nested for statements.

E.8 Consider a two-by-three integer array t.

a) Write a statement that declares and creates t.

b) How many rows does t have?

c) How many columns does t have?

d) How many elements does t have?

e) Write access expressions for all the elements in row 1 of t.

f) Write access expressions for all the elements in column 2 of t.

g) Write a single statement that sets the element of t in row 0 and column 1 to zero.

h) Write individual statements to initialize each element of t to zero.

i) Write a nested for statement that initializes each element of t to zero.

j) Write a nested for statement that inputs the values for the elements of t from the user.

k) Write a series of statements that determines and displays the smallest value in t.

l) Write a single printf statement that displays the elements of the first row of t.

m) Write a statement that totals the elements of the third column of t. Do not use repetition.

n) Write a series of statements that displays the contents of t in tabular format. List the column indices as headings across the top, and list the row indices at the left of each row.

E.9 (Duplicate Elimination) Use a one-dimensional array to solve the following problem: Write an application that inputs five numbers, each between 10 and 100, inclusive. As each number is read, display it only if it’s not a duplicate of a number already read. Provide for the “worst case,” in which all five numbers are different. Use the smallest possible array to solve this problem. Display the complete set of unique values input after the user enters each new value.

E.10 Label the elements of three-by-five two-dimensional array sales to indicate the order in which they’re set to zero by the following program segment:

for ( int row = 0; row < sales.length; row++ )
{
   for ( int col = 0; col < sales[ row ].length; col++ )
   {
      sales[ row ][ col ] = 0;
   }
}

E.11 (Sieve of Eratosthenes) A prime number is any integer greater than 1 that’s evenly divisible only by itself and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:

a) Create a primitive-type boolean array with all elements initialized to true. Array elements with prime indices will remain true. All other array elements will eventually be set to false.

b) Starting with array index 2, determine whether a given element is true. If so, loop through the remainder of the array and set to false every element whose index is a multiple of the index for the element with value true. Then continue the process with the next element with value true. For array index 2, all elements beyond element 2 in the array that have indices which are multiples of 2 (indices 4, 6, 8, 10, etc.) will be set to false; for array index 3, all elements beyond element 3 in the array that have indices which are multiples of 3 (indices 6, 9, 12, 15, etc.) will be set to false; and so on.

When this process completes, the array elements that are still true indicate that the index is a prime number. These indices can be displayed. Write an application that uses an array of 1000 elements to determine and display the prime numbers between 2 and 999. Ignore array elements 0 and 1.

E.12 (Fibonacci Series) The Fibonacci series

0, 1, 1, 2, 3, 5, 8, 13, 21, ...

begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.

a) Write a method fibonacci( n ) that calculates the nth Fibonacci number. Incorporate this method into an application that enables the user to enter the value of n.

b) Determine the largest Fibonacci number that can be displayed on your system.

c) Modify the application you wrote in part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified application to repeat part (b).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset