CH6 Ex
CH6 Ex
CH6 Ex
Arrays and
Now go, write it
Vectors
before them in a table,
and note it in a book.
—Isaiah 30:8
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Assignment Checklist
Name: Date:
Section:
Prelab Activities
Matching YES NO
Fill in the Blank 12, 13, 14, 15, 16, 17, 18
Short Answer 19, 20, 21, 22, 23
Programming Output 24, 25, 26, 27, 28, 29
Correct the Code 30, 31, 32, 33, 34, 35
Lab Exercises
Lab Exercise 1—Rolling Dice YES NO
Follow-Up Questions and Activities 1, 2, 3, 4
Lab Exercise 2—Bubble Sort YES NO
Follow-Up Question and Activity 1
Lab Exercise 3—Salespeople YES NO
Follow-Up Questions and Activities 1, 2, 3
Debugging YES NO
Labs Provided by Instructor
1.
2.
3.
Postlab Activities
Coding Exercises 1, 2, 3, 4, 5, 6
Programming Challenges 1, 2, 3, 4
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Prelab Activities
Matching
Name: Date:
Section:
After reading Chapter 7 of C++ How to Program, Seventh Edition, answer the given questions. These questions
are intended to test and reinforce your understanding of key concepts and may be done either before the lab or
during the lab.
For each term in the column on the left, write the corresponding letter for the description that best matches it
from the column on the right.
Term Description
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Name: Date:
Section:
13. To pass one row of a double-subscripted array to a function that receives a single-subscripted array, pass the
name of the array followed by the .
18. A(n) may be an integer or an integer expression and identifies a particular array element.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Short Answer
Short Answer
Name: Date:
Section:
In the space provided, answer each of the given questions. Your answers should be as concise as possible; aim for
two or three sentences.
20. Describe how a linear search works. On average, how many comparisons must a linear search perform?
21. What is the const qualifier? What happens when the programmer tries to modify the contents of an array
that is passed to a function that receives the array as a const parameter?
22. How is an insertion sort implemented? Why is insertion sort inefficient for sorting large arrays?
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Short Answer
23. Describe how multidimensional arrays might represent a table in which information is arranged in rows and
columns.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Output
Programming Output
Name: Date:
Section:
For each of the given program segments, read the code and write the output in the space provided below each
program. [Note: Do not execute these programs on a computer.]
1 int i;
2 int values[ 10 ] = { 4, 1, 1, 3, 4, 9, 9, 2, 1, 7 };
3
4 cout << "Element" << setw( 13 ) << "Value" << endl;
5
6 for ( i = 0; i < 10; i++ )
7 cout << setw( 7 ) << i << setw( 13 ) << values[ i ] << endl;
Your answer:
Your answer:
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Output
1 #include <iostream>
2 using namespace std;
3
4 void mystery();
5
6 int main()
7 {
8 cout << "First call to mystery:\n";
9 mystery();
10
11 cout << "\n\nSecond call to mystery:\n";
12 mystery();
13 cout << endl;
14 } // end main
15
16 // function mystery definition
17 void mystery()
18 {
19 static int array1[ 3 ];
20 int i;
21
22 cout << "\nValues on entering mystery:\n";
23
24 for ( i = 0; i < 3; i++ )
25 cout << "array1[" << i << "] = " << array1[ i ] << " ";
26
27 cout << "\nValues on exiting mystery:\n";
28
29 for ( i = 0; i < 3; i++ )
30 cout << "array1[" << i << "] = "
31 << ( array1[ i ] += 2 ) << " ";
32
33 } // end function mystery
Your answer:
27. What is output by the following program? What algorithm does this program implement?
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 { © 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Output
Your answer:
Programming Output
11 }
Your answer:
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 int array[ 3 ][ 4 ] = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };
7
8 for ( int i = 0; i < 3; i++ )
9 {
10 for ( int j = 0; j < 4; j++ )
11 {
12 cout << array[ i ][ j ] << " ";
13 }
14
15 cout << endl;
16 }
17 } // end main
Your answer:
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Name: Date:
Section:
For each of the given program segments, determine if there is an error in the code. If there is an error, specify
whether it is a logic or compilation error, circle the error in the program, and write the corrected code in the
space provided after each problem. If the code does not contain an error, write “no error.” [Note: It is possible
that a program segment may contain multiple errors.]
30. The following code should assign 8 to the fifth element in array:
1 array[ 5 ] = [ 8 ];
Your answer:
31. The for loop should initialize all array values to -1.
1 int array[ 10 ];
2
3 for ( int i = 0; i < 9; i++ )
4 array[ i ] = -1;
Your answer:
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
1 int array[ 10 ] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
Your answer:
33. The following code segment should declare two arrays containing five and six elements, respectively:
Your answer:
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Chapter 7 Arrays and Vectors 17
34. The for loop that follows should print array’s values:
1 int array[ 10 ] = { 0 };
2
3 for ( int i = 0; i <= 10; i++ )
4 cout << array[ i ];
Your answer:
35. The for loop that follows should print all of array’s values:
1 int array[ 3 ][ 3 ] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
2
3 for ( int i = 0; i < 3; i++ )
4
5 for ( int i = 0; i < 3; i++ )
6 cout << array[ i ][ i ];
Your answer:
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Lab Exercises
Lab Exercise 1 — Rolling Dice
Name: Date:
Section:
This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The
problem is divided into six parts:
1. Lab Objectives
2. Description of the Problem
3. Sample Output
4. Program Template (Fig. L 7.2)
5. Problem-Solving Tips
6. Follow-Up Questions and Activities
The program template represents a complete working C++ program, with one or more key lines of code replaced
with comments. Read the problem description and examine the sample output; then study the template code.
Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the
program. Compare your output with the sample output provided. Then answer the follow-up questions. The
source code for the template is available from the Companion Website for C++ How to Program, Seventh Edition
at www.pearsonhighered.com/deitel/.
Lab Objectives
This lab was designed to reinforce programming concepts from Chapter 7 of C++ How To Program, Seventh Edi-
tion. In this lab, you will practice:
• Using rand to generate random numbers and using srand to seed the random-number generator.
• Declaring, initializing and referencing arrays.
The follow-up questions and activities also will give you practice:
• Remembering that arrays begin with subscript 0 and recognizing off-by-one errors.
• Preventing array out-of-bounds errors.
• Using two-dimensional arrays.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
1 2 3 4 5 6
1 2 3 4 5 6 7
2 3 4 5 6 7 8
3 4 5 6 7 8 9
4 5 6 7 8 9 10
5 6 7 8 9 10 11
6 7 8 9 10 11 12
Template
1 // Lab 1: dice.cpp
2 #include <iostream>
3 #include <iomanip>
4 #include <cstdlib>
5 #include <ctime>
6 using namespace std;
7
8 int main()
9 {
10 const long ROLLS = 36000;
11 const int SIZE = 13;
12
13 // array exepected contains counts for the expected
14 // number of times each sum occurs in 36 rolls of the dice
15 /* Write the declaration of array exprected here. Assign an
16 initializer list containing the expected values here. Use
17 SIZE for the number of elements */
18 int x; // first die
Problem-Solving Tips
1. Remember that array subscripts always begin with zero. This is also true for each dimension of a mul-
tiple-subscripted array (which this lab does not use).
2. The actual percentage is the likelihood, based on the results of your program, that a dice roll produced
a certain result. In other words, if you roll the dice 36,000 times the actual percentage will be the (num-
ber of times a result occurred / 36000) * 100.
3. The expected percentage is the statistical probability that a dice roll will produce a certain result. This
can be calculated from the diagram “36 possible outcomes of rolling two dice,” shown in the problem
description. For example, there is only one combination that will produce the sum of 2 and there are
36 total combinations that occur with equal likelihood. Therefore, the expected percentage of rolling a
2 is 1/36 or 2.778%.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
2. What happens if the < operator on line 47 of the program template is changed to <=?
3. What happens if the elements of array sum are not initialized to zero? Try running the program without ini-
tializing the array. Show your results.
4. Modify the program to use a two-dimensional array similar to the diagram in Figure L 7.1. Now, rather
than counting the number of times each sum appears, increment the correct cell in the array. Print this array
with the number of times each dice combination occurred. A sample output may look like the following:
1 2 3 4 5 6
1 1011 971 1027 1025 971 1015
2 1013 968 990 968 1081 993
3 993 1014 983 973 1019 977
4 980 1004 974 1022 946 1046
5 1003 1021 1019 979 1004 1056
6 1026 1015 931 989 1014 979
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Name: Date:
Section:
This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The
problem is divided into six parts:
1. Lab Objectives
2. Description of the Problem
3. Sample Output
4. Program Template (Fig. L 7.3–Fig. L 7.4)
5. Problem-Solving Tips
6. Follow-Up Question and Activity
The program template represents a complete working C++ program, with one or more key lines of code replaced
with comments. Read the problem description and examine the sample output; then study the template code.
Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the
program. Compare your output with the sample output provided. Then answer the follow-up question. The
source code for the template is available from the Companion Website for C++ How to Program, Seventh Edition
at www.pearsonhighered.com/deitel/.
Lab Objectives
This lab was designed to reinforce programming concepts from Chapter 7 of C++ How To Program, Seventh Edi-
tion. In this lab, you will practice:
• Sorting data using the bubble sort algorithm.
The follow-up question and activity also will give you practice:
• Optimizing a program to be more efficient.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Template
1 // Lab 2: bubblesort.cpp
2 // This program sorts an array's values into ascending order.
3 #include <iostream>
4 #include <iomanip>
5 using namespace std;
6
7 int main()
8 {
9 const int arraySize = 10; // size of array a
10 int a[ arraySize ] = { 2, 6, 4, 8, 10, 12, 89, 68, 45, 37 };
11 int hold; // temporary location used to swap array elements
12
13 cout << "Data items in original order\n";
14
15 // output original array
16 for ( int i = 0; i < arraySize; i++ )
17 cout << setw( 4 ) << a[ i ];
18
19 // bubble sort
20 // loop to control number of passes
21 /* Write a for header to loop for one iteration less than the size
22 of the array */
23 {
24 // loop to control number of comparisons per pass
25 /* Write a for header to iterate j from 0 and keep
26 looping while j is less than arraySize - 1 */
27 {
28 // compare side-by-side elements and swap them if
29 // first element is greater than second element
30 /* Write an if statement to test if element j is greater than
31 element j + 1 */
32 {
33 /* Write code to swap the values in element j and
34 element j + 1, using hold as temporary storage */
35 } // end if
36 } // end for
37 } // end for
38
39 cout << "\nData items in ascending order\n";
40
41 // output sorted array
42 for ( int k = 0; k < arraySize; k++ )
43 cout << setw( 4 ) << a[ k ];
44
45 cout << endl;
46 } // end main
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Problem-Solving Tips
1. Each “bubbling” pass through the array brings one element, the ith up to its correct position. This means
that the program will require arraySize - 1 passes through the array to sort the entire array.
2. Each bubbling pass will look at each pair of adjacent elements and swap them if they are not already in
sorted order.
3. To swap two elements, the value of one element will have to be stored in a temporary storage variable
while the value of the other element is placed in the first, and then the second element can be replaced
with the temporary storage value.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Name: Date:
Section:
This problem is intended to be solved in a closed-lab session with a teaching assistant or instructor present. The
problem is divided into six parts:
1. Lab Objectives
2. Description of the Problem
3. Sample Output
4. Program Template (Fig. L 7.4)
5. Problem-Solving Tips
6. Follow-Up Questions and Activities
The program template represents a complete working C++ program, with one or more key lines of code replaced
with comments. Read the problem description and examine the sample output; then study the template code.
Using the problem-solving tips as a guide, replace the /* */ comments with C++ code. Compile and execute the
program. Compare your output with the sample output provided. Then answer the follow-up questions. The
source code for the template is available from the Companion Website for C++ How to Program, Seventh Edition
at www.pearsonhighered.com/deitel/.
Lab Objectives
This lab was designed to reinforce programming concepts from Chapter 7 of C++ How To Program, Seventh Edi-
tion. In this lab, you will practice:
• Using double-subscripted arrays to store tables of information.
• Nesting for loops to access multiple-subscripted arrays.
The follow-up question and activities also will give you practice:
• Using const ints to declare identifiers that are used in an array declaration.
• Initializing multidimensional arrays.
• Using character arrays to store strings.
for last month. Your tabular printout should include these cross totals to the right of the totaled rows and to the
bottom of the totaled columns.
Sample Output
Enter the salesperson (1 - 4), product number (1 - 5), and total sales.
Enter -1 for the salesperson to end input.
1 1 9.99
1 2 12.49
1 5 19.98
2 3 15.75
2 4 21.23
3 1 17.33
4 5 22.22
4 1 19.65
-1
The total sales for each salesperson are displayed at the end of each row,
and the total sales for each product are displayed at the bottom of each column.
1 2 3 4 5 Total
1 9.99 12.49 0.00 0.00 19.98 42.46
2 0.00 0.00 15.75 21.23 0.00 36.98
3 17.33 0.00 0.00 0.00 0.00 17.33
4 19.65 0.00 0.00 0.00 22.22 41.87
Template
1 // Lab 3: sales.cpp
2 #include <iostream>
3 #include <iomanip>
4 using namespace std;
5
6 int main()
7 {
8 const int PEOPLE = 5;
9 const int PRODUCTS = 6;
10 /* Write the declaration for array sales here */
11 double value;
12 double totalSales;
13 double productSales[ PRODUCTS ] = { 0.0 };
14 int salesPerson;
15 int product;
16
17 // enter sales slips
18 cout << "Enter the salesperson (1 - 4), product number (1 - 5), and "
19 << "total sales.\nEnter -1 for the salesperson to end input.\n";
20
21 cin >> salesPerson;
22
Problem-Solving Tips
1. This problem asks the reader to input a series of numbers representing the salesperson number, product
number and the dollar amount. The product number and salesperson number represent the row sub-
script and column subscript in the sales array where the dollar amount is added. Each array begins with
subscript zero; therefore, it is recommended that you oversize the array by one element in each dimen-
sion. This allows you to map the product number and salesperson number directly to a subscript with-
out having to subtract one.
2. Table columns contain the total sales for each product. Table rows contain the sales figures for each
salesperson. To create the output, the table header must first be printed. (See template.) When program
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
control reaches the outer for loop, the salesperson number is printed. The inner for loop prints the
amount of each product that the salesperson sold. When the inner loop finishes, control returns to the
outer loop and the \n character is printed.
3. To display totals in the right-most column, simply sum each element in the row and display the total.
This is best done when the array is output. To display the totals at the bottom, declare a one-dimen-
sional array of five elements. While outputting sales, simply add the current column’s value to the ap-
propriate element of the single-subscripted array. After outputting sales and the totals for each row,
iterate through the single-subscripted array and output its values.
2. Change the declaration of productSales, in your solution that corresponds to line 18 in the program tem-
plate, so that salesperson 1 has sold $75.00 of product 3 initially and so that salesperson 4 has sold $63.00
of product 1 initially. All other array values should be initialized to 0.0. [Hint: Use an initializer list to ini-
tialize the array.]
3. Create an additional array that stores the names of all of the salespeople. Allow the user to input the first
names of the four employees. Limit the names to 20 characters. When generating the output table, use the
names of the salespeople rather than numbers.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Debugging
Debugging
Name: Date:
Section:
The program (Fig. L 7.5) in this section does not run properly. Fix all the compilation errors so that the program
will compile successfully. Once the program compiles, compare the output with the sample output, and elimi-
nate any logic errors that may exist. The sample output demonstrates what the program’s output should be once
the program’s code has been corrected.
Sample Output
Name 1 2 3 4 5 6 7 8 9 10
Bob 56 67 83 81 70 84 94 64 68 86
John 76 89 81 42 66 93 104 91 71 85
Joe 65 69 91 89 82 93 72 76 79 99
Broken Code
1 // Debugging: grades.cpp
2 #include <iostream>
3 #include <iomanip>
4 #include <ctime>
5 using namespace std;
6
7 const int NUM_GRADES = 10;
8 const int NUM_SUDENTS = 3;
9
10 int findHighest( int );
11 int findLowest( int * );
12 void printDatabase( const int [][], const char [][ 20 ] );
13
14 int main()
15 {
16 int student1[ NUM_GRADES ] = { 0 };
17 int student2[ NUM_GRADES ] = { 76, 89, 81, 42, 66, 93, 104,
18 91, 71, 85, 105 };
19 int student3[ NUM_GRADES ] = { 65, 69, 91, 89, 82, 93, 72,
20 76, 79, 99 };
21 char names[ NUM_SUDENTS ][ 20 ] = { "Bob", "John", "Joe" };
22
Debugging
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Debugging
77 // output data
78 void printDatabase( int a[][ NUM_GRADES ], char names[][ 20 ] )
79 {
80 cout << "Here is the grade database\n\n"
81 << setw( 10 ) << "Name";
82
83 for ( int n = 1; n <= NUM_GRADES; n++ )
84 cout << setw( 4 ) << n;
85
86 cout << endl;
87
88 for ( int i = 0; i < NUM_SUDENTS; i++ ) {
89 cout << setw( 10 ) << names[ i ];
90
91 for ( int j = 0; j < NUM_GRADES; j++ )
92 cout << setw( 4 ) << a[ i, j ];
93
94 cout << endl;
95
96 } // end for
97
98 cout << endl;
99
100 } // end printDatabase
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Postlab Activities
Coding Exercises
Name: Date:
Section:
These coding exercises reinforce the lessons learned in the lab and provide additional programming experience
outside the classroom and laboratory environment. They serve as a review after you have completed the Prelab
Activities and Lab Exercises successfully.
For each of the following problems, write a program or a program segment that performs the specified action:
3. Write a line of code that accesses the seventh element of the array in Coding Exercise 2 and sets its value to 7.
4. Use the rand function to randomly select an element of the array created in Coding Exercise 1. Assign to that
element the value 2.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Coding Exercises
5. Write a function printArray that can print the contents of the array created in Coding Exercise 1. Assume
that the array’s size is passed as a second argument to the function. Place a space between every number that
is printed. In addition, print a new line after every 20 elements.
6. Write a program that generates a multiplication table. Use a double-subscripted array to represent your table.
The numbers used in the calculations can be in the range 1–5 (i.e., 5 * 5 = 25 is the largest value in this
table). Use a nested for statement to populate the array with the results of each calculation.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Challenges
Programming Challenges
Name: Date:
Section:
The Programming Challenges are more involved than the Coding Exercises and may require a significant amount
of time to complete. Write a C++ program for each of the problems in this section. The answers to these problems
are available from the Companion Website for C++ How to Program, Seventh Edition at www.pearsonhigh-
ered.com/deitel/. Pseudocode, hints and/or sample outputs are provided to aid you in your programming.
1. Use a one-dimensional array to solve the following problem. A company pays its salespeople on a commis-
sion basis. The salespeople each receive $200 per week plus 9 percent of their gross sales for that week. For
example, a salesperson who grosses $5000 in sales in a week receives $200 plus 9 percent of $5000, or a total
of $650. Write a program (using an array of counters) that determines how many of the salespeople earned
salaries in each of the following ranges (assume that each salesperson’s salary is truncated to an integer
amount):
a) $200–299
b) $300–399
c) $400–499
d) $500–599
e) $600–699
f) $700–799
g) $800–899
h) $900–999
i) $1000 and over
Hints:
• Calculate salary as a double. Then use static_cast< int > to truncate the salaries and convert them to
integers. Divide by 100 to obtain an array index.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Challenges
• Sample output:
2. Use a one-dimensional array to solve the following problem: Read in 20 numbers, each of which is between
10 and 100, inclusive. As each number is read, validate it and store it in the array only if it is not a duplicate
of a number already read. After reading all the values, display only the unique values that the user entered.
Provide for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve
this problem.
Hints:
• Compare every value input to all existing array elements. If it is a duplicate, set a flag variable to 1. This
flag should be used to determine whether it is necessary to print the value.
• Use a counter variable to keep track of the number of elements entered into the array and the array po-
sition where the next value should be stored.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Challenges
• Sample output:
3. (The Sieve of Eratosthenes) A prime integer is any integer greater than 1 that is evenly divisible only by itself
and 1. The Sieve of Eratosthenes is a method of finding prime numbers. It operates as follows:
a) Create an array with all elements initialized to 1 (true). Array elements with prime subscripts will remain
1. All other array elements will eventually be set to zero. You will ignore elements 0 and 1 in this exercise.
b) Starting with array subscript 2, every time an array element is found whose value is 1, iterate through
the remainder of the array and set to zero every element whose subscript is a multiple of the subscript
for the element with value 1. For array subscript 2, all elements beyond 2 in the array that are multiples
of 2 will be set to zero (subscripts 4, 6, 8, 10, etc.); for array subscript 3, all elements beyond 3 in the
array that are multiples of 3 will be set to zero (subscripts 6, 9, 12, 15, etc.); and so on.
When this process is complete, the array elements that are still set to one indicate that the subscript is a prime
number. These subscripts can then be printed. Write a program that uses an array of 1000 elements to de-
termine and print the prime numbers between 2 and 999. Ignore elements 0 and 1 of the array.
Hints:
• Use a loop to find all elements that are set to 1. (This must be done in order.) Set all multiples of that
element to 0.
• Print the primes by looping through the array searching for elements equal to 1. Print their subscript.
Increment a counter by one each time a prime number is printed.
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.
Programming Challenges
• Sample output:
2 is a prime number.
3 is a prime number.
5 is a prime number.
7 is a prime number.
11 is a prime number.
13 is a prime number.
17 is a prime number.
19 is a prime number.
23 is a prime number.
29 is a prime number.
31 is a prime number.
...
929 is a prime number.
937 is a prime number.
941 is a prime number.
947 is a prime number.
953 is a prime number.
967 is a prime number.
971 is a prime number.
977 is a prime number.
983 is a prime number.
991 is a prime number.
997 is a prime number.
A total of 168 prime numbers were found.
4. Write a recursive function recursiveMinimum that takes an integer array, a starting subscript and an ending
subscript as arguments and returns the smallest element of the array.The function should stop processing
and return when the starting subscript equals the ending subscript. [Note: This problem is intended for those
students who have studied recursion in Sections 6.19–6.21 of C++ How to Program, Seventh Edition.]
Hints:
• Write a program to test your function. Populate an array with randomly generated integers.
• Function recursiveMinimum should take as its arguments the array, a low value and a high value. The
low and high values represent the boundaries of the array, respectively.
• Recursive functions involving arrays approach their base case by reducing the size of the array using
boundaries, not by literally passing a smaller array.Your function should approach the base case in the
following manner: increment low by one each time until low equals high.
• Sample output:
© 2012 Pearson Education, Inc., Upper Saddle River, NJ. All Rights Reserved.