Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 随机数大于1000的矩阵乘法_C++_Matrix_Matrix Multiplication - Fatal编程技术网

C++ 随机数大于1000的矩阵乘法

C++ 随机数大于1000的矩阵乘法,c++,matrix,matrix-multiplication,C++,Matrix,Matrix Multiplication,我试图得到两个随机生成的数字矩阵相乘,但我不断得到分割错误和idk如何正确分配内存 这方面的任何帮助都会很好 //这是密码 #include<iostream> #include<iomanip> #include<stdlib.h> #include<stdio.h> #include<ctime> using namespace std; int main() { srand (1023); int a[1000][1000],

我试图得到两个随机生成的数字矩阵相乘,但我不断得到分割错误和idk如何正确分配内存

这方面的任何帮助都会很好

//这是密码

#include<iostream>
#include<iomanip>
#include<stdlib.h>
#include<stdio.h>
#include<ctime>

using namespace std;
int main()
{ 
srand (1023);
int a[1000][1000], b[1000][1000], mult[1000][1000], r1, c1, r2, c2, i, j, k;
cout << "Enter rows and columns for first matrix: ";
cin >> r1 >> c1;
cout << "Enter rows and columns for second matrix: ";
cin >> r2 >> c2;

while (c1!=r2)
{
   cout << "Error! column of first matrix not equal to row of second.";

}

cout << endl << endl;
for(i=0; i<r1; ++i)
for(j=0; j<c1; ++j)
{
    cout<<"mult["<<i<<"]["<<j<<"]: "<<rand()%9+1<<" "; //random matrix 1
}

cout << endl<< endl;
for(i=0; i<r2; ++i)
for(j=0; j<c2; ++j)
{
    cout<<"mult["<<i<<"]["<<j<<"]: "<<rand()%9+1<<" "; //random matrix 2
}

for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
   mult[i][j]=rand()%9+1;
}


for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
    mult[i][j]+=a[i][k]*b[k][j]; //matrix multiplication
}

cout << endl << "Output Matrix: " << endl;
clock_t begin = clock();
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
    cout << " " << mult[i][j];
    if(j==c2)
        cout << endl;
clock_t end = clock();
    double elapsed_secs = double(end-begin)*100;
    cout<<"Elapsed Time: "<<elapsed_secs<<" milliseconds\n";
return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{ 
srand(1023);
int a[1000][1000]、b[1000][1000]、mult[1000][1000]、r1、c1、r2、c2、i、j、k;
cout>r1>>c1;
cout>r2>>c2;
而(c1!=r2)
{

cout您试图在当前堆栈中创建超过11MB的数据

数组[1000][1000]=4*1000*1000字节=4000000

仅为3个阵列,您就总共创建了12000000字节

尝试减小数组的大小(或)获取矩阵的大小,并尝试使用new在堆中创建数组

检查堆栈大小,它应该是8MB

ulimit -a | grep stack
这就是堆的工作方式,因为您需要1000到5000

   int** a = new int*[r1];
   for(int i = 0; i < r1; ++i)
   {
      a[i] = new int[c1];
   }

   a[0][0] = 10;

   std::cout << a[0][0] << std::endl;

   // Delete all the columns  
   for(int i = 0; i < r1; ++i)
     delete[] a[i];

   delete []a ;                     
int**a=新的int*[r1];
对于(int i=0;istd::不能使用
int
来存储大量数字。你根本没有初始化数组
a
b
的元素。访问它们的元素,即使使用有效索引,也会产生未定义的行为。那么你如何初始化它们?我需要数组在1000到5000之间,然后你可以在堆中创建。使用新的d delete[]操作符在heapive中创建和删除数组我尝试过这样做,我们有a[]]、b[]]、mult[]],但它不起作用way@nathan编辑了关于如何在堆中创建二维数组的示例答案