C++ Program of Absolute sum of array elements



Problem Description



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





Example 3




Input 


arr = { -2, -3, 4 }


Output 


9


Explanation:


The absolute values of the array elements are:

??2? = 2 , ??3? = 3 , ?4? = 4

The sum of these absolute values is:


2 + 3 + 4 = 9




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; // Initialize sum to 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:



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. We do not have to write the same logic multiple times, we only have to define a single function and call it whenever needed.





Steps for Implementation



  • Create a function that takes the array and its size as a parameter.
  • Now, 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; // Initialize sum to 0
for (int i = 0; i < n; i++) {
sum += abs(arr[i]); // Add the absolute value of each element
}
return sum; // Return the sum
}

int main() {
// Define the array
int arr[] = {-5, 2, -3, 7, -1};
int n = sizeof(arr) / sizeof(arr[0]); // Calculate the size of the array

// Call the function
int result = absoluteSum(arr, n);

// Output the result
cout << "The absolute sum of array elements is: " << result << endl;

return 0;
}




Output:


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 provide the built-in 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





  • Declare the input array or vector containing both positive and negative integers.
  • We use std::accumulate to iterate through the array.
  • Provide a lambda function to calculate the cumulative absolute sum.
  • Output the result. 





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:




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 not important, such as in 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.


Updated on: 2025-02-05T15:33:47+05:30

5 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements