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
CodePudding user response:
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.
CodePudding user response:
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( "]%c", a[r][c], " \n"[c] );
puts( "" );
}
int showPair( int a, int b, int pos ) {
printf( "-x%-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 );
}