C++ Homework # 1: Write a program that does the following: Reads from a file 10
ID: 3529154 • Letter: C
Question
C++ Homework # 1: Write a program that does the following: Reads from a file 10 integers (one per line) - file name Integers.dat Squares each of the integers. Adds the 10 integers together Adds the squares of the integers together Writes each of the integers and its sum into a file called 'SumofSquares.dat' The last line of the file should contain the total of all the integers and the total of all of the squares of all of the integers. Example of the input file (use any integers you prefer): 12 4 223 76 54 90 67 543 1 3 Example of the output file: 144 16 49729 5776 2916 8100 4489 294849 1 9 Integer total = 1073 Squares total = 366029Explanation / Answer
#include<iostream>
using namespace std;
int main()
{
int i;
int sum = 0;
int sum_s = 0;
ifstream input("Integers.dat");
ofstream output("SumOfSquares.dat");
while(input >> i)
{
output << (i*i) << endl:
sum = sum + i;
sum_s = sum+s + (i*i);
}
input.close();
output << " Interger total = " << sum << endl;
output << "Squares total = " << sum_s << endl;
output.close();
return 0;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.