Hello everyone:

As I stated previously I am learning my C++ on the run because is required for one of my classes and well any help I can get is appreciated. I created a code to read a .txt file containing a list of numbers. My problem is that the list of numbers is not being read the way I want to. According to my friend there is an easy solution, but I have not been able to find the answer. Here is the result I get when I run my code

number of data point:  4
x[ ]0x300300
0x300300
0x300300
0x300300
y[ ]0x300320
0x300320
0x300320
0x300320
T= 1 a= 0 b= 0 c= 0 d= 0 e= 0
r = nan

And here is my code

#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
using namespace std;

void sum(double* x, double* y, int T, double* a, double* b, double* c, double* d, double* e);

int main (void)
{
  unsigned long T;
  ifstream in;
  cout << "number of data point:  ";
  cin >> T;
  double* x = new double[T];
  double* y = new double[T];
  cout << "x[ ]";
  in.open("x.txt");
  if (!in)
 	{
	cout << "Unable to open file";
	exit(1); //terminates wih error	   
	}
  while (in >> T)
	{
	cout << x << "\n";
	}
  in.close();
  cout << "y[ ]";
  in.open("y.txt");
  if (!in)
	{
	cout << "Unable to open file";
	exit(1); //terminates wih error
	}
  while (in >> T)
	{
	cout << y << "\n";
	}
	in.close();
  double a = 0.0;
  double b = 0.0;
  double c = 0.0;
  double d = 0.0;
  double e = 0.0;
  sum(x,y,T, &a, &b, &c, &d, &e);
  double r = (T*c-a*b)/sqrt((T*d-a*a)*(T*e-b*b));
  delete[] x;
  delete[] y;
  cout <<"T= "<<T<<" a= "<<a<<" b= "<<b<<" c= "<<c<<" d= "<<d<<" e= "<<e<<"\n";
  cout << "r = " << r << "\n";
  return 0;
}

void sum(double* x, double* y, int T, double* a, double* b, double* c, double* d, double* e)
{
  double sum_x = 0.0;
  double sum_y = 0.0;
  double sum_xy = 0.0;
  double sum_x2 = 0.0;
  double sum_y2 = 0.0;
  for(unsigned long t=0;t<T;t++)
    { sum_x += x[t];
      sum_y += y[t];
      sum_xy += x[t]*y[t];
      sum_x2 += x[t]*x[t];
      sum_y2 += y[t]*y[t];
    }
  *a = sum_x;
  *b = sum_y;
  *c = sum_xy;
  *d = sum_x2;
  *e = sum_y2;
}//end of code

Can you see where the error is located ? I am pretty sure this is a dumb problem to have but any help will be appreciated. Thanks. Oh i attached both x.txt and y.txt file.

G:X

Attachments
1
2
3
4
4
3
2
1

Sorry about that, I am a rookie in to the system and you are right. Apologize.

G

This article has been dead for over six months. Start a new discussion instead.