0

行列乗算用の ac プログラムを作成しました。ここで、行数を N/5 の 5 ブロックに分割します。関数は、最初の反復で行の最初のブロックを計算し、2 番目の反復で 2 番目の部分を計算する必要があります。各反復で開始行と終了行を指定するにはどうすればよいですか? 以下で試してみました。誰かがそれを修正するのを手伝ってくれませんか。

#include <stdio.h>
#include <stdlib.h>
int main()
{
    int i, m, n, p, q, c, d, k,g, sum = 0,start=0,end=m/5;
    int **first, **second, **multiply;
    printf("Enter the number of rows and columns of first matrix\n");
    scanf("%d%d", &m, &n);
    printf("Value entered %d%d \n",m,n);
    first = malloc(m*sizeof(int*));
    for ( i =0;i <m; i++)
        first[i] =malloc(n*sizeof(int));
    printf("Enter the number of rows and columns of second matrix \n");
    scanf("%d%d", &p,&q);
    printf("value entered %d%d \n",p,q);
    second = malloc(p*sizeof(int*));
    for( i=0;i<p;i++)
        second[i] = malloc(q*sizeof(int));
    multiply = malloc(m*sizeof(int*));
    for ( i=0;i<m;i++)
        multiply[i] = malloc(q*sizeof(int));
    printf("Enter the elements of first matrix\n");
    for( c = 0 ; c < m ; c++ )
        for ( d = 0 ; d < n ; d++ )
            scanf("%d", &first[c][d]);
    if ( n != p )
        printf("Matrices with entered orders can't be multiplied with each other.\n");
    else {
        printf("Enter the elements of second matrix\n");
        for ( c = 0 ; c < p ; c++ ){
            for ( d = 0 ; d < q ; d++ )
                scanf("%d", &second[c][d]);
        }
        for(g=0;g<5;g++){
            for ( c = start ; c < end ; c++ ) {
                for ( d = 0 ; d < q ; d++ ) {
                    for ( k = 0 ; k < p ; k++ ) {
                        sum = sum + first[c][k]*second[k][d];
                    }
                    multiply[c][d] = sum;
                    sum = 0;
                }
            }
            start=start+m/5+1;
            end=end+m/5;
        }

        printf("Product of entered matrices:-\n");
        for ( c = 0 ; c < m ; c++ ) {
            for ( d = 0 ; d < q ; d++ )
                printf("%d\t", multiply[c][d]);
            printf("\n");
        }
        for ( i = 0; i < p; i++)
            free(second[i]);
        free(second);
        for ( i = 0; i < m; i++)
            free(multiply[i]);
        free(multiply);
    } 
    for ( i = 0; i < m; i++)
        free(first[i]);
    free(first);
    return 0;
}
4

1 に答える 1

0

開始インデックスは通常(常に0読んでください) ですが、終了インデックスについては、

行列を割り当て/構築するときは、それらを変数rows/に保存する必要があります。cols

于 2013-03-19T18:49:36.620 に答える