Question

In: Computer Science

Topic: Template template void arrayContents(const T *arr, int countSize); int main() { const int ACOUNT =...

Topic: Template
template
void arrayContents(const T *arr, int countSize);
int main()
{
const int ACOUNT = 5;
const int BCOUNT = 7;
const int 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";
cout <<"Array A contents: \n";
arrayContents(a, ACOUNT);
cout <<"Array B contents: \n";
arrayContents(b, BCOUNT);
cout <<"Array C contents: \n";
arrayContents(c, CCOUNT);
return 0;
}
template
void arrayContents(const T *arr, int countSize)
{
for(int i = 0; i < countSize; i++)
cout << arr[i] << " ";
cout << endl;
}
Rewrite the above code and overload function template arrayContents(), so it will
takes two additional integer arguments, named int lowSubscript and int

highSubscript. The function shall validate the lowSubscript and
highSubscript; for:

 If both either is out of range, or
 If highSubscript is less than or equal to lowSubscript
hence,the overloaded arrayContents function should return 0. Otherwise, it
should return the number of elements printed.

Modify the main() function to exercise each versions of arrayContents on arrays
a, b and c.
Notes: the lowSubscript and highSubscript is to identify the index of array.

Solutions

Expert Solution


Given below is the modified code as per specification. Please do rate the answer if it helped. Thank you.

#include <iostream>
using namespace std;


template <typename T>
void arrayContents(const T *arr, int countSize);

template <typename T>
int arrayContents(const T *arr, int countSize, int lowSubscript, int highSubscript);

int main()
{
const int ACOUNT = 5;
const int BCOUNT = 7;
const int 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";
cout <<"Array A contents: \n";
arrayContents(a, ACOUNT);
cout <<"Array B contents: \n";
arrayContents(b, BCOUNT);
cout <<"Array C contents: \n";
arrayContents(c, CCOUNT);

cout << endl << "----------" << endl;
cout <<"Array A contents between index 1 and 3: \n";
arrayContents(a, ACOUNT, 1, 3);
cout <<"Array B contents between index 3 and 6: \n";
arrayContents(b, BCOUNT, 3,6);
cout <<"Array C contents between index 0 and 2: \n";
arrayContents(c, CCOUNT, 0, 2);
return 0;
}


template <typename T>
void arrayContents(const T *arr, int countSize)
{
for(int i = 0; i < countSize; i++)
cout << arr[i] << " ";
cout << endl;
}

template <typename T>
int arrayContents(const T *arr, int countSize, int lowSubscript, int highSubscript){
if(lowSubscript < 0 || highSubscript < 0 || highSubscript < lowSubscript ||
lowSubscript >= countSize || highSubscript >= countSize)
return 0;
  
int count = 0;
for(int i = lowSubscript; i <= highSubscript; i++){
cout << arr[i] << " ";
count++;
}
cout << endl;
return count;
}


Related Solutions

#include <stdio.h> const int DECK_SIZE = 52; int deck[DECK_SIZE]; int main(void) { /* Populate the deck...
#include <stdio.h> const int DECK_SIZE = 52; int deck[DECK_SIZE]; int main(void) { /* Populate the deck */ for(int i = 0; i < DECK_SIZE; i++) { deck[i] = i + 1; }    /* Get the cut position as an integer input */    /* Verify that the input is valid */    /* Cut the deck */    /* Print the resulting deck with one element on each line */ return 0; } Write a program that "cuts" a...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];...
Consider the following code: void swap(int arr[], int i, int j) {        int temp = arr[i];        arr[i] = arr[j];        arr[j] = temp; } void function(int arr[], int length) {        for (int i = 0; i<length / 2; i++)               swap(arr, i, (length / 2 + i) % length); } If the input to the function was int arr[] = { 6, 1, 8, 2, 5, 4, 3, 7 }; function(arr,8); What values would be stored in the array after calling the...
   private static void merge(int arr[], int l, int m, int r) {        //...
   private static void merge(int arr[], int l, int m, int r) {        // Find sizes of two subarrays to be merged        int n1 = m - l + 1;        int n2 = r - m;        /* Create temp arrays */        int L[] = new int[n1];        int R[] = new int[n2];        /* Copy data to temp arrays */        for (int i = 0; i...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int...
Debug this code getting segmentation faults. //array_utils.h int contains(const int *arr,int size , int k); int containsWithin(const int *arr,int size , int k,int i , int j); int *paddedCopy(const int *arr,int oleSize , int newSize); void reverse(int *arr,int size); int *reverseCopy(const int *arr,int size); //array_utils.c #include"array_utils.h" #include<stdlib.h> int contains(const int *arr,int size , int k){    int i=0;    for(i=0;i<size;i++){        if(arr[i] == k)return 1;    }    return 0; } int containsWithin(const int *arr,int size , int k,int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
Given the root C++ code: void sort() {    const int N = 10;    int...
Given the root C++ code: void sort() {    const int N = 10;    int x[N];    for(int i = 0; i < N; i++)    {        x[i] = 1 + gRandom-> Rndm() * 10;        cout<<x[i]<<" "; }    cout<<endl;    int t;       for(int i = 0; i < N; i++)    {    for(int j = i+1; j < N; j++)    {        if(x[j] < x[i])        {   ...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int&...
#include <iostream> using namespace std; const int DECLARED_SIZE = 10; void fillArray(int a[], int size, int& numberUsed) { cout << "Enter up to " << size << " nonnegative whole numbers.\n" << "Mark the end of the list with a negative number.\n"; int next, index = 0; cin >> next; while ((next >= 0) && (index < size)) { a[index] = next; index++; cin >> next; } numberUsed = index; } int search(const int a[], int numberUsed, int target) {...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT