i need to write program in c++This assignment has four parts. Part 2 will use Pa
ID: 646971 • Letter: I
Question
i need to write program in c++This assignment has four parts. Part 2 will use Part 1 results and add new features. Part 3 will use Part 2 results and add more features. Part 4 will be the last section. All parts must be in the main source code file.
Prompt for an input filename to be entered from the keyboard. Procedures: Part 1 Write a program that reads ten elements from a data file into a one-dimensional array, then close the
input data file. No further actions will use the input file. Write the contents of the array to the screen. Output to the screen must be from the array, not while data is being read in from the data file. Then write the contents of the array to a data file. Use ofstream outfile1 as the object, and
Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
int main(){
char inputFileName[100];
cout << "Enter the input filename: ";
cin >> inputFileName;
ifstream infile1(inputFileName);
int part1[10];
// reading the input file into the array
for(int i = 0; i < 10; i++){
infile1 >> part1[i];
}
infile1.close();
// printing the array
for(int i = 0; i < 10; i++){
cout << part1[i] << " ";
}
cout << endl;
ofstream outfile1("arrayout.dat");
// Contents of the array read into file
for(int i = 0; i < 10; i++){
outfile1 << part1[i] << " ";
}
outfile1.close();
ifstream infile2("arrayout.dat");
int temp;
// print the contents of the output file
for(int i = 0; i < 10; i++){
infile2 >> temp;
cout << temp << " ";
}
infile2.close();
cout << endl;
int a[5], b[5];
for(int i = 0; i < 5; i++) a[i] = part1[i];
for(int i = 0; i < 5; i++) b[i] = part1[i + 5];
ofstream outfile2("sub1out.dat");
ofstream outfile3("sub2out.dat");
for(int i = 0; i < 5; i++) outfile2 << a[i] << " ";
outfile2.close();
for(int i = 0; i < 5; i++) outfile3 << b[i] << " ";
outfile3.close();
int c[5];
for(int i = 0; i < 5; i++) c[i] = a[i] + b[i];
ofstream outfile4("addarray.dat");
for(int i = 0; i < 5; i++) outfile4 << c[i] << " ";
outfile4.close();
int d[5];
for(int i = 0; i < 5; i++) d[i] = a[i] * b[i];
ofstream outfile5("multarray.dat");
for(int i = 0; i < 5; i++) outfile5 << d[i] << " ";
outfile5.close();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.