Challenge: Running Totals with new

Lab C++ Intermediate 20 points

Read n and then n numbers. Use new int[n] to make an array of running totals: item i is the sum of the first i + 1 numbers. Print them on one line, then delete[] the array.

Input

n (1 or more), then n integers.

Output

n running totals separated by spaces.

Example

Input

4
1 2 3 4

Output

1 3 6 10
Sample input available Sample output available
Editor
C++

Hints

Keep a running sum while you read.

totals[i] = sum; after adding the i-th number.

Arrays made with new[] are freed with delete[].

Solution explanation

The array size is only known at run time, so it's created with new int[n] and freed with delete[].

Show solution
#include <iostream>
using namespace std;

int main() {
    int n;
    cin >> n;
    int* totals = new int[n];
    int sum = 0;
    for (int i = 0; i < n; i++) {
        int x;
        cin >> x;
        sum += x;
        totals[i] = sum;
    }
    for (int i = 0; i < n; i++) {
        cout << totals[i];
        if (i < n - 1) cout << " ";
    }
    cout << "\n";
    delete[] totals;
    return 0;
}
Output
Answer
HOME LABS COMMUNITY DASHBOARD