Outputing table to Data file in C++

To say I'm a beginner is being generous. I'm working on a project for a beginning C++ Programming for Engineers class. I almost have the complete code accept for one thing. I need the output on the screen to print to a data file called "data" at the same time.

All of my searches so far have led me to code for entering data into the table using scanf and/or fscanf. I don't need to input any data, I just need to take the table I already have and output it to a data file.

I don't know if I'm stating this correctly, but I will include my code so far and hopefully somebody can give me some helpful advice. I would like to include the output from running the program, but I CAN'T PRINT IT TO A FILE that I can copy and paste from!!! Sorry. I'm just a little frustrated.

Thanks!

#include <stdio.h>
#include <math.h>

#define M 10

double asolve(int n, double xi[], double yi[]);
double bsolve(int n, double xi[], double yi[]);

int main()
{
double xi[M] = {274, 293, 313, 333, 353, 373, 393};
double yi[M] = {2.5e-3, 5.5e-4, 1.0e-4, 5.0e-5, 2.0e-5, 1.2e-5, 6.0e-6};
double x2, xy, yHaty, yHat = 0, stdev, skew, skewcoef, s=0, k=0;
int i, n = 7, *nPtr;
nPtr = &n;

printf("%10s%12s%12s%10s%10s%10s\n", "x", "y", "x^2", "xy", "yHat", "yHat-y");
printf("----------------------------------------------------------------\n");
for ( i = 0; i < *nPtr; i++)
{
x2 = xi[i]*xi[i];
xy = xi[i]*yi[i];
yHat = (bsolve(n, xi, yi) * xi[i]) + asolve(n, xi, yi);
yHaty = yHat-yi[i];
s += yHaty*yHaty;
k += yHaty*yHaty*yHaty;

printf("%10.1f%12.6f%12.1f%10.4f%10.4f%10.4f\n", xi[i], yi[i], x2, xy, yHat, yHaty);
}
printf("----------------------------------------------------------------\n\n");

printf("(a) is equal to %f\n(b) is equal to %f\n\n", asolve(n, xi, yi), bsolve(n, xi, yi));
printf("Best fit equation => y=%f + %fx\n\n", asolve(n, xi, yi), bsolve(n, xi, yi));

stdev=pow(s/(n-1), 0.5);
skew = k/(n-1);
skewcoef = skew/(pow(stdev, 3));

printf("Standard Dev = %.6f\nSkewness = %.12f\nSkewness Coeff = %.4f\n\n", stdev, skew, skewcoef);
}

double asolve(int n, double xi[], double yi[])
{
int i;
double sumy = 0, sumx2 = 0, sumx = 0, sumxy = 0;
double a;

for(i=0; i<n; i++)
{
sumy += yi[i];
sumx2 += xi[i]*xi[i];
sumx += xi[i];
sumxy += xi[i]*yi[i];

a = (sumy*sumx2 - sumx*sumxy) / (n*sumx2 - pow(sumx, 2));
}

return a;

}

double bsolve(int n, double xi[], double yi[])
{
int i;
double sumy = 0, sumx2 = 0, sumx = 0, sumxy = 0;
double b;

for (i=0; i<n; i++)
{
sumy += yi[i];
sumx2 += xi[i]*xi[i];
sumx += xi[i];
sumxy += xi[i]*yi[i];
b =(n*sumxy - sumx*sumy)/(n*sumx2 - pow(sumx, 2));
}

return b;

}

closed account (DEUX92yv)
In general, writing to a file takes on this form:
1
2
3
4
/* You'll need to*/ #include <fstream>

ofstream filestreamObject ( "myfile.txt" ); // filestreamObject can be ANY name you choose
filestreamObject >> // Insert ints to write, newlines, spaces, etc here 

For example, I want to write a file called "numbers.txt" that looks like this:
7 6 8
9
8 0

The code would be:
1
2
3
4
#include <fstream>

ofstream myfile ("numbers.txt");
myfile << 7 << " " << 6 << " " << 8 << endl << 9 << endl << 8 << " " << 0;

Hope that helps.
Topic archived. No new replies allowed.