CrazyEngineers
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies
  • pradeep_agrawal

    MemberMar 16, 2008

    sahana
    i have to multiply 2 n*m matrices using pointers
    When you say pointers, do you meant that the matrices are implemented using linked list? Could you please provide more details on how the matrices are implemented.

    -Pradeep
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberMar 17, 2008

    yeah u can implement it by a linked list.
    Are you sure? This action cannot be undone.
    Cancel
  • friendster7

    MemberMar 17, 2008

    if u need to multiply then check out this..this might help

     #include<stdio.h>
    #include<conio.h>
    
    main()
    {
        int mtrx1[5][5],mtrx2[5][5],product[5][5],ro1,ro2,col1,col2;
        unsigned short int posblty;
        static unsigned short int call_main = 0;
        if(call_main == 0 || call_main % 10 == 0)
        {
            clrscr();
            printf("\nThis program will calculate the product of two matrices");
            printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        }
        call_main++;
        getnum(&ro1,1);
        getnum(&col1,3);
        getnum(&ro2,2);
        getnum(&col2,4);
        posblty = is_posble(col1,ro2);
        if(!posblty)
        {
            getch();
            repeat();
        }
        getmatrix(mtrx1,ro1,col1,1);
        getmatrix(mtrx2,ro2,col2,2);
        multiply(mtrx1,mtrx2,product,ro1,col2,col1);
        print_matrix(product,ro1,col2);
        find_trace(product,ro1,col2);
        repeat();
    }
    
    getnum(num,message)
    int *num,message;
    {
        do
        {
        switch(message)
        {
            case 1:
            printf("\nEnter the number of rows in matrix 1:");
            break;
    
            case 2:
            printf("\nEnter the number of rows in matrix 2:");
            break;
    
            case 3:
            printf("\nEnter the number of columns in matrix 1:");
            break;
    
            case 4:
            printf("\nEnter the number of columns in matrix 2:");
            break;
    
            default:
            printf("\nEnter a number:");
        }
        scanf("%d",num);
        }
        while(*num > 5);
        return;
    }
    
    is_posble(col_1,row_2)
    int row_2,col_1;
    {
        if(col_1 != row_2)
        {
            printf("\nThe multiplication of given matrices is not possible");
            return(0);
        }
        else
        {
            return(1);
        }
    }
    
    getmatrix(array,row,column,mtrx_num)
    int *array[5],row,column,mtrx_num;
    {
        int i,j;
        printf("\nEnter matrix %d\n",mtrx_num);
    
        for(i = 0;i < row;i++)
        {
            for(j = 0;j < column;j++)
            {
                scanf("%d",array+(5*i+j));
            }
        }
        return;
    }
    
    multiply(matrix1,matrix2,product,l,m,n)
    int *matrix1[5],*matrix2[5],*product[5],l,m,n;
    {
        int i,j,k,temp1,temp2,temp3;
    
        for(i=0;i < l;i++)
        {
            for(j = 0;j < m;j++)
            {
                *(product+(5*i+j)) = 0;
                for(k = 0;k < n;k++)
                {
                    temp1 = *(matrix1+(5*i+k));
                    temp2 = *(matrix2+(5*k+j));
                    temp3 = *(product+(5*i+j));
                    *(product+(5*i+j)) = temp1 * temp2 + temp3;
                }
            }
        }
        return;
    }
    
    print_matrix(array,row,col)
    int *array[5];
    {
        int i,j;
        printf("\nThe product is :");
        for(i = 0;i < row;i++)
        {
            printf("\n");
            for(j = 0;j < col;j++)
            {
                printf("%5d \t",*(array+(5*i+j)));
            }
        }
        return;
    }
    
    
    Are you sure? This action cannot be undone.
    Cancel
  • sahana

    MemberMar 18, 2008

    hi friendster7,
    i got your code.but is the most optimal?this code is designed for matrices which have a maximum of 5 rows and 5 columns right?so if i dont use a 5*5 matrix (say 3*3 ), its going to be a waste of space right?can we make it a little more optimal.how about dyanamic memory allocation?
    please tell me if i have undserstood it wrongly.
    another constraint i forgot to add in the question is that we cannot get the size of the matrix during runtime and then assign the subscript of the array.
    ie something like
    scanf("%d,%d",&rows,&col);
    int array[row][col];

    otherwise your code was really good.
    Are you sure? This action cannot be undone.
    Cancel
Home Channels Search Login Register