transform

Lab C++ Intermediate 20 points

Read n integers. Use transform with a lambda to square them into a second vector, then print it.

Input

Line 1: n. Line 2: n integers.

Output

One line.

Example

Input

5
1 2 3 4 5

Output

1 4 9 16 25
Sample input available Sample output available
Editor
C++

Hints

transform(in_begin, in_end, out_begin, function)

Solution explanation

STL algorithms plus lambdas replace many hand-written loops.

Show solution
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    vector<long long> v(n), sq(n);
    for (auto &x : v) cin >> x;
    transform(v.begin(), v.end(), sq.begin(), [](long long x) { return x * x; });
    for (int i = 0; i < n; i++) cout << sq[i] << (i < n - 1 ? " " : "\n");
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD