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
Write out C++ code for the following: Declare a templated function that has a return type...
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 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 = 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 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 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 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...
Write a C program to do the following...in this EXACT order: a. Declare an integer array named alpha of 50 elements. b. Use a for loopt to initialize each element of alpha to its index value (e.g. alpha[0] = 0, alpha[1]=1, etc.). c. Output the value of the first element of the array alpha. d. Output the value of the last element of alpha. e. Set the value of the 25th element of the array alpha to 62 and output...
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",...
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...
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....