#matrix
Вопрос:
Итак, у меня было это задание, я должен был написать программу, которая выполняет умножение для двух матриц. Моя программа запрашивает у пользователя количество строк и столбцов для обеих матриц. Чтобы сгенерировать значения матриц, я использовал функцию rand, а затем показал сгенерированные матрицы.
#include <iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main()
{
int FA, CA,num; //number is to store the random number
cout<<"Enter the number of rows for MATRIX A "<<endl;
cin>>CA;
cout<<"Enter the number of columns for MATRIX A "<<endl;
cin>>FA;
int A[FA][CA];
int i, j;
for(i=0;i<FA;i )
{
for(j=0;j<CA;j )
{
num=rand(); //it generates a number ranging from 0 to 10
A[FA][CA] = num;
cout<<" "<<A[FA][CA];
}
cout<<endl;
}
int FB, CB;
cout<<"Enter the number of rows for MATRIX B "<<endl;
cin>>CB;
cout<<"Enter the number of rows for MATRIX B "<<endl;
cin>>FB;
int B[FB][CB];
for(i=0;i<FB;i )
{
for(j=0;j<CB;j )
{
num=rand();
A[FB][CB] = num;
cout<<" "<<A[FB][CB];
}
cout<<endl;
}
Пока все хорошо, далее внутри оператора if проверяется, можно ли выполнить умножение или нет.
Затем следует раздел кода, который, так сказать, выполняет фактическую операцию…
Я чувствую, что здесь что-то не так, но не могу этого найти.
Вот он:
if(FB == CA)
{
cout<<"Multiplication in progress"<<endl;
cout<<endl;
//Creating MATRIX AB
int AB[FA][CB];
int k;
cout<<"MATRIX AB, HAS: "<< FA << " ROWS AND "<< CB << " COLUMNS."<<endl;
//MULTIPLICATION PROCESS
// START
for(i=0;i<FA;i )
{
for(j=0;j<CB;j )
{
AB[i][j]=0;
for(k=0;k<CA;k )
{
AB[i][j] =A[i][k]*B[k][j];
}
}
cout<<endl;
} //finish
// SHOW AB
for (int i = 0; i < FA; i )
{
for (int j = 0; j < CB; j )
{
cout << AB[i][j] << " ";
}
// Newline for new row
cout << endl;
}
}
else
{
cout<<"Multiplication can't be done"<<endl;
}
return 0;
}
The issue is that the AB Matrix displays random numbers.
I can’t seem to find where the issue is.
This is what it displays:
[enter image description here][1]
[1]: https://i.stack.imgur.com/FhBN5.png
Sorry if the formating isn’t the best, it´s my first post and i´m new to programming. Thanks in advance.