Random matrix multiplication

Viewed 69

I need to create at least 2 matrix 4x4, multiplicate them and display the result, but I'm getting this thing as as result img

I'm creating matrix a[i][j] and matrix b[k][l], and trying to pass the result to a new matrix called c[i][j] Besides that, I need to make 2 kinds of matrix multiplication, one like this, and another one like this

Can you please please please help? Code below

#include<iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;

int matriz1() {
    int a[4][4], i, j;
    for (i = 0; i < 4; ++i)
    {
        for (j = 0; j < 4; ++j)
        {
            a[i][j] = rand() % 100 + 1;
        }
    }

    for (i = 0; i < 4; ++i)
    {
        for (j = 0; j < 4; ++j)
            std::cout << a[i][j] << '\t';
        std::cout << '\n';

    }
    std::cout << '\n';
    std::cout << "x" << std::endl;
    std::cout << '\n';
    std::cout << "Matriz 2:" << std::endl;

    int b[4][4], k, l;
    for (k = 0; k < 4; ++k)
    {
        for (l = 0; l < 4; ++l)
        {
            b[k][l] = rand() % 100 + 1;
        }
    }

    for (k = 0; k < 4; ++k)
    {
        for (l = 0; l < 4; ++l)

            std::cout << b[k][l] << '\t';
        std::cout << '\n';
    }

    std::cout << '\n';
    int c[4][4], m, n, x;
   
 
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                for (k = 0; k < 4; k++) {
                    c[i][j] += a[i][k] * b[k][j];
                }
            }
        }
        cout << " RESULTADO!!!!!!!!!!!!!!!!!!!!!!" << endl;
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                cout << c[i][j] << "\t";
            }
            cout << "\n";
        }
        return 0;
    }


int main()
{

    srand(time(0));

    std::cout << "Matriz 1:" << std::endl;
    std::cout << matriz1() << std::endl;

}

SOLVED IN THE COMMENTS! Stop disliking my post its my first post

2 Answers

You do this:

c[i][j] += a[i][k] * b[k][j];

but you never initialized c array, it contains random values, likely something like 0xCDCDCDCD (-842150451). Initialize it like this:

int c[4][4] = {}

You have repeated code, so consider to break it up in functions, e.g. you can initialize matrices as functions and output one as another. THat would make code more readable and easier to find errors.

A common "Beginner's Problem" is writing too much code. One consequence is that there are too many places where bugs and other flaws can hide.

This is in 'C', but the only 'C++' aspect of your code is using cout for output. printf() can also be used with C++.

#include <stdio.h>
#include <stdlib.h>

void fill4x4( int a[][4] ) {
    for( int r = 0; r < 4; r++ )
        for( int c = 0; c < 4; c++ )
            a[r][c] = rand() % 100 + 1;
}

void show4x4( int a[][4] ) {
    for( int r = 0; r < 4; r++ )
        for( int c = 0; c < 4; c++ )
            printf( "%5d%c", a[r][c], "   \n"[c] );
     puts( "" );
}

int showPair( int a, int b, int pos ) {
    printf( "%2dx%-2d%c", a, b, "   \n"[pos] );
    return a * b;
}

void mult4x4( int a[][4], int b[][4], int d[][4], bool x ) {
    for( int r = 0; r < 4; r++ )
        for( int c = 0; c < 4; c++ )
            // assign (=), not accumulate (+=)
            // notice the 'exchange' of row/col access of 'b[][]'
            if( x )
                d[r][c] = showPair( a[r][c], b[r][c], c );
            else
                d[r][c] = showPair( a[r][c], b[c][r], c );
    puts( "" ); show4x4( d );
}

int main()
{
    srand(time(0));

    int a[4][4]; fill4x4( a ); show4x4( a );
    int b[4][4]; fill4x4( b ); show4x4( b );
    int d[4][4];
    mult4x4( a, b, d, true );
    mult4x4( a, b, d, false );

    return 0;
}

Copy, paste, compile and run this to see the (random) output.

EDIT: Pointed out by a question from the OP, here's a further compaction of one function that may-or-may-not be self-evident:

void mult4x4( int a[][4], int b[][4], int d[][4], bool x ) {
    for( int r = 0; r < 4; r++ )
        for( int c = 0; c < 4; c++ )
            d[r][c] = showPair( a[r][c], x?b[r][c]:b[c][r], c );
    puts( "" ); show4x4( d );
}

Further EDIT: A common problem for those with time to fill is mucking around, shrinking code that already works. Here's the result of some of that..

#include <stdio.h>
#include <stdlib.h>

char *fmt1 = "%5d%.2s";
char *fmt2 = "%2dx%-2d%.2s";
char *tail1 = "       ::";
char *tail2 = "       \n";

void show2x4x4( int a[][4], int b[][4] ) {
    for( int r = 0, c; r < 4; r++ ) {
        for( c = 0; c < 4; c++ ) printf( fmt1, a[r][c], &tail1[c+c] );
        for( c = 0; c < 4; c++ ) printf( fmt1, b[r][c], &tail2[c+c] );
    }
    puts( "" );
}

int main() {
    srand(time(0));

    int r, c, v, a[4][4], b[4][4], d[4][4], e[4][4], *px, *py;

    // fill 2 4x4 arrays of random ints as if both were 1x16
    for( px = &a[0][0], py = &b[0][0], v = 0; v < 4 * 4; v++ ) {
        int num = rand();
        *px++ = num       % 100; // two digits only
        *py++ = num / 100 % 100;
    }
    show2x4x4( a, b ); // display

    // show and perform the calc of the product matrices
    for( r = 0; r < 4; r++ ) {
        for( c = 0; c < 4; c++ ) {
            printf( fmt2, a[r][c], b[r][c], &tail1[c+c] );
            d[r][c] = a[r][c] * b[r][c];
        }
        for( c = 0; c < 4; c++ ) { // note b[] swaps col & row
            printf( fmt2, a[r][c], b[c][r], &tail2[c+c] );
            e[r][c] = a[r][c] * b[c][r];
        }
    }
    puts( "" );
    show2x4x4( d, e ); // show both products

    return 0;
}

Output

   29     66     71     20 :   35     88     22     22
   35     80     36     85 :   53      1     28     54
   12     14     12     71 :   95     98     92     19
   62     61     89     17 :   94     63     32     43

29x35  66x88  71x22  20x22 :29x35  66x53  71x95  20x94
35x53  80x1   36x28  85x54 :35x88  80x1   36x98  85x63
12x95  14x98  12x92  71x19 :12x22  14x28  12x92  71x32
62x94  61x63  89x32  17x43 :62x22  61x54  89x19  17x43

 1015   5808   1562    440 : 1015   3498   6745   1880
 1855     80   1008   4590 : 3080     80   3528   5355
 1140   1372   1104   1349 :  264    392   1104   2272
 5828   3843   2848    731 : 1364   3294   1691    731
Related