Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program based on 8_lb that reads in the integer data from \"input.txt\"

ID: 3922269 • Letter: W

Question

Write a program based on 8_lb that reads in the integer data from "input.txt" into a vector. Please prompt for the file name and append the "txt". Then create another vector where each element is the original vector times 10. Create a variable total and sum up the numbers from the second vector. The total should be 17510. Then write the data from the original vector and the 10 times vector to a file. See output.txt for the format. You will need to use set precision At the end of the file, write the total.

Explanation / Answer

#include<iostream>
#include<fstream>
#include<vector>

int main()
{
char filename[20];
vector<int> vec;
vector<int> vec2;
int total=0;
  
cout << "Enter file name with extension ";
cin >> filename;
std::ifstream myfile(filename);
std::ofstream outfile("output.txt");
  
int a,i;
do
{
myfile >> a;   
vec.push_back(a);
} while(!myfile.eof());
  
cout << "Writing original vector to file ";
outfile << "Original Vector ";
  
for(i = 0; i < vec.size(); i++){
   cout <<i;
cout << "value of vec [" << i << "] = " << vec[i] << endl;
outfile << vec[i]<<" ";
}
for( i = 0; i < vec.size(); i++){
vec2.push_back(vec[i]*10);
}
cout << "Writing 10x vector to file ";
outfile << "10x Vector ";
  
for( i = 0; i < vec2.size(); i++){
cout << "value of vec [" << i << "] = " << vec2[i] << endl;
total=total+vec2[i];
outfile << vec2[i]<<" ";
}
outfile <<" Total of 10x vector is "<<total<<" ";
      

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote