
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Program for Absolute Sum of Array Elements
In this problem, we are given an array of integers, and our task is to calculate the absolute sum of its elements. The absolute sum means the sum of the absolute values of all the array elements. In this article, we are going to learn how to compute the absolute sum of an array's elements using C++.
Example 1
Input:
arr = {-3, 2, -7, 4}
Output:
16
Explanation:
The absolute values of the array elements are: | -3 | = 3, | 2 | = 2, | -7 | = 7, | 4 | = 4. The sum of these absolute values is: 3 + 2 + 7 + 4 = 16.
Example 2
Input:
arr = {-1, -1, -1}
Output:
3
Explanation: The absolute values of the array elements are: | -1 | = 1, | -1 | = 1, | -1 | = 1. The sum of these absolute values is: 1 + 1 + 1 = 3
Below are different approaches to finding the absolute sum of an array's elements.
Using Loop Approach
In this approach, we use a loop to iterate through the array and calculate the absolute sum of array elements by adding the absolute value of each element. After traversing the complete array, return the final result.
Steps for Implementation
- Define the array and initialize its elements.
- Use a loop to traverse the array.
- For each element, calculate its absolute value and add it to a sum variable.
- Output the result.
Implementation Code
#include<bits/stdc++.h> using namespace std; int main() { int arr[] = {-5, 2, -3, 7, -1}; int n = sizeof(arr) / sizeof(arr[0]); int sum = 0; // Calculate the absolute sum for (int i = 0; i < n; i++) { sum += abs(arr[i]); // Add the absolute value of each element } // Output the result cout << "The absolute sum of array elements is: " << sum << endl; return 0; }
Output
Following is the output of the above program ?
The absolute sum of array elements is: 18
Time Complexity: O(n)
Space Complexity: O(1).
Using a Function
In this approach, we use the function to calculate the absolute sum of array elements of the array. The function is used to make code reusable and easier to maintain.
Steps for Implementation
- Create a function that takes the array and its size as a parameter.
- Use a loop inside the function to calculate the absolute sum.
- Call the function and pass the array as an argument.
- Output the result.
Implementation Code
#include<bits/stdc++.h> using namespace std; // Function to calculate absolute sum int absoluteSum(int arr[], int n) { int sum = 0; for (int i = 0; i < n; i++) { sum += abs(arr[i]); } return sum; } int main() { // Define the array int arr[] = {-5, 2, -3, 7, -1}; int n = sizeof(arr) / sizeof(arr[0]); int result = absoluteSum(arr, n); // Output cout << "The absolute sum of array elements is: " << result << endl; return 0; }
Output
Following is the output of the above program ?
The absolute sum of array elements is: 18
Time Complexity: O(n).
Space Complexity: O(1).
Using Standard Template Library (STL)
STL (Standard Template Library) is a set of template classes and functions that provides an inbuilt implementation of many programming tools and data structures. In C++, we use the STL std::accumulate function combined with a lambda to compute the absolute sum of an array.
Steps for Implementation
- We use std::accumulate to iterate through the array.
- Provide a lambda function to calculate the cumulative absolute sum.
- The result returned by std::accumulate is stored in a variable.
- Finally, print the absolute sum output.
Implementation Code
#include<bits/stdc++.h> using namespace std; int AbsoluteSumSTL(int arr[], int n) { return accumulate(arr, arr + n, 0, [](int sum, int val) { return sum + abs(val); }); } int main() { int arr[] = {-3, 2, -7, 4}; int n = sizeof(arr) / sizeof(arr[0]); int result = AbsoluteSumSTL(arr, n); cout << "The absolute sum of the array elements is: " << result << endl; return 0; }
Output
Following is the output of the above program ?
The absolute sum of the array elements is: 16
Time Complexity: O(N)
Space Complexity: O(1)
Real-Life Applications
Data Analysis: Calculating absolute sums is a common task in situations where the sign of data points is unimportant, such as finding total deviations in statistics.
Signal Processing: Absolute values are used to measure amplitudes in signals, where negative values represent direction rather than magnitude.
Mathematics: It helps in calculations that deal with magnitudes or total distances, irrespective of direction.