threads in Win32 using C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Anjola
    New Member
    • Sep 2006
    • 11

    threads in Win32 using C++

    if I haveto multiply two matrices one 3x2 and the other 2x3, what threads I have to create . I know that there are 10 of them.
    #include <stdio.h>
    #include <iostream.h>
    /* Allocate memory for a matrix of given order */
    void create_matrix( int ***mat, int m, int n)
    {
    int **la = NULL;
    int i;

    //la = (int**)malloc(m * sizeof(int*));

    for ( i=0; i <m; i++ )
    la[i] = (int *)malloc( n* sizeof(int));

    *mat = la;

    return;
    }


    /* free memory of matrix allocated in create_matrix() */

    void free_matrix( int ***mat, int m)
    {
    int **la = NULL;
    int i;

    la = *mat;

    for ( i=0; i <m; i++ )
    free (la[i] );

    free ( la );

    *mat=NULL;

    return;
    }


    /* Print a matrix of order (mxn) */

    void print_matrix( int **mat, int m, int n)
    {
    int i,j;

    printf("Matrix of order(%dx%d)\n" ,m,n);
    printf("\n");
    for ( i=0; i <m; i++ )
    {
    for ( j=0; j <n; j++ )
    printf("%4d",ma t[i][j]);
    printf("\n");
    }
    return;
    }

    /* Read a matrix of order (mxn) */
    void read_matrix( int **mat, int m, int n)
    {
    int i,j;

    printf("\nEnter %d elemnts for row, %d times\n",n,m);

    for ( i=0; i <m; i++ )
    {
    for ( j=0; j <n; j++ )
    scanf("%d",&mat[i][j]);
    }
    return;
    }
    int main()
    {
    int i,j,k;
    int **a,**b,**c;

    int m1,n1; /* A of m1xn1 */
    int m2,n2; /* B of m2xn2 */

    int m3,n3; /* C of m3xn3 => m1Xn2 (?) */

    while(1) {

    printf("\nEnter order of the First matrix: ");
    scanf("%d%d",&m 1,&n1);

    printf("\nEnter order of the Second matrix: ");
    scanf("%d%d",&m 2,&n2);

    /* Check if n1 == m2 */
    if ( m2 == n1 )
    break;

    printf("\nMatri ces can not be multiplied.. Try again.. ");

    }

    m3 = m1;
    n3 = n2;

    /* Allocate memory for all three matrices */

    create_matrix( &a, m1, n1 );
    create_matrix( &b, m2, n2 );
    create_matrix( &c, m3, n3 );

    /* Read the matrices to be multiplied */

    read_matrix( a, m1, n1 );
    read_matrix( b, m2, n2 );

    print_matrix( a, m1, n1 );
    print_matrix( b, m2, n2 );

    /* Multyply the matrices */

    for ( i = 0; i < m3; i++ )
    {
    for ( j = 0; j < n3; j++ )
    {
    c[i][j] = 0;

    for ( k = 0; k < m2; k ++ )
    c[i][j] += a[i][k]*b[k][j] ;
    }
    }

    print_matrix( c, m3, n3 );

    free_matrix( &a, m1 );
    free_matrix( &b, m2 );
    free_matrix( &c, m3 );

    return 0;

    }
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by Anjola
    if I haveto multiply two matrices one 3x2 and the other 2x3, what threads I have to create . I know that there are 10 of them.
    a. There are rather more than 10 threads on any running Windows system.

    b. But you should only need 1 to perform any sort of mathematical operation including matrix multiplication.

    Comment

    Working...