valarray sum() in C++ Last Updated : 23 Oct, 2018 Comments Improve Suggest changes Like Article Like Report The sum() function is defined in valarray header file. This function returns the sum of all the elements in the valarray, as if calculated by applying operator+= to a copy of one element and all the other elements, in an unspecified order. Syntax: T sum() const; Returns: This function returns the sum of all the elements in the valarray. Below programs illustrate the above function: Example 1:- CPP // C++ program to demonstrate // example of sum() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing valarray valarray<int> varr = { 15, 10, 30, 33, 40 }; // Displaying sum of valarray cout << "The sum of valarray is = " << varr.sum() << endl; return 0; } Output: The sum of valarray is = 128 Example 2:- CPP // C++ program to demonstrate // example of sum() function. #include <bits/stdc++.h> using namespace std; int main() { // Initializing valarray valarray<int> varr = { 1, 2, 3, 4, 5 }; // Displaying sum of valarray cout << "The sum of valarray is = " << varr.sum() << endl; return 0; } Output: The sum of valarray is = 15 Comment More infoAdvertise with us Next Article valarray sum() in C++ bansal_rtk_ Follow Improve Article Tags : Misc C++ Programs C++ CPP-Functions cpp-valarray +1 More Practice Tags : CPPMisc Similar Reads valarray size() function in C++ The size() function is defined in valarray header file. This function is used to find the size of valarray and returns the size of valarray. Syntax: size_t size() const; Parameter: This function doesn't takes any parameter. Returns: This function returns the number of element in valarray. Below prog 1 min read valarray resize() function in C++ The resize() function is defined in valarray header file. This function resizes the valarray to contain n elements and assigns value to each element.Syntax: void resize( size_t n, T value = T() ); Parameter: This method accepts two parameters: n: It represents the new size of valarray.value: It repr 2 min read Array sum in C++ STL Array sum can be defined as the sum of all elements of an array. In this article, we will learn how to find the array sum using C++ STL.ExamplesInput: arr[] = {5, 10, 15, 11, 9}Output: 50Explanation: As 5 + 10 + 15 + 11 + 19 = 50Input: arr[] = {1, 2, 3, 4, 5}Output: 15Explanation: As 1 + 2 + 3 + 4 + 2 min read Add Two Numbers in C++ Given two integers, the task is to add these integer number and print their sum in C++.ExamplesInput: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the two 3 min read Sum of all odd length subarrays Given an array arr[] consisting of N integers, the task is to find the sum of all the elements of all possible subarrays of odd length. Examples: Input: arr[] = {3, 2, 4}Output: 18Explanation:The odd length subarrays along with their sum are as follows:1) {3} = sum is 3.2) {2} = sum is 2.3) {4} = su 9 min read C/C++ Programs sArray C/C++ ProgramsC Program to find sum of elements in a given arrayC program to find largest element in an arrayRecursive C program to linearly search an element in a given arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum 15+ min read C++ Program to Count pairs with given sum Given an array of integers, and a number 'sum', find the number of pairs of integers in the array whose sum is equal to 'sum'. Examples: Input : arr[] = {1, 5, 7, -1}, sum = 6 Output : 2 Pairs with sum 6 are (1, 5) and (7, -1) Input : arr[] = {1, 5, 7, -1, 5}, sum = 6 Output : 3 Pairs with sum 6 are 4 min read Array Product in C++ Using STL The product of all elements is the result of multiplying all the elements within the specified range. In this article we will learn different methods to find the product of all elements of an array in C++ using STL.The simplest method to find the product of all elements in array is by using accumula 2 min read How to Find the Variance of Numbers in 2D Array in C++? The variance of numbers is a measure of the spread of a set of values which is used to find how much the values in a dataset differ from mean or average. In this article, we will learn how to find the variance of numbers in a 2D array in C++. Example Input: myMatrix = { { 10, 20, 30 }, { 40, 50, 60 3 min read How to Loop Over an Array in C++? In C++, an array is a data structure that stores elements of similar type in contiguous memory locations. We can access the elements of an array using array indexing. In this article, we will learn how to loop over an array in C++. Example: Input: int arr[] = [1, 2, 3, 4, 5] Output: Array Elements: 2 min read Like