Hi. i am taking a C++ intro course and one of my projects is to create a program that will give me the beta 0 and beta 1 of a regression. The user must input a the following: how many data sets?, each of the Xs and each of the Ys. the following is what I have gotten so far with a little help from people in my class. we are all having major problems with this project. here is wat i have so far.

int i;
void getb1(double X[], double Y[], int size, double &b1)
{
double sumY = 0.0;
double sumX = 0.0;
double sumXY = 0.0;
double b1;                                            //this is where the third error is
double sqrX = 0.0;
    for (i=0; i<size; i++)
    {

        sumXY = sumXY + X[i]*Y[i];

        sumX = X + X[i];                                   //this is where the fourth error is
        sumY = Y + Y[i];                                    //this is where the fifth error is

        sqrX = sqrX + X[i]*X[i];
    }

b1 = (size*sumXY-sumX*sumY)/(size*sqrX-sumX*sumX);

}

void getb0(double X[], double Y[], double b1, int size, double &b0)
{
double sumX = 0.0;
double Xbar = 0.0;
double Ybar = 0.0;
double sumY = 0.0;

    for (i = 0; i < size; i++)
    {
        sumX = sumX + X[i];
        sumY = sumY + Y[i];
    }
Xbar = sumX/size;
Ybar = sumY/size;

}


#include <iostream>
#include <string>

using namespace std;
void getb1 (double X[], double Y[], int size, double &b1);
void getb0 (double X[], double Y[], int size, double &bo);

int main ()
{
    int size;
    double X[] = {0};
    double Y[] = {0};
    double b1;
    double b0;
    cout << "Please give me the size of your variables." << endl;
    cin >> size;
    cout << "Now start to input your x variables" << endl;
    for (i = 0; i < size; i++){
        cin >> X[i];
    }
    cout << "Now start to input your Y variables." << endl;
    for (i = 0; i < size; i++){
        cin >> Y[i];
    }
    getb1 (X[], Y[], size, b1);                             //this is where the first error is
    getb0 (X[], Y[], size, b1, b0);                       //this is where the second error is

    cout << "Now you have the model." << endl;
    cout << "Y = " << b1 << "X" << "+" << b0 << endl;

    return 0;

}

these are the errors i get when i build the program as it is (th errors are listed in the program above):

error C2059: syntax error : ']'
error C2059: syntax error : ']'
error C2082: redefinition of formal parameter 'b1'
error C2111: '+' : pointer addition requires integral operand
error C2111: '+' : pointer addition requires integral operand

i have a feeling that there are major problems in this program other than the ones listed, but i just cant figure it out.

also please note that i am quite new to all this so i don't understand all the terminology. I just wrote this program according to the my class slides, which are not very helpful.
thanks in advance for all the help.

Recommended Answers

All 9 Replies

Welcome to Daniweb! When posting code to this board please enclose int in code tags. You can read about code takes by reading the watermarks that appear in the message box where you enter the information that you want to appear in the post or by reading the anouncement section at the top of the board.

1) When passing arrays, just pass the name of the array:

getb1 (X[], Y[], size, b1); //this is where the first error is

should be:

getb1 (X, Y, size, b1);

2) This:

double X[] = {0};

Is legal. It declares an array of type double with a single value, whick is zero. If you don't know the size of the array at the time of compilation, then you should either declare the array with dynamic rather than static memory, that is use new[] to declare memory for the array (and use delte[] to release the memory used by the array), or let C++ do it for by using a vector object from the STL vector class.

3) Don't declare i as global. It's legal, but it's bad form.

4) If you are going to have the function definitions before main() is defined, then it isn't necessary to use function protocols.

5) It is tradition to list the include files before the globals variables and function protocols/definitions, though whether it's mandatory, I don't know, as I've never tried listing them in any other order.

I have rewritten the program with the fixes you mentioned. But i still get the 2 errors.
these are the errors:

error C2111: '+' : pointer addition requires integral operand
error C2111: '+' : pointer addition requires integral operand

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int i;
void getb1 (double X[], double Y[], int size, double &b1);
void getb0 (double X[], double Y[], int size, double &bo);

int main ()
{
	int size;
	double X[] = {0};
	double Y[] = {0};
	double b1;
	double b0;
	cout << "Please give me the size of your variables." << endl;
	cin >> size;
	cout << "Now start to input your x variables" << endl;
	for (i = 0; i < size; i++){
		cin >> X[i];
	}
	cout << "Now start to input your Y variables." << endl;
	for (i = 0; i < size; i++){
		cin >> Y[i];
	}
	getb1 (X, Y, size, b1);
	getb0 (X, Y, size, b0);

	cout << "Now you have the model." << endl;
	cout << "Y = " << b1 << "X" << "+" << b0 << endl;

	return 0;

}


void getb1(double X[], double Y[], int size, double &b1)
{
double sumY = 0.0;
double sumX = 0.0;
double sumXY = 0.0;
double sqrX = 0.0;
	for (i = 0; i < size; i++)
	{
		sumX = X + X[i];  
		sumY = Y + Y[i];

		sumXY = sumXY + X[i]*Y[i];

		sqrX = sqrX + X[i]*X[i];
	}

b1 = (size*sumXY-sumX*sumY)/(size*sqrX-sumX*sumX);

}

void getb0(double X[], double Y[], double b1, int size, double &b0)
{
double sumX = 0.0;
double Xbar = 0.0;
double Ybar = 0.0;
double sumY = 0.0;


	for (i = 0; i < size; i++)
	{
		sumX = sumX + X[i];
		sumY = sumY + Y[i];
	}
Xbar = sumX/size;
Ybar = sumY/size;
b0 = Ybar - b1*Xbar;
}

