โ Back to Arrays
Question 123
Running Sum (Prefix Sum)
Running Sum (Prefix Sum)
An array called nums has already been created for you with the values {10, 20, 30, 40, 50}.
Your task is to print the running sum (also called a prefix sum) at each position. The running sum at each position is the total of all elements from the start of the array up to and including that position.
Here's how it works:
- After index 0: 10 (just the first element)
- After index 1: 10 + 20 = 30
- After index 2: 10 + 20 + 30 = 60
- After index 3: 10 + 20 + 30 + 40 = 100
- After index 4: 10 + 20 + 30 + 40 + 50 = 150
Keep a running total variable. As you loop through, add each element to it and print the total so far.
Print the running sums on one line separated by spaces.
Expected output:
10 30 60 100 150 Expected Output:
10 30 60 100 150
Topics:
Arrays
Code Editor
1
Tab to indent ยท Ctrl+Enter to run ยท Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.