Question

Write out C++ code for the following: Declare a templated function that has a return type of T. It has two parameters: An array of T items named arr, and an integer named size. This function is assumed to work with numerical data types. Within the function, create a new T variable named sum and initialize it to 0. Use a for-loop to go from 0 (inclusive) to size (exclusive), adding each element from arr to the sum. Finally, return the value of sum after the for-loop has finished.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PROGRAM

// I am taken array size 10 if want increase this using pointers or array size

#include <iostream>
using namespace std;
  
template <class T, int max> // Create Template object and integer number
T sum(T arr[], int size) // Create template function with two arguments
{
T m = max; // Declare integer variable and store the template argument max
T s=0; // Declare and assign sum of Template variable
for (int i = 0; i < size; i++){
m = arr[i]; // assign array elements to template second argument max
s+=m; // calculate sum of array elements
}
  
return s; // return template sum
}
  
int main()
{
int arr[10]; // Declare integer array variable arr
float a[10]; // Delcare float array variable a
int n; // Declare integer n
cout<<"\nEnter the Size of an Array: ";
cin>>n;
cout<<"\nRead integer Array elemnts\n";
for(int i=0;i<n;i++)
cin>>arr[i]; // Read integer array
  
cout<<"\nRead Float Array elemnts\n";
for(int i=0;i<n;i++)
cin>>a[i]; // REad float array
  
cout <<"\nSum of Integer Array Elements: "<<sum<int, 1000>(arr, n) << endl; // call function sum() and print sum of integer array elements
cout <<"\nSum of Float Array Elements: "<<sum<float, 1000>(a, n) << endl; // call function sum() and print sum of float array elements
return 0;
}

OUTPUT


Enter the Size of an Array: 5

Read integer Array elemnts
10
20
35
48
96

Read Float Array elemnts
10.52
12.63
20.56
42.36
85.36

Sum of Integer Array Elements: 209

Sum of Float Array Elements: 171.43

Add a comment
Know the answer?
Add Answer to:
Write out C++ code for the following: Declare a templated function that has a return type...
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
  • Write a templated function sumList that will take in an array of any type and the...

    Write a templated function sumList that will take in an array of any type and the length of the array. It should add all the elements in the array and return the total. Hint: you can declare a variable sum of type T and initialize it like this: T sum {}; The {} are the best way to make sure that numbers get set to 0, a string gets created as empty etc... #include <iostream> using namespace std; //Do not...

  • 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...

  • Write a C code to perform the following tasks: In main: Define an array of SIZE...

    Write a C code to perform the following tasks: In main: Define an array of SIZE = 7 and initialize them with 3, 5, 7, 9, 11, 13, 15 Call function sum with the array and size as parameters Print each element of the array Print the sum In function sum. Use for-loop to calculate sum

  • Write a C program to do the following: 1. Write a C function named find that...

    Write a C program to do the following: 1. Write a C function named find that receives a one-dimensional array of type character named arr and its size of type integer. After that, the function must select a random index from the array. The function must find if the character stored in the randomly selected index comes before all the characters to its left alphabetically. Finally, the function prints to the screen the element if the element meets the condition...

  • Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory...

    Write the code to dynamically allocate ONE integer variable using calloc (contiguous allocation) or malloc (memory allocation) and have it pointed to by a pointer (of type int * ) named ptr_1. Use ptr_1 to assign the number 7 to that dynamically allocated integer, and in another line use printf to output the contents of that dynamically allocated integer variable. Write the code to dynamically allocate an integer array of length 5 using calloc or malloc and have it pointed...

  • c++ code Lab Exercises This assignment consists of two exercises. Exercise 1- Count Inversions Exercise Objectives...

    c++ code Lab Exercises This assignment consists of two exercises. Exercise 1- Count Inversions Exercise Objectives Define and call functions ✓ Using Array Using nested loop ✓ Using input/output statement Problem Description Inside "Lab2" folder, create a project named "Lab2Ex1". Use this project to write and run a C++ program that produces: Define a constant value called A_SIZE with value of 10. ► Declare an integer 10 array of size A_SIZE called Arr. Define a function called Read that accept...

  • a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a...

    a) Declare and instantiate an array named scores of twenty-five elements of type int.   (b)Write a statement that declares an array namedstreetAddress that contains exactly eighty elements of typechar. 2. In a single statement: declare, create and initialize an arraynamed a of ten elements of type int with the values of the elements (starting with the first) set to 10, 20,..., 100 respectively. 3. Declare an array reference variable, week, and initialize it to an array containing the strings "mon",...

  • C++ Programming 1. Write a function (header and body) that accepts an integer as an argument...

    C++ Programming 1. Write a function (header and body) that accepts an integer as an argument and returns that number squared. 2. Write the code that will fill an integer array named multiples with 10 integers from 5 to 50 that are multiples of five. (This should NOT be in the form of an initialization statement.) 3. Write the code that will display the contents of the integer array named multiples. Recall: the size of the array is 10. 4....

  • *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create...

    *Answer must be in C* Write a complete program as follows (declare any necessary variables): Create an array of doubles named “hummus” with 2 rows and 300 columns. Initialize all array elements to zero. Write a loop that will repeatedly ask the user to enter a value and store it in the first row of the array (do not overwrite previously entered values) until the whole first row is populated with the user input. (hint the loop should have the...

  • in C++ and also each function has its own main function so please do the main...

    in C++ and also each function has its own main function so please do the main function as well. Previously when i asked the question the person only did the function and didn't do the main function so please help me do it. 1-1. Write a function that returns the sum of all elements in an int array. The parameters of the function are the array and the number of elements in the array. The function should return 0 if...

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
ADVERTISEMENT