change lines that look like this:

sumX = X + X;

to the equivalent of this:

sumX += X;

since X is the name of an array and acts like a pointer so the compiler thinks you want to do pointer addition when it sees the + operator after tha array name, but that requires an int after the + sign, however X is a double, hence the error message.

> sumX = X + X;
X is an array, X is an array member - what are you trying to do here?

> sumX = X + X;
X is an array, X is an array member - what are you trying to do here?

well if you look at the main ()

i ask the user to input all Xs and all Ys using the For loop.

what this equation is suppose to do is sum up all the Xs. So basically, as each X value is entered by the user it is added to the previous one until all the X values are summed up.

Hey Lerner,

first of i would like to thank you for all you help. I greatly appreciate it.

i have made all the fixes that you suggested and now the program builds just fine. but when i start without debugging, i get an error. i ask the user 2 input the size and the input is accepted. in my test i input 5. when the program prompted the user to input the X values, the program gave some weird error after the third X input. Since i cant explain the error i took a pic of it. the following are program with all your suggested corrections and the pic of the error.

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
int i;
void getb1 (double X[], double Y[], int size, double &b1);
void getb0 (double X[], double Y[], double b1, int size, double &bo);

int main ()
{
    int size;
    double X[] = {0};
    double Y[] = {0};
    double b1;
    double b0;
    cout << "Please give me the size of your variables." << endl;
    cin >> size;
    cout << "Now start to input your x variables" << endl;
    for (i = 0; i < size; i++){
        cin >> X[i];
    }
    cout << "Now start to input your Y variables." << endl;
    for (i = 0; i < size; i++){
        cin >> Y[i];
    }
    getb1 (X, Y, size, b1);
    getb0 (X, Y, size, b1, b0);

    cout << "Now you have the model." << endl;
    cout << "Y = " << b1 << "X" << "+" << b0 << endl;

    return 0;

}


void getb1(double X[], double Y[], int size, double &b1)
{
double sumY = 0.0;
double sumX = 0.0;
double sumXY = 0.0;
double sqrX = 0.0;
    for (i = 0; i < size; i++)
    {
        sumX += X[i];  
        sumY += Y[i];

        sumXY = sumXY + X[i]*Y[i];

        sqrX = sqrX + X[i]*X[i];
    }

b1 = (size*sumXY-sumX*sumY)/(size*sqrX-sumX*sumX);

}

void getb0(double X[], double Y[], double b1, int size, double &b0)
{
double sumX = 0.0;
double Xbar = 0.0;
double Ybar = 0.0;
double sumY = 0.0;


    for (i = 0; i < size; i++)
    {
        sumX = sumX + X[i];
        sumY = sumY + Y[i];
    }
Xbar = sumX/size;
Ybar = sumY/size;
b0 = Ybar - b1*Xbar;
}

here is the like to the image of the error

Go back to post #2 and the discussion under 2). If you don't know the size of the array to begin with you either need to create the array large enough to all possible inputs, keep track of the number of inputs to be sure you don't overwrite the array (and, hopefully, crash the program) like you do now, or use an object, like the STL vector class objects, that automatically declare the memory you to hold the information. Since I don't know which of these techniques you have available to use you have to choose the approach.

ok so i have rewritten the code slightly differently.
it builds just fine and the problem of it crashing when inputing the X values has also been fixed.

Now the only problem i have is that the output result is completely out of wack. it always gives me the same bogus answer.

here is the new code:

#include <iostream>
#include <string>
#include <cmath>

using namespace std;
void getb1 (double X[], double Y[], double size, double &b1);
void getb0 (double X[], double Y[], double b1, double size, double &bo);

int main ()
{
	int size = 0;
	double *X;
	double *Y;
	double b1;
	double b0;
	cout << "Please give me the size of your variables." << endl;
	cin >> size;
	X = new double[size];
	Y = new double[size];
	cout << "Now start to input your x variables" << endl;
	for (int i = 0; i < size; i++){
		cin >> X[i];
	}
	cout << "Now start to input your Y variables." << endl;
	for (int i = 0; i < size; i++){
		cin >> Y[i];
	}
	getb1 (X, Y, (double)size, b1);
	getb0 (X, Y, (double)size, b1, b0);


	cout << "Now you have the model." << endl;
	cout << "Y = " << b1 << "X" << "+" << b0 << endl;

	return 0;

}


void getb1(double X[], double Y[], double size, double &b1)
{
	int i;
	double sumY = 0.0;
	double sumX = 0.0;
	double sumXY = 0.0;
	double sqrX = 0.0;
		for (i = 0; i < size; i++)
		{
			sumX += X[i];  
			sumY += Y[i];

			sumXY = sumXY + X[i]*Y[i];

			sqrX = sqrX + X[i]*X[i];
		}

	b1 = ((double)size*sumXY-sumX*sumY)/((double)size*sqrX-sumX*sumX);

}

void getb0(double X[], double Y[], double b1, double size, double &b0)
{
	int i;
	double sumX = 0.0;
	double Xbar = 0.0;
	double Ybar = 0.0;
	double sumY = 0.0;

		for (i = 0; i < size; i++)
		{
			sumX = sumX + X[i];
			sumY = sumY + Y[i];
		}
	Xbar = sumX/(double)size;
	Ybar = sumY/(double)size;
	b0 = Ybar - b1*Xbar;
}

Leave size an int wherever it is used.

For getb0(), the declaration and definition have size being passed as a parameter between b1 and b0 whereas in the function call size is being passed before b1 and b0. That means size and b1 will be interchanged in the calculations within getbo(). Don't know if that will correct the results or if there is another problem, too.

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.