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

I have to write a program using files that creates an input and output file. I h

ID: 3625960 • Letter: I

Question

I have to write a program using files that creates an input and output file. I have a program below written using pointers, but unfortunately I can't use pointers since I havent learned about them yet. I was wondering if someone can help me rewrite this program using the while loop instead with no pointers!. Here is my code using pointers and more far below are the sample runs :

#include <fstream>
#include <iostream>
#include<string>
#include <iomanip>
using namespace std;


int main()
{

ifstream inFile;//this reads the input file
ofstream outFile;//this writes to the output file
inFile.open("input.txt");//helps to open the input.txt file to read data from it
outFile.open("output.txt");//creates output.txt file to write data into it
//if file is not found, it terminates program
if(inFile.fail())
{
cout<<"Can't find the input file ";
return 1;
}
//declaring variables
int length;
string n1;
int *data;//pointer to allocate data at runtime for the numbers in the text file
//gets the count numbers from file
inFile>> length>> n1;
data= new int (length);

for(int i=0; i<length; i++)

{
inFile>>*data;
outFile<<*data<<" ";
cout<<*data <<" ";
data++;
}
outFile<<endl;
cout<<endl;
data--;
int min=*data;

for(int i=1; i<length; i++)

{
data--;
if(min>*data)
min=*data;
}

outFile<<"Min:"<<min<<endl;
int max=*data;
for(int i=1; i<length; i++)

{
data++;
if(max<*data)
max=*data;
}
//outputs the maximum value from the input txt file
outFile<<"Max:"<<max<<endl;
int sum=0;

for(int i=0; i<length; i++)

sum+=*data--;
//outputs the total value from the
outFile<<"Total:"<< sum<<endl;
outFile<<"Average:"<<setprecision(1)<<fixed<<((float)sum)/length<<endl;
system("pause");
//closes the files
inFile.close();
outFile.close();

return 0;

}


Here is how the sample runs are suppose to be:

******Sample Run # 1******
INPUT FILE
6:
1 2 3 4 5 6

OUTPUT FILE
1 2 3 4 5 6
Minimum:1
Maximum:6
Total:21
Average:3.5

******Sample Run # 2******
INPUT FILE
6:
0 1000 1 0 -999 1


OUTPUT FILE
0 1000 1 0 -999 1
Minimum:-999
Maximum:1000
Total:3
Average:0.5

******Sample Run # 3******
INPUT FILE
6:
-2 1 -3 -1 2 3

OUTPUT FILE
-2 1 -3 -1 2 3
Minimum:-3
Maximum:3
Total:0
Average:0.0

******Sample Run #4******
INPUT FILE
3:
4 5 8

OUTPUT FILE
4 5 8
Minimum:4
Maximum:8
Total:17
Average:5.7

******Sample Run #5*****
INPUT FILE
3:
-2 -1 -3

OUTPUT FILE
-2 -1 -3
Minimum:-3
Maximum:-1
Total:-6
Average:-2.0

Thanks for the help.

Explanation / Answer

please rate - thanks

#include <fstream>
#include <iostream>
#include<string>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;//this reads the input file
ofstream outFile;//this writes to the output file
inFile.open("input.txt");//helps to open the input.txt file to read data from it
outFile.open("output.txt");//creates output.txt file to write data into it
//if file is not found, it terminates program
if(inFile.fail())
{
cout<<"Can't find the input file ";
return 1;
}
//declaring variables
int length;
int data,sum,min,max;
string n1;
inFile>> length>> n1;
inFile>>data;
sum=data;
min=data;
max=data;
for(int i=1; i<length; i++)
{
inFile>>data;
outFile<<data<<" ";
cout<<data<<" ";
if(min>data)
   min=data;
if(max<data)
     max=data;
sum+=data;
}
outFile<<endl;
cout<<endl;
outFile<<"Min:"<<min<<endl;
//outputs the maximum value from the input txt file
outFile<<"Max:"<<max<<endl;
outFile<<"Total:"<< sum<<endl;
outFile<<"Average:"<<setprecision(1)<<fixed<<((float)sum)/length<<endl;
system("pause");
//closes the files
inFile.close();
outFile.close();

return 0;

}