-3

Description

This code is intended to find a spanner index (i) when dealing with Splines/NURBS basis functions based on a knot vector (U), a choosen knot (u), the degree of the desired curve (p) and the number of basis funcions (n). The algorithm was taken from the NURBS Book by Piegl and Tiller. The error, I guess, is in the way I declared variable U. Thaks in advanced!

code

    # include <iostream>
    using namespace std;

    int n=3;    
    int p=2;
    double u=5/2;
    int U[11]={0,0,0,1,2,3,4,4,5,5,5};

    int FindSpan(n,p,u,U)     /* an error in this line */
    {
        if (u==U[n+1]) return (n);
        low=p; high=n+1;
        mid=(low+high)/2
        while(u<U[mid] || u>=U[mid+1])
        {
            if (u<U[mid]) high=mid;
            else  low=mid;
            mid=(low+high)/2
        }
        return (mid);
    }
  • 1
    You didn't specify types for the parameters. C++ is a statically typed language. The fact you have variables with the same name above means nothing to the function prototype. – StoryTeller - Unslander Monica Apr 25 '17 at 05:22
  • int FindSpan(int n, int p, double u, int U[]) make function signature like this – Undefined Behaviour Apr 25 '17 at 05:23
  • 4
    Frankly. if you are trying to program in C++ based on your knowledge in some scripting language, just stop. This is the worst way to learn C++. Instead, pick up a [recommended book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and learn in a structured manner. – StoryTeller - Unslander Monica Apr 25 '17 at 05:25
  • 1
    Welcome to StackOverflow. Please take the tour stackoverflow.com/tour, learn asking good questions stackoverflow.com/help/how-to-ask, make a MCVE stackoverflow.com/help/mcve Especially do not make the actual error messages a secret. – Yunnosch Apr 25 '17 at 05:35
  • Looks a lot like Javascript in C++. – Dean Seo Apr 25 '17 at 05:40
  • Note that 5/2 is 2 and not 2.5, since it is an integer division. – Jonas Apr 25 '17 at 06:21

1 Answers1

0

You have forgotten some semicolons and types!

here is the correct code:

#include <iostream>
using namespace std;

int n=3;    
int p=2;
double u=5/2;
int U[11]={0,0,0,1,2,3,4,4,5,5,5};

int FindSpan(int n, int p, int u, int U[])
{
    if (u==U[n+1]) return (n);
    int low=p, high=n+1;
    int mid=(low+high)/2;
    while(u<U[mid] || u>=U[mid+1])
    {
        if (u<U[mid]) high=mid;
        else  low=mid;
        mid=(low+high)/2;
    }
    return (mid);
}
int main() {
    cout << FindSpan(n, p, u, U);
    return 0;
}
Mehdi
  • 47
  • 5