how to computate gradient of given points?

hi, i'm the newcomer here.. i got a bit problem with this program..

#include <iostream>
#define N 3
using namespace std;

typedef struct
{
double x,y;
}POINT;

typedef struct
{
POINT b,e;
double gradient;
}LINE;

class cg
{
public:
cg(){}
void Calculate(POINT *q);
};
void cg::Calculate(POINT *q)
{
LINE Ln[N+1];
Ln[1].b=q[1];
Ln[1].e=q[2];
Ln[2].b=q[2];
Ln[2].e=q[3];
Ln[3].b=q[3];
Ln[3].e=q[1];

double gradient;
int pt[i];


for(int i=1;i<=2;i++)
{
gradient=((pt[i+1].y=4*i-7)-(pt[i].y=4*i-7))/((pt[i+1].x=i*i+3)-(pt[i].x=i*i+3));
cout<<"The gradient of this 2 point is "<<gradient<<endl;
}
}


int main()
{
cg w;
POINT pt[N+1];
for(int i=1;i<=3;i++)
{
pt[i].x=i*i=3;pt[i].y=4*i-7;
}
w.Calculate(pt);
return 0;
}

is it complete enough to computate the gradient?
thanks.
This looks enough to cause confusion.
There's a lot going on in this line:
gradient=((pt[i+1].y=4*i-7)-(pt[i].y=4*i-7))/((pt[i+1].x=i*i+3)-(pt[i].x=i*i+3));

I would recommend simplifying this code by breaking that up into multiple lines, it has no less than five separate assignments in there. But ultimately I think it calculates gradient = 0 / 0; which is clearly wrong.

This line is also too complex: pt[i].x=i*i=3;pt[i].y=4*i-7;
Adding a bit of spacing we see this:
1
2
    pt[i].x = i*i = 3;
    pt[i].y = 4*i-7;

But this expression isn't valid: i*i = 3

Again, break up the code into multiple lines, keep things simple.
Last edited on
ok,thanks:)
Topic archived. No new replies allowed.