Thread: Passing a double array to a function as an argument

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    361

    Passing a double array to a function as an argument

    In the attached code it won't let me pass the double array to the function. I am not sure how to fix it, I have tried to make a pointer and pass that as an argument but that didn't work either.

    I have tried numerous way I can think of trying to pass it as an argument but it simply doesn't like the arrays.

    There are 2 other errors in the code but knowing me it is something simple I am overlooking.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >directionfunct(&x, &y, coords[][13], &letters, ans, cx, cy, j, answer[12], ax, ay, attempt);
    This is a function call, not a function declaration. This will work better:

    directionfunct(&x, &y, coords, &letters, ans, cx, cy, j, answer[12], ax, ay, attempt);

    >while(letters != 12)
    do..while loops always end with a semicolon.

    And scattered throughout every conditional test for equality in your program you use = instead of ==. = means assignment, == means comparison. The two are not interchangeable.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Ahh, thanks...that helped a lot.

    It compiles fine but does not build now.

    I cannot find out what the problem is with it now, maybe tried to call something wrong.

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Here is the updated code.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Glirk Dient
    Ahh, thanks...that helped a lot.

    It compiles fine but does not build now.

    I cannot find out what the problem is with it now, maybe tried to call something wrong.
    Posting some error messages would help us.

    >>if(coords[cx][cy] = 0)
    You're using the wrong =, I guess it should be ==.

    >>int startingpoint(int *x, int *y, int attempt);
    >>int startingpoint(int x,int y, int attempt)
    The prototype and the function don't match types on the first two parameters.

    Also, directionfunct doesn't match either.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Homework.obj : error LNK2001: unresolved external symbol "int __cdecl directionfunct(int *,int *,int (* const)[13],int *,int,int,int,int *,int,int,int,int)" (?directionfunct@@YAHPAH0QAY0N@H0HHH0HHHH@Z)
    Homework.obj : error LNK2001: unresolved external symbol "int __cdecl startingpoint(int *,int *,int)" (?startingpoint@@YAHPAH0H@Z)
    Debug/Homework.exe : fatal error LNK1120: 2 unresolved externals

    Those are the errors.

    I am not sure how to fix those, and I am not sure exactly how to fix what hammer said about startingpoint() I did add the * and & in, tried both and neither worked.

    Maybe I should read up more on pointers.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Any suggestions on what could be causing the errors? Anything will help.
    Last edited by Glirk Dient; 09-08-2003 at 02:28 PM.

  8. #8
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>fatal error LNK1120: 2 unresolved externals
    This means that you are calling a function that doesn't actually exist. The function arguments are probably slightly different on the function call to the function definition. Also remember that the function prototype at the top of the source file must have the same parameters as the function header itself.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    The following code will compile (old code has been commented). However your code has an infinite loop somewhere.
    Code:
    /*****************************************/
    /*                                       */
    /*                                       */
    /*            AI Problem Solving         */
    /*                Created by:            */
    /*             Zachary Carlyon           */
    /*                                       */
    /*****************************************/
    
    //This program is made to run through a problem.
    //The problem is your have 6 squares, 3 rows and 2 collumns.
    //Inbetween each square and on the outside are streets, with parking meeters in which you must "check". You need to find the quickest route
    //Some streets have 2 sides of parking meeters in which you must go through twice to check both sides.
    //You cannot go on a street more than once, except for the streets with parking meeters in which you must go through only twice.
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    //int directionfunct(int *x, int *y, int coords[12][13], int *letters, int ans, int cx, int cy, int *j, int answer, int ax, int ay, int attempt);
    int directionfunct(int &x, int &y, int coords[][13], int &letters, int ans,int cx, int cy, int j, int answer[12], int ax, int ay, int attempt);
    
    //int startingpoint(int *x, int *y, int attempt);
    int startingpoint(int &x,int &y);
    
    int main(int argc, int* argv[])
    {
    	int answer[12];
    	int coords[12][13];
    	int letters;
    	int i,x,y,cx,cy,ax,ay,attempt,ans;
    	//int *j;
    	int j;
    
    	answer[1] = 1;
    	cx = 0;
    	cy = 0;
        j = 0;
    	attempt = 12;
    	
    	do
    	{
    
            if(attempt == 12)
    		{
    		    coords[1][2] = 1;
    	        coords[1][5] = 1;
    	        coords[2][3] = 1;
    	        coords[2][6] = 1;
    	        coords[3][4] = 1;
    	        coords[3][7] = 2;
                coords[4][8] = 1;
                coords[5][9] = 1;
    	        coords[5][6] = 2;
    	        coords[6][7] = 2;
                coords[6][10] = 2;
    	        coords[7][8] = 2;
    	        coords[7][11] = 2;
    	        coords[8][12] =1;
    	        coords[9][10] =1;
    	        coords[10][11] = 1;
    	        coords[11][12] = 1;
    		}
    		//startingpoint(&x, &y, attempt);
    		startingpoint(x, y);
    		//directionfunct(&x, &y, coords, &letters, ans, cx, cy, j, answer[12], ax, ay, attempt);
    		directionfunct(x, y, coords, letters, ans, cx, cy, j, answer, ax, ay, attempt);
    	}while(letters != 12);
    	for(i = 0;i < 13; i++)
    	{
    		printf("%d/n",answer[ans]);
    		ans++;
    	}
        
    	return 0;
    }
    
    //directionfunct() is what makes it keep moving and not  move into a space that is already used.
    //points[][] is the current point it is at, nextdir[][] is the next point it will move too.
    //int directionfunct(int x, int y, int coords[][13], int letters, int ans,int cx, int cy, int j,
    //int answer[12], int ax, int ay, int attempt)
    int directionfunct(int &x, int &y, int coords[][13], int &letters, int ans,int cx, int cy, int j, int answer[12], int ax, int ay, int attempt)
    {
    	int direction,done;
        int points[3][4];
    	int nextpoint[3][4];
    	int nextdir;
    
    	ans = 0;
    	x = ax;
    	y = ay;
    	done = 1;
    
    	points[1][1] = 1;
    	points[1][2] = 2;
    	points[1][3] = 3;
    	points[1][4] = 4;
    	points[2][1] = 5;
    	points[2][2] = 6;
    	points[2][3] = 7;
    	points[2][4] = 8;
    	points[3][1] = 9;
    	points[3][2] = 10;
    	points[3][3] = 11;
    	points[3][4] = 12;
    
    	nextpoint[1][1] = 1;
    	nextpoint[1][2] = 2;
    	nextpoint[1][3] = 3;
    	nextpoint[1][4] = 4;
    	nextpoint[2][1] = 5;
        nextpoint[2][2] = 6;
        nextpoint[2][3] = 7;
    	nextpoint[2][4] = 8;
        nextpoint[3][1] = 9;
    	nextpoint[3][2] = 10;
        nextpoint[3][3] = 11;
        nextpoint[3][4] = 12;
    
    	if(x == 0 && y == 0)
    	{
    		ax = 1;
    		ay = 1;
    		return ax, ay;
    	}
    
    	direction = (rand()%4)+1;
    	while(x !=4)
    	{
    	    if(direction == 1)
    		{
    			x++;
    			//if(coords[cx][cy] = 0)
    			if(coords[cx][cy] == 0)
    			{
    				x--;
    				done--;
                }
    		}
    	}
    	while(x !=1)
    	{
    	    if(direction == 2)
    		{
    		    x--;
    			//if(coords[cx][cy] = 0)
    			if(coords[cx][cy] == 0)
    			{
    				x++;
    				done--;
                }
    		}
    	}
    	while(y !=1)
    	{
    	    if(direction == 3)
    		{
    		    y++;
    			if(coords[cx][cy] == 0)
    			{
    				y--;
    				done--;
                }
    		}
    	}
    	while(y !=3)
    	{
    	    if(direction == 4)
    		{
    		    y--;
    			if(coords[cx][cy] == 0)
    			{
    				y++;
    				done--;
                }
    		}
    	}
    
    	nextdir = (rand()%4+1);
        while(ax !=4)
    	{
    	    if(direction == 1)
    		{
    			ax++;
    			if(coords[cx][cy] == 0)
    			{
    				ax--;
    				done--;
                }
    		}
    	}
    	while(ax !=1)
    	{
    	    if(direction == 2)
    		{
    		    ax--;
    			if(coords[cx][cy] == 0)
    			{
    				ax++;
    				done--;
                }
    		}
    	}
    	while(ay !=1)
    	{
    	    if(nextdir == 3)
    		{
    		    ay++;
    			if(coords[cx][cy] == 0)
    			{
    				ay--;
    				done--;
                }
    		}
    	}
    	while(ay !=3)
    	{
    	    if(nextdir == 4)
    		{
    		    ay--;
    			if(coords[cx][cy] == 0)
    			{
    				ay++;
    				done--;
                }
    		}
    	}
    
    	cx = points[x][y];
    	cy = nextpoint[ax][ay];
    	letters++;
    	ans++;
    	j = points[x][y];
    	answer[ans] = j;
    	letters++;
    	attempt--;
    	while(done != 0)
    	{
    	    coords[cx][cy]--;
    	}
    	return x,y,coords[cx][cy];
    }
    
    //startingpoint() creates a random starting point;
    //int startingpoint(int x,int y, int attempt)
    int startingpoint(int &x,int &y)
    {
    	//do
    	//{
    	    x = (rand()%3)+1;
    	    y = (rand()%4)+1;
    	//}while(attempt == 12);
    	
    	//return x,y;
    }

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I made a couple of other simple changes, but it still loops infinitely.
    Code:
    /*****************************************/
    /*                                       */
    /*                                       */
    /*            AI Problem Solving         */
    /*                Created by:            */
    /*             Zachary Carlyon           */
    /*                                       */
    /*****************************************/
    
    //This program is made to run through a problem.
    //The problem is your have 6 squares, 3 rows and 2 collumns.
    //Inbetween each square and on the outside are streets, with parking meeters in which you must "check". You need to find the quickest route
    //Some streets have 2 sides of parking meeters in which you must go through twice to check both sides.
    //You cannot go on a street more than once, except for the streets with parking meeters in which you must go through only twice.
    
    //#include "stdafx.h"
    #include "stdio.h"
    #include "stdlib.h"
    
    //int directionfunct(int *x, int *y, int coords[12][13], int *letters, int ans, int cx, int cy, int *j, int answer, int ax, int ay, int attempt);
    void directionfunct(int &x, int &y, int coords[][13], int &letters, int ans,int cx, int cy, int j, int answer[12], int ax, int ay, int &attempt);
    
    //int startingpoint(int *x, int *y, int attempt);
    void startingpoint(int &x,int &y);
    
    int main(int argc, int* argv[])
    {
    	int answer[12];
    	int coords[12][13];
    	int letters;
    	int i,x,y,cx,cy,ax,ay,attempt,ans;
    	//int *j;
    	int j;
    
    	answer[1] = 1;
    	cx = 0;
    	cy = 0;
        j = 0;
    	attempt = 12;
    	
    	do
    	{
    
            if(attempt == 12)
    		{
    		    coords[1][2] = 1;
    	        coords[1][5] = 1;
    	        coords[2][3] = 1;
    	        coords[2][6] = 1;
    	        coords[3][4] = 1;
    	        coords[3][7] = 2;
                coords[4][8] = 1;
                coords[5][9] = 1;
    	        coords[5][6] = 2;
    	        coords[6][7] = 2;
                coords[6][10] = 2;
    	        coords[7][8] = 2;
    	        coords[7][11] = 2;
    	        coords[8][12] =1;
    	        coords[9][10] =1;
    	        coords[10][11] = 1;
    	        coords[11][12] = 1;
    		}
    		//startingpoint(&x, &y, attempt);
    		startingpoint(x, y);
    		//directionfunct(&x, &y, coords, &letters, ans, cx, cy, j, answer[12], ax, ay, attempt);
    		directionfunct(x, y, coords, letters, ans, cx, cy, j, answer, ax, ay, attempt);
    	}while(letters != 12);
    	for(i = 0;i < 13; i++)
    	{
    		printf("%d/n",answer[ans]);
    		ans++;
    	}
        
    	return 0;
    }
    
    //directionfunct() is what makes it keep moving and not  move into a space that is already used.
    //points[][] is the current point it is at, nextdir[][] is the next point it will move too.
    //int directionfunct(int x, int y, int coords[][13], int letters, int ans,int cx, int cy, int j,
    //int answer[12], int ax, int ay, int attempt)
    void directionfunct(int &x, int &y, int coords[][13], int &letters, int ans,int cx, int cy, int j, int answer[12], int ax, int ay, int &attempt)
    {
    	int direction,done;
        int points[3][4];
    	int nextpoint[3][4];
    	int nextdir;
    
    	ans = 0;
    	x = ax;
    	y = ay;
    	done = 1;
    
    	points[1][1] = 1;
    	points[1][2] = 2;
    	points[1][3] = 3;
    	points[1][4] = 4;
    	points[2][1] = 5;
    	points[2][2] = 6;
    	points[2][3] = 7;
    	points[2][4] = 8;
    	points[3][1] = 9;
    	points[3][2] = 10;
    	points[3][3] = 11;
    	points[3][4] = 12;
    
    	nextpoint[1][1] = 1;
    	nextpoint[1][2] = 2;
    	nextpoint[1][3] = 3;
    	nextpoint[1][4] = 4;
    	nextpoint[2][1] = 5;
        nextpoint[2][2] = 6;
        nextpoint[2][3] = 7;
    	nextpoint[2][4] = 8;
        nextpoint[3][1] = 9;
    	nextpoint[3][2] = 10;
        nextpoint[3][3] = 11;
        nextpoint[3][4] = 12;
    
    	if(x == 0 && y == 0)
    	{
    		ax = 1;
    		ay = 1;
    		//return ax, ay;
    		return;
    	}
    
    	direction = (rand()%4)+1;
    	while(x !=4)
    	{
    	    if(direction == 1)
    		{
    			x++;
    			//if(coords[cx][cy] = 0)
    			if(coords[cx][cy] == 0)
    			{
    				x--;
    				done--;
                }
    		}
    	}
    	while(x !=1)
    	{
    	    if(direction == 2)
    		{
    		    x--;
    			//if(coords[cx][cy] = 0)
    			if(coords[cx][cy] == 0)
    			{
    				x++;
    				done--;
                }
    		}
    	}
    	while(y !=1)
    	{
    	    if(direction == 3)
    		{
    		    y++;
    			if(coords[cx][cy] == 0)
    			{
    				y--;
    				done--;
                }
    		}
    	}
    	while(y !=3)
    	{
    	    if(direction == 4)
    		{
    		    y--;
    			if(coords[cx][cy] == 0)
    			{
    				y++;
    				done--;
                }
    		}
    	}
    
    	nextdir = (rand()%4+1);
        while(ax !=4)
    	{
    	    if(direction == 1)
    		{
    			ax++;
    			if(coords[cx][cy] == 0)
    			{
    				ax--;
    				done--;
                }
    		}
    	}
    	while(ax !=1)
    	{
    	    if(direction == 2)
    		{
    		    ax--;
    			if(coords[cx][cy] == 0)
    			{
    				ax++;
    				done--;
                }
    		}
    	}
    	while(ay !=1)
    	{
    	    if(nextdir == 3)
    		{
    		    ay++;
    			if(coords[cx][cy] == 0)
    			{
    				ay--;
    				done--;
                }
    		}
    	}
    	while(ay !=3)
    	{
    	    if(nextdir == 4)
    		{
    		    ay--;
    			if(coords[cx][cy] == 0)
    			{
    				ay++;
    				done--;
                }
    		}
    	}
    
    	cx = points[x][y];
    	cy = nextpoint[ax][ay];
    	letters++;
    	ans++;
    	j = points[x][y];
    	answer[ans] = j;
    	letters++;
    	attempt--;
    	while(done != 0)
    	{
    	    coords[cx][cy]--;
    	}
    	//return x,y,coords[cx][cy];
    }
    
    //startingpoint() creates a random starting point;
    //int startingpoint(int x,int y, int attempt)
    void startingpoint(int &x,int &y)
    {
    	//do
    	//{
    	    x = (rand()%3)+1;
    	    y = (rand()%4)+1;
    	//}while(attempt == 12);
    	
    	//return x,y;
    }

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Here's one loop that's suspicious (unless done is 0, this loops forever):
    Code:
    	while(done != 0)
    	{
    	    coords[cx][cy]--;
    	}

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Swoopy, you can edit posts rather than post some slightly changed 50 lines of code twice...Sorry just something that gets on my nerves
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    True, but you don't increase your post count that way . I can understand why it gets on your nerves. The reason I don't is it can get confusing if someone copies your code, then they find mistakes, and then they point them out. In the meantime you've edited the post to fix those mistakes.

    Maybe if the mods find it irritating as well, they can edit out the duplicate code.

    Swoopy

  14. #14
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Awsome, thanks a lot it works. It's running right now, hopefully it will find an optimal answer, or else I guess I will need to mess around with the code some more.

  15. #15
    Registered User
    Join Date
    Jan 2003
    Posts
    361
    Apparently now the program has to close whenever it reaches x or y withing directionfunct().

    Apparently after I declare points x and y seem to crash the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 04:09 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  4. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM