Pointer help !!

This is a discussion on Pointer help !! within the C Programming forums, part of the General Programming Boards category; How to mutiply two matrixes using pointer representation ?? I know well how to do it without pointers....but I am ...

  1. #1
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79

    Pointer help !!

    How to mutiply two matrixes using pointer representation ??
    I know well how to do it without pointers....but I am facing difficulty while doing the same with pointers as I am new to it...Please help !

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You've been here long enough that you should know to show us your code, and ask specific questions...

    We're not going to write code for you.

  3. #3
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by CommonTater View Post
    You've been here long enough that you should know to show us your code, and ask specific questions...

    We're not going to write code for you.
    well I know how to pass an array to another function
    like I did here

    Code:
    #include <stdio.h>
    void display (int *q,int ,int);
    int main ()
        {
        int arr[3][3]={
            1,2,3,4,5,6,7,8,9
        };
        display (&arr[0][0],3,3);
        return 0 ;
        
    }
    void display (int *q,int row,int col)
    {
        int i,j ;
    printf ("Matrix is : \n");
        for (i=0;i<row;i++)
        { 
            for (j=0;j<col;j++)
            
                printf ("%d ",*(q+i*col+j));
                printf ("\n");    
        }
        
        printf ("Transpose of the matrix is : \n");
        
        for (i=0;i<row;i++)
        { 
            for (j=0;j<col;j++)
            
                printf ("%d ",*(q+j*col+i));
                printf ("\n");    
        }
    }
    but to multiply two matrixes I need to send two arrays into a single function using pointers..how to do it ??

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    If you can send one array into a function with one parameter, you can send two arrays into a function using two parameters. The only thing that changes is the amount of pointers in the function prototype.

  5. #5
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by whiteflags View Post
    If you can send one array into a function with one parameter, you can send two arrays into a function using two parameters. The only thing that changes is the amount of pointers in the function prototype.

    ya...but that would be sending two arrays one after another....not simultaneously...how to send them simultaneously ?

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  6. #6
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Change your function prototype and definition to handle the extra pointer parameter.

  7. #7
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,524
    Maybe I wasn't clear.

    Code:
    void display (int *q,int ,int, int *p);
    Now you see the difference in the amount of pointers in the prototype.

    Though, if q and p are not guaranteed to have the same dimensions, pass in rows and columns as well.

  8. #8
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by Tclausex View Post
    Change your function prototype and definition to handle the extra pointer parameter.
    Thanks a lot..but I am still having a bit problem while multiplying them in void display() after sending both the matrix

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  9. #9
    Team Bring It rajarshi's Avatar
    Join Date
    Nov 2011
    Location
    India
    Posts
    79
    Quote Originally Posted by whiteflags View Post
    Maybe I wasn't clear.

    Code:
    void display (int *q,int ,int, int *p);
    Now you see the difference in the amount of pointers in the prototype.

    Though, if q and p are not guaranteed to have the same dimensions, pass in rows and columns as well.


    ya .got u now !!

    " I failed in some subjects in exam , but my friend passed in all . Now he is an engineer in Microsoft and I am the owner of Microsoft !! "

    - Bill Gates .

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    531
    Don't forget that matrix multiplication can give you a different shaped matrix back. I think you'll need to return the number rows and cols of the result too. You could just work it out in the calling function, but that's not very clean.

    At this point I think I'd create a struct type to hold a matrix's elements and dimensions rather than pass around lots of matrix+row+cols all around the place.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-30-2009, 04:41 PM
  2. Replies: 9
    Last Post: 06-13-2009, 02:31 AM
  3. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  4. Replies: 4
    Last Post: 08-27-2007, 11:51 PM
  5. Replies: 4
    Last Post: 11-05-2006, 01:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21