Question

Write a function template named summarray, with two parameters: and array called are and an int...

Write a function template named summarray, with two parameters: and array called are and an int holding the number of elements in arr. The function should return the sum of the elements in arr. this template must work for double or int arrays.

Show a sample function call to the template function you wrote.

What would you have to do to use the templates sumArray function with an array of objects of a custom class?   
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

#include <iostream>
using namespace std;

template <class T>
T summarray (T arr[], int num) { // template function with two parameter
T result;
int i=0;
for(i=0;i<num;i++){
result=result+arr[i]; // summing array
}
return (result); //return result
}

int main () {
int arr[20]; // declaration
double arrdouble[20];
int ch;
int ex;
int n,i,sum;
double sumdouble;
cout<<"\n Enter number of elements for array \t";
cin>>n;
do{
    cout<<"\n 1. Sum integer elements 2. Sum double elements \t";
    cin>>ch;
    switch(ch){
        case 1: cout<<"\n Enter "<<n<<" integer number \t "; // accept integer number
        for(i=0;i<n;i++){
        cin>>arr[i];
        }
        sum=summarray<int>(arr,n); // call template function with two parameter
        cout << sum << endl;
        break;
        case 2: cout<<"\n Enter "<<n<<" double number \t "; // accept double number
        for(i=0;i<n;i++){
        cin>>arrdouble[i];
        }
        sumdouble=summarray<double>(arrdouble,n); // call template function with two parameter
        cout << sumdouble << endl;
        break;
    }
    cout<<"\n Press 0 for exit otherwise press any number \t";
    cin>>ex;
}while(ex!=0);



}

/*

output:

Enter number of elements for array     4                                                                                              

                                                                                                                                       

1. Sum integer elements 2. Sum double elements         1                                                                              

                                                                                                                                       

Enter 4 integer number          5 7 6 1                                                                                               

19                                                                                                                                     

                                                                                                                                       

Press 0 for exit otherwise press any number    5                                                                                      

                                                                                                                                       

1. Sum integer elements 2. Sum double elements         2                                                                              

                                                                                                                                       

Enter 4 double  number          5.4 1.2 0.3 8.4                                                                                       

15.3                                                                                                                                   

                                                                                                                                       

Press 0 for exit otherwise press any number    0  

*/

Add a comment
Know the answer?
Add Answer to:
Write a function template named summarray, with two parameters: and array called are and an int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters:...

    Code written in NASM Function called findLargest Write a function called findLargest that receives two parameters: an unsigned doubleword array and the length of the array. The function must return the value of the largest array member in eax. Preserve all registers (except eax) that are modified by the function. Write a test program in main that calls findLargest three times, each call using a different array with different lengths. Function called countHits Write a function called named countHits that...

  • Write a function named stats, that takes an array and the number of elements in the...

    Write a function named stats, that takes an array and the number of elements in the array as arguments. It must compute and print the minimum value in the array, maximum value in the array and the average value of all the values inside the array. Your function MUST be named stats Your function has two parameters in the following order: an array of type double The number of elements in the array, type int Your function should NOT return...

  • 3. Write Java methods to accomplish each of the following Write a method named simpleAry which...

    3. Write Java methods to accomplish each of the following Write a method named simpleAry which accepts and return nothing. It creates an array that can hold ten integers. Fill up each slot of the array with the number 113. Then, display the contents of the array on the screen. You must use a loop to put the values in the array and also to display them. Write a method named sury which accepts an array of type double with...

  •     (10 pts) Define a two-dimensional array named temp with three rows and four columns of type int such that the first...

        (10 pts) Define a two-dimensional array named temp with three rows and four columns of type int such that the first row is initialized to 6, 8, 12, 9; the second row is initialized to 10, 13, 6, 16; and the third row is initialized to 27, 5, 10, 20.     (10 pts) Consider the following declarations, and see p.566: enum brands = { GM, FORD, TOYOTA, BMW, KIA }; const int N_BRANDS = 5; const int COLOR_TYPES = 6; double...

  • 1. Write a C function named find that receives a one-dimensional array of type integer named...

    1. Write a C function named find that receives a one-dimensional array of type integer named arr and its size of type integer. The function fills the array with values that are the power of four (4^n) where n is the index. After that, the function must select a random index from the array and move the array elements around the element stored in the randomly selected index Example arr = [1, 4, 16, 64, 256) if the randomly selected...

  • 5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below....

    5. (40 Points) Write a complete Java class named ArrayMethods that implements the methods listed below. All the methods accept the following parameters: Parameters: intar An array of int values. int firstIndex The index of the first element to include in the sum. int lastIndex The index of the last element to include in the sum. Please note that all methods must verify the validity of first Index and lastIndex. public static int sum(int[] arr, int first Index, int lastIndex)...

  • C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array...

    C Programming Language 2(a) Define a struct with 1 int array named i, 1 float array named f, and one double array named d, each of size M. (b)Declare array x with N of those structs. (c)Write a void function to traverse array x (using a pointer) assigning to each element in each array d (in each struct in array x) the sum of the corresponding elements in arrays i and f (in the same struct). Use 3 pointers (of...

  • Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify...

    Submissions) Part A Type and run the Array class template discussed in lecture (Templates notes). Modify the class by adding sort member function (refer to Algorithm Analysis notes for sorting algorithms) Sample usage: a. sort(); Add the needed code in main to test your function. template <class T> 1/you can use keyword typename instead of class class Array { private: T *ptr; int size; public: Array(T arr[], int s); void print(); template <class T> Array<T>:: Array (T arr[], int s)...

  • Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

  • Write a C function named add with the following parameters: three float arrays a, b and...

    Write a C function named add with the following parameters: three float arrays a, b and c, each of the three arrays is a two dimensional array. The function should add the first two arrays a and b (entry by entry), storing the results in the third array c. In the main function, call the add function with appropriate values. The main function should print the sum of the two arrays stored in the third array. Sample run: float al...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT