And need to be writing in C++ language
Programm need to start with #include<fstream>
Prepare a text file data_in.txt with the following information
(highlight the piece of text below with numbers and copy it to a
text file):
54, 70, 75, 63, 17, 59, 87, 16, 93, 81, 60, 67, 90, 53, 88, 9, 61,
8, 96, 98, 12, 34, 66, 76, 38, 55, 58, 27, 92, 45, 41, 4, 20, 22,
69, 77, 86, 35, 19, 32, 49, 15, 29, 23, 83, 95, 25, 91, 33, 47, 24,
62, 13, 42, 73, 44, 78, 72, 7, 5, 10, 48, 71, 18, 39, 97, 64, 79,
51, 74, 31, 37, 57, 30, 94, 80, 28, 1, 56, 85, 46, 100, 82, 40, 26,
21, 68, 43, 14, 3, 65, 99, 89, 52, 84, 36, 2, 6, 11, 50
2. Write a program that:
a) Reads integers from the data_in.txt file, places them in an
array, and outputs the contents of the array to the screen.
b) The text file data_out.txt outputs:
* the contents of the array in reverse order, and
* the sum of all figures.
3. Try a situation where the input file has a different name than
your program, such as "datain.txt", and the program does not use
the is_open () function to check the existence of the file - put
the relevant lines of code in the comments.
4. Answer the questions:
a) How does the program "behave" - does it display an error
message?
b) What appears on the screen when the contents of an array are
output? What are they about data?
c) Is the information in the file “data_out.txt” output? If so,
what information?
3.The third question asks you to check what would change in the program if the name of the input file were changed. And the file is_open () function is added to the comments.
4.The fourth question is simply to answer the questions about how the program worked at the time.
ANSWER :
Answer to (1)
& (2)
:-
===============
#include <iostream> #include <fstream> #include
<string> using namespace std; int main() { int num = 0, cnt,
value, array_sum; string line; ifstream inFile; // input file
stream object ofstream outFile;// output file stream object int
num_array [200]; // array to store integers inFile.open
("data_in.txt", ios::in ); //opening input file
outFile.open("data_out.txt", ios::out); // opening output file cnt
= 0; while ( getline (inFile,line, ',') ) { num = stoi(line);
num_array[cnt] = num; array_sum += num; cnt++; } // Output array to
screen cout << "SET OF INTEGER VALUES READ FROM
data_input.txt FILE :" << endl; for(int i=0; i < cnt; i++)
{ cout << num_array[i] << "\t"; } // Writing reversed
integer to output file for(int i=cnt-1; i >= 0; i--) { outFile
<< num_array[i] << ", "; } outFile << endl;
outFile << array_sum; inFile.close(); outFile.close(); return
0; }
OUTPUT FILE :-

CONTENT OF data_output.txt file
-------------------------------------------------
50, 11, 6, 2, 36, 84, 52, 89, 99, 65, 3, 14, 43, 68, 21, 26, 40,
82, 100, 46, 85, 56, 1, 28, 80, 94, 30, 57, 37, 31, 74, 51, 79, 64,
97, 39, 18, 71, 48, 10, 5, 7, 72, 78, 44, 73, 42, 13, 62, 24, 47,
33, 91, 25, 95, 83, 23, 29, 15, 49, 32, 19, 35, 86, 77, 69, 22, 20,
4, 41, 45, 92, 27, 58, 55, 38, 76, 66, 34, 12, 98, 96, 8, 61, 9,
88, 53, 90, 67, 60, 81, 93, 16, 87, 59, 17, 63, 75, 70, 54,
5050
----------------------------------------------------------------------------------------------------------------------------------------------------
Answer for (3) :-
==========
inFile.open ("datain.txt", ios::in ); //opening input file
outFile.open("data_out.txt", ios::out); // opening output file
Answer for (4):-
==========
(a) There is no error message displayed
(b) Program does not display the array and no output on the
screen
(c) Nothing written to the output file
90 SET OF INTEGER VALUES READ FROM data_input.txt FILE : 54 70 75
63 17 59 87 9 61 8 96 98 12 34 41 4 20 22 69 77 86 95 25 91 33 47
24 62 10 48 71 18 39 97 64 80 28 1 56 85 100 65 99 52 84 Process
returned o (Oxo) execution time : 0.247 s 16 66 35 13 79 82 6 93 76
19 42 51 40 11 81 38 32 73 74 26 50 60 55 49 44 31 21 67 58 15 78
37 68 27 29 72 57 43 53 92 23 7 88 45 83 5 94 3 30 14 46 36 89
2
Hope it helps... please give an upvote. it's very important to me... thank you:)
Get Answers For Free
Most questions answered within 1 hours.