Question: Please help me fix my code The problem is that the output file shows up blank In code output and input: Enter input file datain.txt
Please help me fix my code
The problem is that the output file shows up blank
In code output and input:
Enter input file
datain.txt
(displays datain data)
Enter output file
dataout.txt
Then that file is blank, here's the code
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main ()
{
// Declare variables, streams, arrays and strings
ifstream inputfile; //input file
ofstream outputfile; //output file
double navg; //noisy avg
double savg; //smooth avg
double ndev; //noisy deviation
double sdev; //smooth deviation
double noisy[100];
double smooth[100];
int index(0); //declaring the index for the array
// Get the name of the input file
{ //bracket allows the use of local variables
string filename; // filename is the local variable
cout << "Enter the name of the input file." << endl;
cin >> filename;
// opening and checking if the input file exists
inputfile.open(filename.c_str());
if (inputfile.fail())
{
do {
char response;
cerr << "Could not open file; " << filename << endl;
cerr << "Would you like to enter a new file? (y or n)" << endl;
cin >> response;
if (tolower(response) != 'y')
exit(1); //exits code if user does not enter another name
else
{
cout << "Enter the name of the input file." << endl;
cin >> filename;
inputfile.clear(); //clears fail status of inputfile
inputfile.open(filename.c_str());
}
} while (inputfile.fail());
}
}
// read data from input file into an array
while (!inputfile.eof())
{
inputfile >> noisy[index];
index++; //index is after inputfile statement so index is equal to number of terms in array
}
// calculate the standard deviation and average of the original data
{ // bracket allows use of local variables
double sum(0); // sum is the local variable
for (int i = 0; i < index; i++)
{
sum += noisy[i];
}
navg = sum / index;
}
{ //allows for use of local variables
double var(0); // var and var_sum are the local variables
double var_sum(0);
for (int i = 0; i < index; i++)
{
var_sum += pow((noisy[i] - navg), 2);
}
var = var_sum / (index - 1);
ndev = sqrt(var);
}
// smooth the original data using a sliding median
for (int i = 1; i < index; i++)
{
if (noisy[i] < noisy[i - 1] && noisy[i] > noisy[i + 1]
|| noisy[i] > noisy[i - 1] && noisy[i] < noisy[i + 1])
smooth[i] = noisy[i]; //for case that noisy[i] is median value
else if (noisy[i - 1] < noisy[i] && noisy[i - 1] > noisy[i + 1]
|| noisy[i - 1] > noisy[i] && noisy[i - 1] < noisy[i + 1])
smooth[i] = noisy[i - 1]; //for case noisy[i - 1] is median value
else if (noisy[i + 1] < noisy[i] && noisy[i + 1] > noisy[i - 1]
|| noisy[i + 1] > noisy[i] && noisy[i + 1] < noisy[i - 1])
smooth[i] = noisy[i + 1]; //for case noisy[i + 1] is median value
}
// give smoothed array end terms values equal to the noisy array end terms
smooth[0] = noisy[0];
smooth[index - 1] = noisy[index - 1];
// calculate the average and standard deviation of the smoothed data
{ //brackets allow the use of local variables
double sum(0); //sum is the local variable
for (int i = 0; i < index; i++)
{
sum += smooth[i];
}
savg = sum / index;
}
{ //brackets used to allow use of local variables
double var(0); //var and var_sum are local variables
double var_sum(0);
for (int i = 0; i < index; i++)
{
var_sum += pow((smooth[i] - savg), 2);
}
var = var_sum / (index - 1);
sdev = sqrt(var);
}
// print the original and smoothed data
for (int i = 0; i < index; i++)
cout << fixed << setprecision(2) << noisy[i] << " ";
cout << endl;
for (int i = 0; i < index; i++)
cout << fixed << setprecision(2) << smooth[i] << " ";
cout << endl;
// print original average and standard deviation
cout << "The noisy average is: " << navg << endl;
cout << "The noisy standard deviation is: " << ndev << endl;
// print smoothed average and standard deviation
cout << "The smooth average is: " << savg << endl;
cout << "The smooth standard deviation is: " << sdev << endl;
// Get the name of the output file
{ //brackets to allow for the use of local variables
string filename; // local variable
cout << "Enter the name of the output file." << endl;
cin >> filename;
// Opening and then checking if the output file exists
outputfile.open(filename.c_str());
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
