← Back to Streams
Question 608
Squares Table
Squares Table
Write a functionvoid writeSquares(const string& filename, int n)that opensfilenamewith anofstreamand writes one line for every integer k from 1 to n (n is always at least 1). Each line holds k, a single space, and k*k, followed by a newline. ForwriteSquares("squares_a.txt", 3)the file must contain exactly (see Expected Output). The test reopens the file and prints every line, so that is also the output. Do NOT write a main function.
Expected Output:
1 1 2 4 3 9 1 1 2 4 3 9 4 16 5 25
= newline. The output ends with a newline — print one after the last line.
Hints3 available
Sign in to use hintsTopics:
Streamsofstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · 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.