Thread: Need some help...

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Need some help...

    My code manage to build without errors, but it crashes when I try to run it. Anybody can give me some help?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    #include <malloc.h>
    
    void rubikdesign(int **rubikm);
    void switchrowncolumn(int **rubikm, int r);
    void rotateupperleft(int **rubikm);
    void rotatelowerleft(int **rubikm);
    void rotatemidclock(int **rubikm);
    void rotatemidanticlock(int **rubikm);
    void complete(int **rubikcc);
    
    
    int main(void)
    {
    	int i,j, **rubik, **rubikc, n;
    	char choice, mode;
    	
    	rubikdesign(rubik);
    	complete(rubikc);
    	
    	for(i=0; i<2; i++)
    	{
    		for(j=0; j<4; j++)
    		{
    			printf("%d", rubik[i][j]);
    		}
    		printf("\n");
    	}
    	
    	printf("Select the mode of solution:\n\n");
    	printf("1. Manual Mode\n");
    	printf("2. Automated Mode\n\n");
    	printf("Enter your selection: ");
    	scanf("%c", &mode);
    	
    	if(mode == '1')
    	{  	 
    		do
        	{
    			for(i=0; i<2; i++)
    			{
    				for(j=0; j<4; j++)
    				{
    					printf("%d", rubik[i][j]);
    				}
    				printf("\n");
    			}
    			printf("Manual Mode\n");
    			printf("Transform Operations\n");
    		
    			printf("n0 = Switch rows for the nth column from the left, where n = 1,2,3,4\n");
    			printf(" 7 = Rotate Upper row left one step\n");
    			printf(" 1 = Rotate Lower row left one step\n");
    			printf(" 2 = Rotate the mid four 'squares/numbers' Clockwise one step\n");
    			printf(" 8 = Rotate the mid four 'squares/numbers' Anti-Clockwise one step\n");
    	        
    			printf("Enter Transform Operation (one at a time): ");
    			scanf("%c", &choice);
            
    			switch (choice)
            	{
            		case  'n':
    					printf("Enter column that will switch rows(n=1,2,3,4): ");
    					scanf("%d", &n);
               			switchrowncolumn(rubik, n);
            		break;
            		case  '7':
             		    rotateupperleft(rubik);
           			break;
            		case  '1':
                		rotatelowerleft(rubik);
            		break;
            		case  '2':
                		rotatemidclock(rubik);
           			break;
           			case '8':
                		rotatemidanticlock(rubik);
            		break;
            		default:
                		printf("Invalid choice\n");
            		break;
            	}
        	} while(**rubik != (**rubikc));
    	}
    	
    	else if(mode == '2')
    	{
    		return 0;
    	}
    	
    	/*Free memory for each row*/
    	for(i=0;i<2;i++)
    	{
    		free(rubik[i]);
    	}
    		
    	free(rubik);
    	
    	return 0;	 
    }
    
    void rubikdesign(int **rubikm)
    {
    	int i;
    
    	/*Memory allocation for row*/
    	rubikm = (int**)malloc(2*sizeof(int*));
    		
    	/*Memory allocation for coloumn*/
    	for(i=0; i < 4; i++)
    	{
    		rubikm[i] = (int*)malloc(4*sizeof(int));
    	}
    		
    	rubikm[0][0] = 4;
    	rubikm[0][1] = 7;
    	rubikm[0][2] = 1;
    	rubikm[0][3] = 6;
    	rubikm[1][0] = 2;
    	rubikm[1][1] = 3;
    	rubikm[1][2] = 8;
    	rubikm[1][3] = 5;
    		
    }
    
    void switchrowncolumn(int **rubikm, int r)
    {
    	int temp;
    	
    	temp = rubikm[0][r];
    	rubikm[0][r] = rubikm[1][r];
    	rubikm[1][r] = temp;
    }
    
    void rotateupperleft(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[0][0];
    	rubikm[0][0] = rubikm[0][1];
    	rubikm[0][1] = rubikm[0][2];
    	rubikm[0][2] = rubikm[0][3];
    	rubikm[0][3] = temp;
    
    }
    
    void rotatelowerleft(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[1][0];
    	rubikm[1][0] = rubikm[1][1];
    	rubikm[1][1] = rubikm[1][2];
    	rubikm[1][2] = rubikm[1][3];
    	rubikm[1][3] = temp;
    }
    
    void rotatemidclock(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[0][1];
    	rubikm[0][1] = rubikm[1][1];
    	rubikm[1][1] = rubikm[1][2];
    	rubikm[1][2] = rubikm[0][2];
    	rubikm[0][2] = temp;
    
    }
    
    void rotatemidanticlock(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[0][1];
    	rubikm[0][1] = rubikm[0][2];
    	rubikm[0][2] = rubikm[1][2];
    	rubikm[1][2] = rubikm[1][1];
    	rubikm[1][1] = temp;
    }
    
    
    void complete(int **rubikcc)
    {
    	rubikcc[0][0] = 1;
    	rubikcc[0][1] = 2;
    	rubikcc[0][2] = 3;
    	rubikcc[0][3] = 4;
    	rubikcc[1][0] = 5;
    	rubikcc[1][1] = 6;
    	rubikcc[1][2] = 7;
    	rubikcc[1][3] = 8;
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    First off, don't include malloc.h; stdlib.h is what you need to include to use malloc(). Also, you shouldn't cast the return value of malloc() as it's unnecessary.

    Code:
    	/*Memory allocation for row*/
    	rubikm = (int**)malloc(2*sizeof(int*));
    You are allocating space for an array of integer pointers. This array is of size 2.
    Code:
    	/*Memory allocation for coloumn*/
    	for(i=0; i < 4; i++)
    	{
    		rubikm[i] = (int*)malloc(4*sizeof(int));
    	}
    The array you allocated is of size 2, yet you are iterating through it as if it were size 4.

    Also, rubikm will not be returned back to the calling function. It may be difficult for you to understand since you are using a double pointer, but you are essentially doing this:
    Code:
    void foo(int x)
    {
        x = 5;
    }
    
    int main(void)
    {
        int x;
        foo(x);
        printf("x = %d\n", x);  // x does not equal 5 here since it was passed by value
    }
    You are passing the double pointer by value, so the memory that you allocate in the function is lost since the caller's pointer will not point to that memory.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    So to solve the problem....

    Can anybody give me some pointers on how to pass back and forth between 2-d pointers?

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by darkconvoy View Post
    Can anybody give me some pointers on how to pass back and forth between 2-d pointers?
    How about returning the same pointer that you passed to the function, as in
    Code:
    rubik = rubikdesign(rubik);

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    New problem

    This set of code has some problems passing back and forth the 2D pointer through the functions [The program says incompatible pointer type]:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <string.h>
    
    void rubikdesign(int **rubikm);
    void switchrowncolumn(int **rubikm, int r);
    void rotateupperleft(int **rubikm);
    void rotatelowerleft(int **rubikm);
    void rotatemidclock(int **rubikm);
    void rotatemidanticlock(int **rubikm);
    void complete(int **rubikcc);
    
    
    int main(void)
    {
    	int i,j, **rubik, **rubikc, n;
    	char choice, mode;
    	
    	rubikdesign(&rubik);
    	complete(&rubikc);
    	
    	for(i=0; i<2; i++)
    	{
    		for(j=0; j<4; j++)
    		{
    			printf("%d", rubik[i][j]);
    		}
    		printf("\n");
    	}
    	
    	printf("Select the mode of solution:\n\n");
    	printf("1. Manual Mode\n");
    	printf("2. Automated Mode\n\n");
    	printf("Enter your selection: ");
    	scanf("%c", &mode);
    	
    	if(mode == '1')
    	{  	 
    		do
        	{
    			for(i=0; i<2; i++)
    			{
    				for(j=0; j<4; j++)
    				{
    					printf("%d", rubik[i][j]);
    				}
    				printf("\n");
    			}
    			printf("Manual Mode\n");
    			printf("Transform Operations\n");
    		
    			printf(" 0 = Switch rows for the nth column from the left, where n = 1,2,3,4\n");
    			printf(" 7 = Rotate Upper row left one step\n");
    			printf(" 1 = Rotate Lower row left one step\n");
    			printf(" 2 = Rotate the mid four 'squares/numbers' Clockwise one step\n");
    			printf(" 8 = Rotate the mid four 'squares/numbers' Anti-Clockwise one step\n");
    	        
    			printf("Enter Transform Operation (one at a time): ");
    			scanf("%c", &choice);
            
    			switch (choice)
            	{
            		case  '0':
    					printf("Enter nth column that will switch rows(n=1,2,3,4): ");
    					scanf("%d", &n);
               			switchrowncolumn(&rubik, n);
            		break;
            		case  '7':
             		    rotateupperleft(&rubik);
           			break;
            		case  '1':
                		rotatelowerleft(&rubik);
            		break;
            		case  '2':
                		rotatemidclock(&rubik);
           			break;
           			case '8':
                		rotatemidanticlock(&rubik);
            		break;
            		default:
                		printf("Invalid choice\n");
            		break;
            	}
        	} while(**rubik != (**rubikc));
    	}
    	
    	else if(mode == '2')
    	{
    		return 0;
    	}
    	
    	/*Free memory for each row*/
    	for(i=0;i<2;i++)
    	{
    		free(rubik[i]);
    	}
    		
    	free(rubik);
    	
    	return 0;	 
    }
    
    void rubikdesign(int **rubikm)
    {
    	int i;
    
    	/*Memory allocation for row*/
    	rubikm = (int**)malloc(2 * sizeof(int*));
    		
    	/*Memory allocation for coloumn*/
    	for(i=0; i < 2; i++)
    	{
    		rubikm[i] = (int*)malloc(4*sizeof(int));
    	}
    		
    	rubikm[0][0] = 4;
    	rubikm[0][1] = 7;
    	rubikm[0][2] = 1;
    	rubikm[0][3] = 6;
    	rubikm[1][0] = 2;
    	rubikm[1][1] = 3;
    	rubikm[1][2] = 8;
    	rubikm[1][3] = 5;
    		
    }
    
    void switchrowncolumn(int **rubikm, int r)
    {
    	int temp;
    	
    	temp = rubikm[0][r-1];
    	rubikm[0][r-1] = rubikm[1][r-1];
    	rubikm[1][r-1] = temp;
    }
    
    void rotateupperleft(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[0][0];
    	rubikm[0][0] = rubikm[0][1];
    	rubikm[0][1] = rubikm[0][2];
    	rubikm[0][2] = rubikm[0][3];
    	rubikm[0][3] = temp;
    
    }
    
    void rotatelowerleft(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[1][0];
    	rubikm[1][0] = rubikm[1][1];
    	rubikm[1][1] = rubikm[1][2];
    	rubikm[1][2] = rubikm[1][3];
    	rubikm[1][3] = temp;
    }
    
    void rotatemidclock(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[0][1];
    	rubikm[0][1] = rubikm[1][1];
    	rubikm[1][1] = rubikm[1][2];
    	rubikm[1][2] = rubikm[0][2];
    	rubikm[0][2] = temp;
    
    }
    
    void rotatemidanticlock(int **rubikm)
    {
    	int temp;
    	
    	temp = rubikm[0][1];
    	rubikm[0][1] = rubikm[0][2];
    	rubikm[0][2] = rubikm[1][2];
    	rubikm[1][2] = rubikm[1][1];
    	rubikm[1][1] = temp;
    }
    
    
    void complete(int **rubikcc)
    {
    	rubikcc[0][0] = 1;
    	rubikcc[0][1] = 2;
    	rubikcc[0][2] = 3;
    	rubikcc[0][3] = 4;
    	rubikcc[1][0] = 5;
    	rubikcc[1][1] = 6;
    	rubikcc[1][2] = 7;
    	rubikcc[1][3] = 8;
    }

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    30

    Help me anyone..??

    Can anyone help me with my code?

Popular pages Recent additions subscribe to a feed