Description: This problem asks you to create a program that takes a list of numbers as input and calculates the running sum of the list. The running sum is a sequence where each element is the sum of all previous elements and the current element in the input list. The program should output the running sum at each step.
function runningSum(arr)
sum ← 0
for i from 0 to length(arr) - 1
sum ← sum + arr[i]
print sum
end for
end function
// Example usage:
list ← {1, 2, 3, 4}
runningSum(list)
// Output: 1 3 6 10
def runningSum(arr):
total = 0
for num in arr:
total += num
print(total)
# Example usage
nums = [1, 2, 3, 4]
runningSum(nums)
# Output: 1 3 6 10
public class Main {
public static void runningSum(int[] arr) {
int sum = 0;
for (int num : arr) {
sum += num;
System.out.print(sum + " ");
}
}
public static void main(String[] args) {
int[] nums = {1, 2, 3, 4};
runningSum(nums);
// Output: 1 3 6 10
}
}
#include <iostream>
using namespace std;
void runningSum(int arr[], int length) {
int sum = 0;
for (int i = 0; i < length; i++) {
sum += arr[i];
cout << sum << " ";
}
}
int main() {
int nums[] = {1, 2, 3, 4};
int length = sizeof(nums) / sizeof(nums[0]);
runningSum(nums, length);
// Output: 1 3 6 10
return 0;
}