Thread: double astrix 2D array signature question..

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i remember in previous examples in this forum
    It sounds like you are rote learning rather than actually understanding the reason behind doing that.

    Quote Originally Posted by transgalactic2
    that inorder to use int** we need to pass &arr
    so we could make changes on *arr
    That would be true if arr is an int* and you wanted to change the pointer itself from within the function such that the change is reflected in the caller. Here you just want to pass a pointer to a pointer, so why take its address?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i tried to write a program for it
    but i get read accsses error
    i dont know why??

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
     
    int what1(int **arr, int m, int n); 
    int what2 (int **arr, int row, int col, int m, int n);
    int main()
    {
    	int ** arr=(int**)malloc(6*sizeof(int*));
    	*arr=(int*)malloc(5*sizeof(int));
    
    	arr[0][0]=0;
    	arr[0][1]=1;
    	arr[0][2]=1;
    	arr[0][3]=0;
    	arr[0][4]=1;
    	
        arr[1][0]=0;
    	arr[1][1]=0;
    	arr[1][2]=1;
    	arr[1][3]=0;
    	arr[1][4]=0;
    
    	 arr[2][0]=0;
    	arr[2][1]=0;
    	arr[2][2]=1;
    	arr[2][3]=1;
    	arr[2][4]=1;
    
     arr[3][0]=0;
    	arr[3][1]=1;
    	arr[3][2]=0;
    	arr[3][3]=0;
    	arr[3][4]=0;
    
     arr[4][0]=1;
    	arr[4][1]=0;
    	arr[4][2]=0;
    	arr[4][3]=1;
    	arr[4][4]=1;
    
     arr[5][0]=1;
    	arr[5][1]=0;
    	arr[5][2]=1;
    	arr[5][3]=1;
    	arr[5][4]=0;
    
    what1(arr,6,5) ;
    	return 0;
    }
    
    int what1(int **arr, int m, int n){
    int i, j, tmp, stam=0;
    
    for(i=0; i<m; i++)
    for(j=0; j<n; j++){
       tmp = what2(arr,i,j,m,n);
       if (tmp>stam) 
             stam = tmp;
    }
    return stam;
    }
    
    int what2 (int **arr, int row, int col, int m, int n){
    int i,j,tmp, stam=0;
    
    if (row < 0 || row >= m || col < 0 || col >= n) return 0;
    if (arr[row][col] == 0) return 0;
     
    
    arr[row][col] = 0;
    for(i=-1; i<2; i++)
    	for(j=-1; j<2; j++){
    		if(!i && !j) continue;
    tmp = 1 + what2(arr, row+i, col+j, m, n);	
    if (tmp > stam)  stam = tmp;
    	}
    arr[row][col] = 1;
    
    return stam;
    }

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by transgalactic2
    i tried to write a program for it
    but i get read accsses error
    i dont know why??
    You only allocated space for one pointer element to point to in your dynamically allocated array of pointers.

    It does not have to be so hard. For example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    void print(int **numbers, size_t num_rows, size_t num_cols);
    
    int main(void)
    {
        const size_t num_rows = 4;
        const size_t num_cols = 3;
    
        /* For simplification, assume that malloc() never returns a null pointer. */
        int **numbers = malloc(num_rows * sizeof(*numbers));
        size_t n = 1;
        size_t i;
        for (i = 0; i < num_rows; ++i)
        {
            size_t j;
            numbers[i] = malloc(num_cols * sizeof(*numbers[i]));
            for (j = 0; j < num_cols; ++j)
            {
                numbers[i][j] = n++;
            }
        }
    
        print(numbers, num_rows, num_cols);
    
        for (i = 0; i < num_rows; ++i)
        {
            free(numbers[i]);
        }
        free(numbers);
    
        return 0;
    }
    
    void print(int **numbers, size_t num_rows, size_t num_cols)
    {
        size_t i;
        for (i = 0; i < num_rows; ++i)
        {
            size_t j;
            for (j = 0; j < num_cols; ++j)
            {
                printf("%d ", numbers[i][j]);
            }
            puts("");
        }
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    Quote Originally Posted by laserlight View Post
    It sounds like you are rote learning rather than actually understanding the reason behind doing that.


    That would be true if arr is an int* and you wanted to change the pointer itself from within the function such that the change is reflected in the caller. Here you just want to pass a pointer to a pointer, so why take its address?
    yes we changed the head of the linked list
    so we use &head

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiplying a 2D array by a 1D array
    By youngvito in forum C Programming
    Replies: 14
    Last Post: 06-12-2009, 03:50 PM
  2. Question Fortran like 2D array
    By TauWich in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2007, 06:26 AM
  3. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM
  4. 2d array question
    By JoshR in forum C++ Programming
    Replies: 5
    Last Post: 04-09-2005, 12:52 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM