the program that im having problems reads in a txt file sums both arrays of data, puts the sums into some equations and out puts the results in a txt file.
#include <iostream> // include all headers needed here
#include <cmath>
#include <cstring>
#include <cctype>
#include <fstream>
#include <cstdlib>
using namespace std;
double Sum (double a[], int t); // prototype function declaration(s)
const int b= 1000;
const int w=100;
int main(){
double x[b];
double y[b];
double xx[b];
double xy[b];
char c[w];
cout<<"Please enter the input filename:"<<endl; // read the input and output filenames
cin>>c;
char h[w];
cout<<"Please enter the output filename:"<<endl;
cin>>h;
ifstream in(c);
if (!in){
cerr<<"Failed to open input file"<<c<<endl;
exit(1);
}
int t=0; // declare arrays, open input file and read numbers into arrays
while (in){
if (t< b){
in>> x[t];
in>>y[t];
xx[t]=pow(x[t],2);
xy[t]=(x[t])*(y[t]);
++t;
}
else break;
}
in.clear(); // close the input file and open the output file
in.close();
ofstream out(h);
if (!out){
cerr<<"Failed to open output file"<<h<<endl;
exit(1);
}
double Sa, Sb, Sc, Sd, D, m, dm, l, dc, g, v, f, xsq ;
Sb=Sum(x,t) ; // work as long as the constant b is the same as the number of inputs data
Sa=Sum(xx,t) ;
Sc=Sum(y,t);
Sd=Sum(xy,t);
D = (( t * Sa) -( pow(Sb,2)));
m = ((t * Sd) - (Sb*Sc)) / D;
dm = sqrt( t/D ) ;
l = ((Sc*Sa) - (Sb * Sd)) / D;
dc = sqrt( Sa /D ) ;
v=(m*x[t])+(l-y[t]); // not working (residual) on output
f= (m * x[t])+l; // reconising the l but not reconising m*x[t] (mx+c) on output file
xsq = ((m*(Sum(x,t)))-(Sum(y,t))+l);
out<<"For y = mx + c, with errors on y of sigma, the line of best fit has"<<endl;
out<<"\n m ="<<m<<"+/-("<<dm<<"*sigma)"<<endl;
out<<"c ="<<l<<"+/-("<<dc<<"*sigma)"<<endl;
out<<"\n The data and the fitted points are:"<<endl;// compute fit parameters and errors
out<<"\n x\t\ty\t\tmx+c\t\tresidual"<<endl;
for (int i=0; i<t-1; ++i){
out<<"\n"<<x[i]<<"\t\t"<<y[i]<<"\t\t"<<(m * x[t])+l<<"\t\t"<<v<<endl;
}
out<<"Chi-squared ="<<xsq<<"/sigma^2"<<endl;
out.close(); // write to output file and close it
}
double Sum (double a[], int t){
double S=0;
for (int t=0; t<b; ++t){
S += a[t];
}
return S;
}
at the minute it is reading in and out fine. its what i want it to do with the arrays that im inputting that it is not doing. Now i think there is something wrong with the summing fuction and i have also put notes as to where i also think it is going wrong but im not sure.
the input txt file is attached as well.