0
votes

I get an error/warning when trying to get the minimum/maximum values from the points in the main function. How can I calculate the max/min points? Is there any easier method? Am i supposed to use a structure?

        
test.c:73:14: warning: incompatible pointer types passing 'int [100][100]' to
      parameter of type 'int *' [-Wincompatible-pointer-types]
      convexHull(points, n);

        test.c:33:21: note: passing argument to parameter 'point' here
        void convexHull(int point[], int n)
                    ^
        1 warning generated.**
void convexHull(int point[], int n)
{
  int i;
  //n is the size
  int min_x=0;
  int max_x=0;

  if (n <3)
  {
    printf("Convex hull can't have less than 3 points\n.");
  }

  //Finding point with min/max x-coordinate.
  for (i=1;i<n;i++)
  {
    if (point[i] < point[min_x])
    {
      min_x=i;
    }
    if (point[i] > point[max_x])
    {
      max_x = i;
    }
  }
  printf("%d\n",max_x);
  printf("%d\n",min_x);
}

int main()
{
  int n;
  int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
                  {3, 0}, {0, 0}, {3, 3}};

  n = sizeof(points)/sizeof(points[0]);
  convexHull(points, n);


  return 0;
}
1
You can't pass a 2-D array to a function that expects a 1-D array.stark
I fixed the issue. I did convexHull(int point[][100],int n) and that error seemed to go away. Now i am noticing when it prints the max value, it literally goes all the way until 100 as that is what i want it to do...but i want it to print the max value of x.IBSurviver
That can't work. n will be 10000, so way out of bounds.stark

1 Answers

0
votes

The array points is declared as a two-dimensional array

  int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
                  {3, 0}, {0, 0}, {3, 3}};

So used in an expression as a function argument it is implicitly converted to pointer to its first element of the type int ( * )[100].

However the corresponding function parameter has the type int *

void convexHull(int point[], int n)

because the parameter declared like int point[] is adjusted by the compiler to the declaration int * point.

And there is no implicit conversion from the type int ( * )[100] to the type int *.

Moreover it seems this declaration

  int points[100][100] = {{0, 3}, {2, 2}, {1, 1}, {2, 1},
                  {3, 0}, {0, 0}, {3, 3}};

does not make sense because each "point" has only twp elements as for example {0, 3} but not 100 elements.

What you need is to declare a structure for example like

struct Point
{
    int x;
    int y;
};

and use it in your program.

In this case the array of points can be defined the following way

  struct Point points[] = { {0, 3}, {2, 2}, {1, 1}, {2, 1},
                  {3, 0}, {0, 0}, {3, 3} };

So the function declaration and definition should be changed accordingly.