C++ language
I need exercise 5.1 and 5.2. answers PLEASE
The following program reads a file containing some integer values (we do not know how many values). It displays the number, the number to the power of two, and the sum up to that point. At last it displays the average of these numbers.
// P51.cpp - This program reads some integers from a file and
displays:
// The number, number^2, and the sum of all numbers read up to that
point
#include
#include // Step (1)
#include
#include
using namespace std;
int main( )
{
int x, count = 0;
float sum = 0, avg;
char input_file[15]; // Step
(2)-A
ifstream in_s; // Step (2)-B - declaration of the stream of type input
cout << "Please input the input file name \n"; //
Step (3)-A Get the file name
cin >> input_file;
in_s.open(input_file); // Step (3)-B
- connect to the input file and test
if(in_s.fail( ))
{
cout << "Input file
opening failed. \n";
exit(1); // if we couldn't
open the file to read from we exit
}
cout << "\t x \t\t x^2 \t\t Current Sum
\n";
cout << "\t === \t\t === \t\t ==========
\n";
while( in_s >> x) // Step
(4)-Read all numbers one-by-one to the end of the
file
{
sum = sum +
x;
cout << "\t
" << x <<"\t\t " << pow(x,2) << "\t\t "
<< sum << "\n";
count++;
}
avg = sum/count;
cout << "\n \t\t The average of these " << count
<< " numbers is: " << avg << endl;
in_s.close( ); // Step (5)-Close the connection (close the file)
return 0;
}
Every input or output file seems to have two names. The actual names, or external file, is the name of the file as it appears on your computer disk. The name of the stream used to read from/write to a file is referred to as internal file name. The internal name is actually the name of the stream that you are using to read from/write to the input/output file.
Introduction to Classes and Objects
In C++ the streams cin, cout, and any other
streams that you may declare are called objects.
You use the class ifstream to define
objects of type ifstream and class
ofstream to declare objects of type ofstream. An
object is a new type of variable that allows you to include
functions, as well as data, in it. An object may include several
variables and functions of various types. In the above program, we
have an object called in_s. This object
has several functions that were used in that program. These
functions are open(....), close( ), and fail( ). The functions are
called member functions. Note that you would have
similar functions for a stream of type output, but they may not
have the same functionality as they are members of a different
object type. As you can see in the above program, the member
functions are called using the dot operator (ex.
in_s.close( ) ). The period between the in_s
(calling object) and the close( ) (member
function) is referred to as the dot
operator.
Exercise 5.1
Carefully copy or cut and paste the program P51.cpp into a file
called ex51.cpp. Compile and run the program to make sure it
compiles without any error. Create a file, input.txt, and type the
following 8 integers in it: 4 4 4 4 4 4 4 4. Save the file and
exit. Now, run your program and when the program asks for the input
file, type input.txt. Your program should display an output similar
to the one given below:
Please input the input file name.
input.txt
x
x^2
Current Sum
===
===
==========
4
16
4
4
16
8
4
16
12
4
16
16
4
16
20
4
16
24
4
16
28
4
16
32
The average of these 8 numbers is: 4
This is the correct result.
Exercise 5.2
Modify the above program such that the program writes the output to
a file called output.txt, instead of displaying it.
#include <iostream>
#include <iomanip>
#include<fstream>
#include<math.h>
using namespace std;
int main( )
{
int x, count = 0;
float sum = 0, avg;
char input_file[15],output_file[15]; // Step (2)-A
ifstream in_s; // Step (2)-B - declaration of the stream of type
input
ofstream out_s; // declaration of the stream of type output
cout << "Please input the input file name \n"; // Step
(3)-A Get the file name
cin >> input_file;
in_s.open(input_file); // Step (3)-B - connect to the input file
and test
if(in_s.fail( ))
{
cout << "Input file opening failed. \n";
exit(1); // if we couldn't open the file to read from we exit
}
cout << "Please input the output file name \n"; // A Get
the output file name
cin >> output_file;
out_s.open(output_file); // Open output file
// Write headers to output file
out_s << "\t x \t\t x^2 \t\t Current Sum \n";
out_s << "\t === \t\t === \t\t ========== \n";
while( in_s >> x) // Step (4)-Read all numbers one-by-one
to the end of the file
{
sum = sum + x;
// Write to output file
out_s << "\t " << x <<"\t\t " << pow(x,2)
<< "\t\t " << sum << "\n";
count++;
}
avg = sum/count;
out_s << "\n \t\t The average of these " << count
<< " numbers is: " << avg << endl;
in_s.close( ); // Step (5)-Close the connection (close the
file)
out_s.close(); // Close the output file
return 0;
}
OUTPUT

OUTPUT FILE - output.txt

C++ language I need exercise 5.1 and 5.2. answers PLEASE The following program reads a file...