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.

Recommended Answers

All 7 Replies

during the input loop t is the index of the elements going into the arrays x and y. However, after the input loop t is the number of inputs read from the file. Therefore after the input loop t is not a useful index for accessing loop elements as there will either be junk in the element with that index or it will be beyond the memory of the arrays and may crash your program.

in Sum() this:

for (int t=0; t<b; ++t)
  S += a[t];

should be something like this:

for(int i = 0; i < t; ++i)
   S += a[i];

thanks i have done changed that althrough the read out remains the same. still has numbers that are stupidly big for the data inputed. looking at the results i dont think the input array is being stopped after the last digit held in the array is read in, how do i do this?

Learning how to debug it yourself would be the best answer. There are three routine ways to do debug something like this. The first is to develop a set of inputs where you know the answers. Then, sprinkle a series of output statements to be sure you get the answers you expect each step of the way. Alternatively you can learn to use a debugging program and follow the values of one (or more) varialbes during the course of the program. Last, and probably best, rather than trying to debug the entire program at once it isn't a bad idea to debug it one step at a time as you are writing the program and not wait until the end to try to debug the entire program at once.

Alternatively, you could post your revised code and someone might be willing to look at it again.

#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];  //more numbers appear inputted than actually are
		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, v, f, xsq ;
	
		Sb=Sum(x,t) ;  // work as long as the constant b is the same as the number of inputs
		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 g=0; g<t; ++g){
	
		S += a[g];
}
	return S;
}

one of the problems i can see is somewhere more inputs are being read than there are actually in the file, can someone suggest a way to fix this problem.

Try this, using xy_data_1.txt as input file. If result of t at end of snippet not equal to 11, then there is a problem. I'm at work without a compiler so you will have to run the code and report back.

double tempX;
double tempY;
while (in >> tempX >> tempY) //read in one line of data until file is completely read
{
   if (t < b) //if not overwriting the buffers
   {		
       x[t] = tempX;
       y[t] = tempY;
       xx[t] = pow(x[t], 2);
       xy[t] = x[t] * y[t];
       ++t;
    }
    else
       break;
}
if(in.eof())
{
  cout << "entire file has been read" << endl;
  in.clear();
}
in.close();
cout << t << " lines of data from file have been read into program" << endl;
cout << "enter any key to continue" << endl;
cin.get();

The reason for the above recommendation is that after reading the last line of the file the ifstream is still in a valid state as it hasn't read EOF yet. Therefore since you are checking for a valid state of the stream to enter the loop the body of the loop will process one too many times. If you attempt input in the conditional and it fails, then the body of the loop won't be processed and you should end up with the correct number of inputs.

thankyou very much and it has solved the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.