I created a program which outputs a y coordinate based on a value of an x.
Here is my code.

double xmax,xmin,xspac; 
      cout << " Input a value of minimum value for x: " << endl;
      cin >> xmin;
      cout << " Input a value of maximum value for x: " << endl; 
      cin >> xmax;
      cout << " Input a value of interval spacing: " << endl;
      cin >> xspac;

 if(xmin < myvector[0].x || xmax > myvector[n_points-1].x)
{
          cout << " Error, outside the bounds of the given points " << endl;
}
 else{

      for (xmin; xmin <= xmax; xmin = xmin+xspac){
     if (xmin >= myvector[0].x && xmin <= myvector[1].x)
     {  
              int ctr = 0;
               cout << "[" << xmin << "," << myvector[ctr].y + (myvector[ctr].b*(xmin-myvector[ctr].x)) + (myvector[ctr].c * pow(xmin-myvector[ctr].x,2)) + (myvector[ctr].d*pow(xmin-myvector[ctr].x,3)) << "]" << endl;}
      if (xmin > myvector[1].x && xmin <= myvector[2].x)
      {int ctr = 1;
      cout << "[" << xmin << "," << myvector[ctr].y + (myvector[ctr].b*(xmin-myvector[ctr].x)) + (myvector[ctr].c * pow(xmin-myvector[ctr].x,2)) + (myvector[ctr].d*pow(xmin-myvector[ctr].x,3)) << "]" << endl;}
if (xmin > myvector[2].x && xmin <= myvector[3].x)
{int ctr = 2;
      cout << "[" << xmin << "," << myvector[ctr].y + (myvector[ctr].b*(xmin-myvector[ctr].x)) + (myvector[ctr].c * pow(xmin-myvector[ctr].x,2)) + (myvector[ctr].d*pow(xmin-myvector[ctr].x,3)) << "]" << endl;}
}
}

The function is in the form f(h) = y + b(h-x) + c(h-x)^2 + d(h-x)^3. However the values for x,y,b,c,d are stored in a vector so in the code above i accessed the elements one by one. Is there a way to compress the code so that the "If" statement loops.

You can use loop for ctr variables. Also, you should you a shorter variable name for myvector ( I change it to m ) so as to save some space.

for (int ctr = 0 ; i <= 2; i++)
	{
	    if (xmin >= m[ctr].x && xmin <= m[ctr+1].x)
        {
            cout << "[" << xmin << "," << m[ctr].y + (m[ctr].b*(xmin-m[ctr].x)) + (m[ctr].c * pow(xmin-m[ctr].x,2)) + (m[ctr].d*pow(xmin-m[ctr].x,3)) << "]" << endl;
        }
    }

I've tried listing down more if statements for more intervals. For example i have:

if (xmin > myvector[0].x && xmin <= myvector[1].x)
if (xmin > myvector[1].x && xmin <= myvector[2].x)
if (xmin > myvector[2].x && xmin <= myvector[3].x)
if (xmin > myvector[3].x && xmin <= myvector[4].x)
if (xmin > myvector[4].x && xmin <= myvector[5].x)
if (xmin > myvector[5].x && xmin <= myvector[6].x)

if i have 3 intervals i only need

if (xmin > myvector[0].x && xmin <= myvector[1].x)
if (xmin > myvector[1].x && xmin <= myvector[2].x)
if (xmin > myvector[2].x && xmin <= myvector[3].x)

is there a code which tells the program to disregard the following if statements i.e.

if (xmin > myvector[3].x && xmin <= myvector[4].x)
if (xmin > myvector[4].x && xmin <= myvector[5].x)
if (xmin > myvector[5].x && xmin <= myvector[6].x)

You can divide into two cases, one when ctr <= 2 and one ctr > 2

for (int ctr = 0 ; ctr <= 5; ctr++)
{
if (ctr <=2 ) 
{
   //sth happen
}
else
{
    // sth happen
}
}
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.