2

so i have this structure:

// Structure used to define a point (x,y) in the grid.
typedef struct
{
    int x, y;
} Point;

and this function

Sequence getSequence(int grid[][MAXCOLS], Point startPos)
{
    // Create an empty sequence
    Sequence emptySeq;
    emptySeq.size = 0;

    // Use the empty sequence to start the recursive function
    return generateSeq(grid, startPos, emptySeq);
}

i do not know the position where the sequence begins. so i have too call on the getSequence function 16 times in main, so that you can pass each of the 16 grid positions to it as possible starting positions.

i've tried this but it didnt work.

getSequence(grid, x.0, y.0 );

can someone please show me how to call on the getSequence in main. Im new to programing

Thanks

user1896464
  • 345
  • 1
  • 4
  • 11

3 Answers3

4

The most straight forward method would be to create an instance of Point as Arun said like so:

int main()
{
   Point p1 ;
   // create grid here

   p1.x = 0 ;
   p1.y = 0 ;
   getSequence( grid, p1 );
}

If you can use C++11 then you can also do this:

 getSequence( grid, {0,0} );

To be more like C++-like, though you really should use a constructor like so:

class Point
{
     public:
     int x, y;

     Point( int a = 1, int b = 1 ) : x(a) , y(b) {} 
} ;

Also then you should think about making x and y to be private as well but your particular example starts getting more complicated then. If you have a constructor your 16 calls becomes slightly easier since you can do this:

 getSequence( grid,  Point(1,1) );
 getSequence( grid,  Point(2,2) );

Or you could use a loop as well, the values of a and b will depend on your application:

for( int a = 0, b = 0; a < 5; ++a, ++b)
{
   getSequence(grid, Point(a,b) );
}
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • can i just use a loop go through the positions? or do i have to do this 16 times changing the x and y values each time? : ` startPos.x=0; startPos.y=0; getSequence(grid, startPos);` – user1896464 Mar 26 '13 at 02:20
  • @user1896464 added some more details that hopefully provide a good answer – Shafik Yaghmour Mar 26 '13 at 02:25
  • do i have to do this 16 times: getSequence( grid, Point(1,1) ); or can i just use a loop to change the numbers? – user1896464 Mar 26 '13 at 02:29
  • @user1896464 Added another addition with a loop that you could use, hopefully it is easy to modify for your needs – Shafik Yaghmour Mar 26 '13 at 02:34
  • You don't need two constructors; one with default arguments would suffice. – johnsyweb Mar 26 '13 at 02:53
  • @Johnsyweb Yeah, I was trying to be a little more general, do you think it is confusing having both? – Shafik Yaghmour Mar 26 '13 at 02:54
  • @ShafikYaghmour: I'm not sure "confusing" is the word I'd use, but should `Point` be extended to cope with three dimensions (say `int z` is added), it would be better to minimise the number of constructors to modify. – johnsyweb Mar 26 '13 at 03:37
  • In your amendment, I think you meant, "`Point( int a = 1, int b = 1) : x(a) , y(b) {}`" (or similar). – johnsyweb Mar 26 '13 at 04:34
1

As an alternative to the other answers, you could give Point a constructor:

struct Point
{
    Point(int x, int y) : x(x), y(y){}
    int x, y;
}

Then do:

getSequence(grid, Point(0, 0));

That will remove the need to explicitly construct (or modify) a Point object every time you want to call the function, and IMO is more readable since the purpose of Point in this context is merely a parameter to getSequence.

Note: the typedef keyword in front of struct is unnecessary in C++.

Community
  • 1
  • 1
JBentley
  • 6,099
  • 5
  • 37
  • 72
0

You have to create an instance of Point and pass it to getSequence method.

    Point gridpoint; 
    gridpoint.x = 0;
    gridpoint.y = 0;
    getSequence(grid, gridpoint);
    // Use gridpoint here   
Arun
  • 2,087
  • 2
  • 20
  • 33