Thread: Passing a 2d array by Reference

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204

    Passing a 2d array by Reference

    I created a problem and solved it. Now i rewrote it and try to pass a variable named square by reference. Now im having trouble. What is the right way to pass a 2d array by reference?


    Call by Value - Working
    PHP Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>


    #define SIZE 9


    void createprintdataint [SIZE][SIZE] );
    int hasduplicateint [SIZE][SIZE], int );
    void printmaxint [SIZE][SIZE] );

    int square[SIZE][SIZE] = { };


    int main()
    {
        
    clrscr();
        
    createprintdatasquare );
        
    getch();
        return 
    0;
    }


    void createprintdataint location[SIZE][SIZE] )
    {
        
    int genint 0;
        
    int col 0row 0;

        
    srandtime) );

        for( 
    col 0col != SIZEcol++ ){
            
    gotoxy( ( col ) * 5);
            
    printf"(%d)"col );
        }


        for( 
    row 0row != SIZErow++ ){
            
    gotoxy2,  ( row ) * );
            
    printf"(%d)"row );

            for( 
    col 0genint 0col != SIZEcol++ ){
                while( !
    genint || hasduplicatelocationgenint ) ){
                    
    genint = ( rand() % 99 ) + 1;
                }
                
    location[row][col] = genint;
                
    gotoxy( ( col ) * 5, ( row ) * );
                
    printf"%d"genint );
            }
        }
        
    gotoxy2522 );
        
    printmaxlocation );
    }


    int hasduplicateint location[SIZE][SIZE], int i )
    {
        
    int hasdup 0;
        
    int rowcol;

        for( 
    col 0col != SIZEcol++ ){
            for( 
    row 0row != SIZErow++ ){
                if( 
    location[row][col] == ){
                    
    hasdup 1;
                    break;
                }
            }
            if( 
    hasdup ) break;
        }
        return 
    hasdup;
    }


    void printmaxint location[SIZE][SIZE] )
    {
        
    int rowcol;
        
    int max location[0][0], min maxcurval min;

        for( 
    row 0row != SIZErow++ ){
            for( 
    col 0col != SIZEcol++ ){
                
    curval location[row][col];
                
    max = ( curval max curval max );
                
    min = ( curval min curval min );
            }
        }

        
    printf"\nThe highest value generated is %d"max );
        
    printf"\nThe lowest value generated is %d"min );




    Call by Reference - Not Working!!
    PHP Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>


    #define SIZE 9


    void createprintdataint ** );
    int hasduplicateint **, int );
    void printmaxint ** );



    int main()
    {
        
    int square[12][12] = { };
        
    clrscr();
        
    createprintdatasquare );
        
    getch();
        return 
    0;
    }


    void createprintdataint **location )
    {
        
    int genint 0;
        
    int col 0row 0;

        
    srandtime) );

        for( 
    col 0col != SIZEcol++ ){
            
    gotoxy( ( col ) * 5);
            
    printf"(%d)"col );
        }


        for( 
    row 0row != SIZErow++ ){
            
    gotoxy2,  ( row ) * );
            
    printf"(%d)"row );

            for( 
    col 0genint 0col != SIZEcol++ ){
                while( !
    genint || hasduplicatelocationgenint ) ){
                    
    genint = ( rand() % 99 ) + 1;
                }
                
    location[row][col] = genint;
                
    gotoxy( ( col ) * 5, ( row ) * );
                
    printf"%d"genint );
            }
        }
        
    gotoxy2522 );
        
    printmaxlocation );
    }


    int hasduplicateint **locationint i )
    {
        
    int hasdup 0;
        
    int rowcol;

        for( 
    col 0col != SIZEcol++ ){
            for( 
    row 0row != SIZErow++ ){
                if( 
    location[row][col] == ){
                    
    hasdup 1;
                    break;
                }
            }
            if( 
    hasdup ) break;
        }
        return 
    hasdup;
    }


    void printmaxint **location )
    {
        
    int rowcol;
        
    int max location[0][0], min maxcurval min;

        for( 
    row 0row != SIZErow++ ){
            for( 
    col 0col != SIZEcol++ ){
                
    curval location[row][col];
                
    max = ( curval max curval max );
                
    min = ( curval min curval min );
            }
        }

        
    printf"\nThe highest value generated is %d"max );
        
    printf"\nThe lowest value generated is %d"min );

    Last edited by loko; 07-23-2005 at 05:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Passing an array as reference to a function?
    By Kylecito in forum C++ Programming
    Replies: 10
    Last Post: 03-11-2006, 02:25 AM
  4. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  5. Passing 2D array to a function
    By Krupux in forum C Programming
    Replies: 4
    Last Post: 09-04-2003, 07:08 AM