期望的恒定表达

时间:2014-10-13 00:47:40

标签: c++ arrays algorithm debugging compiler-errors

我正在使用分而治之的算法技术实现最接近的对问题的版本。但是,当我尝试编译我的代码时,在几个位置我得到错误“预期的常量表达式”。我知道数组应该在它们中有常量值,但我不太确定在这种情况下有什么问题。我试图研究解决方案,很多人建议使用malloc,但似乎总是不赞成。有人能帮我解决这个问题吗?下面是错误评论的代码,希望你可以看到它们。非常感谢您的帮助,我真的很感激!

#include <iostream>
#include <float.h>
#include <stdlib.h>
#include <math.h>
using namespace std;

//A struct to represent the points on an x,y plane
struct Point{
    int x, y;
};

//function to sort x coordinates
int compareX(const void* a, const void* b){
    Point *p1 = (Point *)a, *p2 = (Point *)b;
    return (p1->x - p2->x);
}

//function to sort y coordinates
int compareY(const void* a, const void* b){
    Point *p1 = (Point *)a, *p2 = (Point *)b;
    return (p1->y - p2->y);
}

//function to find the distance between any two points
float dist(Point p1, Point p2){
    return sqrt( (float)(p1.x - p2.x) * (p1.x - p2.x) + 
                 (float)(p1.y - p2.y) * (p1.y - p2.y) 
                );
}

//utility function to find the minimum of any two float values
float min(float x, float y){
    if(x < y)
        return x;
    else
        return y;
}

//brute force function to find the closest of two points
float bruteforce(Point P[], int n){
     float min = FLT_MAX;

     for(int i=0; i<n; i++){
          for(int j = i+1; j < n; j++)
               if(dist(P[i], P[j]) < min)
                    min = dist(P[i], P[j]);
     }
     return min;
}

//function to find the distance between the closest points of a given size
float closestArray(Point array1[], int size, float d){
     float min = d; //initialize the minimum distance as d

     //go through the points 1 by 1 and try the next until the difference is smaller than d
     for (int i=0; i < size; i++)
          for (int j= i + 1; j< size && (array1[i].y - array1[i].y) < min; j++)
               if(dist(array1[i],array1[j]) < min)
                    min = dist(array1[i], array1[j]);

     return min;
}


float closestPoint(Point Px[], Point Py[], int n){

     if(n <= 3)
          return bruteforce(Px, n);

    //find the middle point
    int mid = n/2;
    Point midPoint = Px[mid];

    //divide the points along the vertical line
    Point Pyleft[mid + 1]; //left of vertical line <--- ERROR
    Point Pyright[n-mid-1]; //right of vertical line <--- ERROR
    int li = 0; //index of left subarray
    int ri = 0; //index of right subarray
    for ( int i =0; i<n; i++){
        if (Py[i].x <= midPoint.x)
            Pyleft[li++] = Py[i];
        else
            Pyright[ri++] = Py[i];
    }

    //calculate the smallest ditance dl on the left middle point and dr on the right side
    float dl = closestPoint(Px, Pyleft, mid);
    float dr = closestPoint(Px + mid, Pyright, n-mid);

    //find the smaller of the two distances
    float d = min(dl, dr);

     //build another array Q that contains points closer than d to the line passing through the middle
     Point q[n]; // <--- ERROR
     int j = 0;
     for (int i = 0; i<n; i++)
          if(abs(Py[i].x - midPoint.x) < d)
               q[j] = Py[i], j++;

     return min(d, closestArray(q, j, d) );
}

//function that finds the smallerst distance
float closest(Point P[], int n){
     Point Px[n]; //<--- ERROR
     Point Py[n]; //<---ERROR
     for(int i=0; i < n; i++)
     {
          Px[i] = P[i];
          Py[i] = P[i];
     }

     qsort(Px, n, sizeof(Point), compareX);
     qsort(Py, n, sizeof(Point), compareY);

     //recursive function to find smallest distance
     return closestPoint(Px, Py, n);
}

int main()
{

    Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}};
    int n = sizeof(P) / sizeof(P[0]);
    cout << "The smallest distance is " << closest(P, n);
    return 0;
}

#include <iostream> #include <float.h> #include <stdlib.h> #include <math.h> using namespace std; //A struct to represent the points on an x,y plane struct Point{ int x, y; }; //function to sort x coordinates int compareX(const void* a, const void* b){ Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->x - p2->x); } //function to sort y coordinates int compareY(const void* a, const void* b){ Point *p1 = (Point *)a, *p2 = (Point *)b; return (p1->y - p2->y); } //function to find the distance between any two points float dist(Point p1, Point p2){ return sqrt( (float)(p1.x - p2.x) * (p1.x - p2.x) + (float)(p1.y - p2.y) * (p1.y - p2.y) ); } //utility function to find the minimum of any two float values float min(float x, float y){ if(x < y) return x; else return y; } //brute force function to find the closest of two points float bruteforce(Point P[], int n){ float min = FLT_MAX; for(int i=0; i<n; i++){ for(int j = i+1; j < n; j++) if(dist(P[i], P[j]) < min) min = dist(P[i], P[j]); } return min; } //function to find the distance between the closest points of a given size float closestArray(Point array1[], int size, float d){ float min = d; //initialize the minimum distance as d //go through the points 1 by 1 and try the next until the difference is smaller than d for (int i=0; i < size; i++) for (int j= i + 1; j< size && (array1[i].y - array1[i].y) < min; j++) if(dist(array1[i],array1[j]) < min) min = dist(array1[i], array1[j]); return min; } float closestPoint(Point Px[], Point Py[], int n){ if(n <= 3) return bruteforce(Px, n); //find the middle point int mid = n/2; Point midPoint = Px[mid]; //divide the points along the vertical line Point Pyleft[mid + 1]; //left of vertical line <--- ERROR Point Pyright[n-mid-1]; //right of vertical line <--- ERROR int li = 0; //index of left subarray int ri = 0; //index of right subarray for ( int i =0; i<n; i++){ if (Py[i].x <= midPoint.x) Pyleft[li++] = Py[i]; else Pyright[ri++] = Py[i]; } //calculate the smallest ditance dl on the left middle point and dr on the right side float dl = closestPoint(Px, Pyleft, mid); float dr = closestPoint(Px + mid, Pyright, n-mid); //find the smaller of the two distances float d = min(dl, dr); //build another array Q that contains points closer than d to the line passing through the middle Point q[n]; // <--- ERROR int j = 0; for (int i = 0; i<n; i++) if(abs(Py[i].x - midPoint.x) < d) q[j] = Py[i], j++; return min(d, closestArray(q, j, d) ); } //function that finds the smallerst distance float closest(Point P[], int n){ Point Px[n]; //<--- ERROR Point Py[n]; //<---ERROR for(int i=0; i < n; i++) { Px[i] = P[i]; Py[i] = P[i]; } qsort(Px, n, sizeof(Point), compareX); qsort(Py, n, sizeof(Point), compareY); //recursive function to find smallest distance return closestPoint(Px, Py, n); } int main() { Point P[] = {{2, 3}, {12, 30}, {40, 50}, {5, 1}, {12, 10}, {3, 4}}; int n = sizeof(P) / sizeof(P[0]); cout << "The smallest distance is " << closest(P, n); return 0; }

0 个答案:

没有答案