Example #1
0
/**
 * NAME: regular_mult
 * INPUT: MATRIX* m1, MATRIX* m2, MATRIX* res
 * USAGE: Multiplies m1 and m2 naively and stores the result in res.
 *
 * NOTES: pointers for m1, m2, m3 must all be malloced before using this function.
 */
void regular_mult(MATRIX* m1, MATRIX* m2, MATRIX* res)
{
    // Checks to see whether m1 and m2 can be multiplied.
    if (m1->numCols != m2->numRows)
    {
        printf("Error: Matrices cannot be multiplied");
        return;
    }

    // Define numRows and numCols of res
    int rowSize = m1->numRows;
    int colSize = m2->numCols;

    // Fill in matrix information
    res->numRows = rowSize;
    res->numCols = colSize;

    // Allocate memory for the rows of the matrix.
    res->matrix = (BIGNUM**) malloc(rowSize * sizeof(BIGNUM*));
    for(int i = 0; i < rowSize; i++)
    {
        // Allocate memory for each entry in the matrix.
        res->matrix[i] = (BIGNUM*) malloc(colSize * sizeof(BIGNUM));

        // Go across the columns of m2
        for (int j=0; j< colSize; j++)
        {
            // Initialize a temporary bignum to store a sum.
            BIGNUM* sum = malloc(sizeof(BIGNUM));
            bignum_from_int(0,sum);

            // Go down the rows of m1 and across the columns of m2.
            // Naive multiplication is performed here.
            for (int k = 0; k < m1->numCols; k++)
            {
                // Initialize a temporary bignum to store the result of multiplication.
                BIGNUM* multRes = malloc(sizeof(BIGNUM));
                bignum_from_int(0,multRes);
                mult_bignums(&m1->matrix[i][k], &m2->matrix[k][j], multRes);

                // Initialize a temporary bignum for addition.
                BIGNUM* addRes = malloc(sizeof(BIGNUM));
                bignum_from_int(0,addRes);
                add_bignums(multRes, sum, addRes);

                // Store the addition and free varibles.
                *sum = *addRes;
                free(multRes);
                free(addRes);
            }

            // Store the sum into the matrix and free temp bignum.
            res->matrix[i][j] = *sum;
            free(sum);
        }
    }
}
Example #2
0
int main() {
    char *in_string;
    bignum *sum = make_bignum_with_length( 0 );

    while( scanf( "%s", in_string ) != EOF ) {
        sum = add_bignums( make_bignum( in_string ), sum );
    }

    print_bignum( sum );
}