mardi 10 mars 2020

2D x 2D when the rows and columns are set by a user, random

My task was to write a program that multiplies two matrices. The program should retrieve information from the user how many rows and columns he has the first of the matrix and how much the second of the matrix (by which we will multiply). Then enter the range of numbers for the first and second matrices. The tables are filled with random numbers from the given ranges. Then we do the multiplication matrix. We display matrices filled with random numbers and the resulting matrix. So I did it, but the answer is not right. Can anybody help me out with this stuff ? Many thanks in advance...

#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;
int main()
{
  srand(time(NULL));
  cout << "Provide first matrix dimensions (rows, cols):\n";
  int TWier1, TKol1, za;
  cin >> TWier1 >> TKol1;
  int taba[TWier1][TKol1];
  cout << "Provide matrix max value:\n";
  cin >> za;
  for(int i=0;i<TWier1;i++)
  {
    for(int j=0;j<TKol1;j++)
    {
      taba[i][j] = rand() % za;
    }
  }
  for(int i=0;i<TWier1;i++)
  {
    for(int j=0;j<TKol1;j++)
    {
      cout << taba[i][j] << " ";
    }
    cout << "\n";
  }
  cout << "Provide second matrix dimensions (rows, cols):\n";
  int TWier2, TKol2;
  cin >> TWier2 >> TKol2;
  int tabb[TWier2][TKol2];
  for(int i=0;i<TWier2;i++)
  {
    for(int j=0;j<TKol2;j++)
    {
      taba[i][j] = rand() % za;
    }
  }
  for(int i=0;i<TWier2;i++)
  {
    for(int j=0;j<TKol2;j++)
    {
      cout << tabb[i][j] << " ";
    }
    cout << "\n";
  }
  int tabc[TWier1][TKol2];
  for(int i=0;i<TWier1;i++)
  {
    for(int j=0;j<TKol2;j++)
    {
      tabc[i][j]=0;
    }
  }
  for(int i=0;i<TWier1;i++)
  {
    for(int j=0;j<TKol2;j++)
    {
      for(int k=0;k<TWier1;k++)
      {
        tabc[i][j] += taba[k][j]*tabb[i][k];
      }
    }
  }
  for(int i=0;i<TWier1;i++)
  {
    for(int j=0;j<TKol2;j++)
    {
      cout << tabc[i][j] << " ";
    }
    cout << "\n";
  }
  system("pause");
  return 0;
}



Aucun commentaire:

Enregistrer un commentaire