(Print Array Range) Overload function template printArray of Fig. 14.1 so that i
ID: 3633767 • Letter: #
Question
(Print Array Range)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 22-24 of Fig 14.1) Be sure to test all capabilities of both versions of printArray.
Please provide screenshot when posting your answer.
Explanation / Answer
Dear,
Here is the code
// Using template functions
#include < iostream >
using namespace std;
template< class T >
int printArray( T const * const array, int size, int lowSubscript,
int highSubscript )
{
if ( size < 0 || lowSubscript < 0 || highSubscript >= size )
return 0; // negative size or subscript out of range
for ( int i = lowSubscript, count = 0; i <= highSubscript; ++i )
{
++count;
cout << array[ i ] << ' ';
}
cout << ' ';
return count; // number or elements output
}
int main()
{
const int aCount = 5, bCount = 7, cCount = 6;
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;
cout << " Array a contains: ";
elements = printArray( a, aCount, 0, aCount - 1 );
cout << elements << " elements were output ";
cout << "Array a from 1 to 3 is: ";
elements = printArray( a, aCount, 1, 3 );
cout << elements << " elements were output ";
cout << "Array a output with invalid subscripts: ";
elements = printArray( a, aCount, -1, 10 );
cout << elements << " elements were output ";
cout << "Array b contains: ";
elements = printArray( b, bCount, 0, bCount - 1 );
cout << elements << " elements were output ";
cout << "Array b from 1 to 3 is: ";
elements = printArray( b, bCount, 1, 3 );
cout << elements << " elements were output ";
cout << "Array b output with invalid subscripts: ";
elements = printArray( b, bCount, -1, 10 );
cout << elements << " elements were output ";
cout << "Array c contains: ";
elements = printArray( c, cCount, 0, cCount - 1 );
cout << elements << " elements were output ";
cout << "Array c from 1 to 3 is: ";
elements = printArray( c, cCount, 1, 3 );
cout << elements << " elements were output ";
cout << "Array c output with invalid subscripts: ";
elements = printArray( c, cCount, -1, 10 );
cout << elements << " elements were output" << endl;
//pause system for a while
system("pause");
return 0;
}//end main
Output:
Array a contains:
1 2 3 4 5
5 elements were output
Array a from 1 to 3 is:
2 3 4
3 elements were output
Array a output with invalid subscripts:
0 elements were output
Array b contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7
7 elements were output
Array b from 1 to 3 is:
2.2 3.3 4.4
3 elements were output
Array b output with invalid subscripts:
0 elements were output
Array c contains:
H E L L O
6 elements were output
Array c from 1 to 3 is:
E L L
3 elements were output
Array c output with invalid subscripts:
0 elements were output
Hope this will help you..
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.