Overload function template printArray of Fig. 14.1 so that it takes two additional integer arguments, namely int lowSubscript and int highSubscript. A call to this function will print only the designated portion of the array. Validate lowSubscript and highSubscript; if either is out of range or if highSubscript is less than or equal to lowSubscript, the overloaded printArray function should
return 0; otherwise, printArray should return the number of elements printed. Then modify main to exercise both versions of printArray on arrays a, b and c (lines 23–25 of Fig. 14.1). Be sure to test all capabilities of both versions of printArray.
What will be an ideal response?
```
// Using template functions
#include
using namespace std;
// function template printArray definition
// original function from Fig. 14.1
template< typename T >
void printArray( const T *array, int count )
{
// display array
for ( int i = 0; i < count; i++ )
cout << array[ i ] << " ";
cout << endl;
} // end function printArray
// overloaded function template printArray
// takes upper and lower subscripts to print
template< typename T >
int printArray( T const * array, int size, int lowSubscript,
int highSubscript )
{
// check if subscript is negative or out of range
if ( size < 0 || lowSubscript < 0 || highSubscript >= size )
return 0;
int count = 0;
// display array
for ( int i = lowSubscript; i <= highSubscript; i++ )
{
count++;
cout << array[ i ] << ' ';
} // end for
cout << endl;
return count; // number of elements output
} // end overloaded function printArray
int main()
{
const int ACOUNT = 5; // size of array a
const int BCOUNT = 7; // size of array b
const int CCOUNT = 6; // size of array c
// declare and initialize arrays
int a[ ACOUNT ] = { 1, 2, 3, 4, 5 };
double b[ BCOUNT ] = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7 };
char c[ CCOUNT ] = "HELLO"; // 6th position for null
int elements;
// display array a using original printArray function
cout << "\nUsing original printArray function\n";
printArray( a, ACOUNT );
// display array a using new printArray function
cout << "Array a contains:\n";
elements = printArray( a, ACOUNT, 0, ACOUNT - 1 );
cout << elements << " elements were output\n";
// display elements 1-3 of array a
cout << "Array a from positions 1 to 3 is:\n";
elements = printArray( a, ACOUNT, 1, 3 );
cout << elements << " elements were output\n";
// try to print an invalid element
cout << "Array a output with invalid subscripts:\n";
elements = printArray( a, ACOUNT, -1, 10 );
cout << elements << " elements were output\n\n";
// display array b using original printArray function
cout << "\nUsing original printArray function\n";
printArray( b, BCOUNT );
// display array b using new printArray function
cout << "Array b contains:\n";
elements = printArray( b, BCOUNT, 0, BCOUNT - 1 );
cout << elements << " elements were output\n";
// display elements 1-3 of array b
cout << "Array b from positions 1 to 3 is:\n";
elements = printArray( b, BCOUNT, 1, 3 );
cout << elements << " elements were output\n";
// try to print an invalid element
cout << "Array b output with invalid subscripts:\n";
elements = printArray( b, BCOUNT, -1, 10 );
cout << elements << " elements were output\n\n";
// display array c using original printArray function
cout << "\nUsing original printArray function\n";
printArray( c, CCOUNT );
// display array c using new printArray function
cout << "Array c contains:\n";
elements = printArray( c, CCOUNT, 0, CCOUNT - 2 );
cout << elements << " elements were output\n";
// display elements 1-3 of array c
cout << "Array c from positions 1 to 3 is:\n";
elements = printArray( c, CCOUNT, 1, 3 );
cout << elements << " elements were output\n";
// try to display an invalid element
cout << "Array c output with invalid subscripts:\n";
elements = printArray( c, CCOUNT, -1, 10 );
cout << elements << " elements were output" << endl;
} // end main
```
Using original printArray function
1 2 3 4 5
Array a contains:
1 2 3 4 5
5 elements were output
Array a from positions 1 to 3 is:
2 3 4
3 elements were output
Array a output with invalid subscripts:
0 elements were output
Using original printArray function
1.1 2.2 3.3 4.4 5.5 6.6 7.7
Array b contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7
7 elements were output
Array b from positions 1 to 3 is:
2.2 3.3 4.4
3 elements were output
Array b output with invalid subscripts:
0 elements were output
Using original printArray function
H E L L O
Array c contains:
H E L L O
5 elements were output
Array c from positions 1 to 3 is:
E L L
3 elements were output
Array c output with invalid subscripts:
0 elements were output
You might also like to view...
What is the difference between a spanning tree and a minimum spanning tree?
What will be an ideal response?
Match the following one or more keystrokes to their function:
I. Ctrl II. Shift III. Ctrl + C IV. Ctrl + V V. flag-type emblem key A. copy B. paste C. used to select two or more files that are not adjacent to one another D. used to display the Start menu E. used to select several contiguous files
Participants' first and last names will be stored in the Putt for Paws tournament. This is an example of a(n) ________
Fill in the blank(s) with correct word
In addition to sales, CRM, and SCM, the modules in an ERP system support business activities such as _____________.
A. product planning B. manufacturing and purchasing C. inventory control and distribution D. all of the above