很抱歉让我的作业感到厌烦。我正在使用 C++ 编写一个程序,该程序创建 3 个随机矩阵并计算每个矩阵的总和并打印出总和最大的一个。到目前为止,我遇到了两个问题。
-我的矩阵 2 和矩阵 3 是相同的,原因不明
- 我的矩阵的总和不正确,矩阵总和 2 与矩阵总和 3 不同,尽管它们是相同的矩阵。
到目前为止,这是我想出的:
#include <iostream>
#include <algorithm>
#include <ctime>
using namespace std;
int main()
{
int a[4][4];
{ // matrix 1
int Suvaline1 = rand() % 100 ;
int Suvaline2 = rand() % 100 ;
int Suvaline3 = rand() % 100 ;
int Suvaline4 = rand() % 100 ;
int Suvaline5 = rand() % 100 ;
int Suvaline6 = rand() % 100 ;
int Suvaline7 = rand() % 100 ;
int Suvaline8 = rand() % 100 ;
int Suvaline9 = rand() % 100 ;
int Suvaline10 = rand() % 100 ;
int Suvaline11 = rand() % 100;
int Suvaline12 = rand() % 100 ;
int Suvaline13 = rand() % 100 ;
int Suvaline14 = rand() % 100 ;
int Suvaline15 = rand() % 100;
int Suvaline16 = rand() % 100 ;
int a[4][4] = {Suvaline1,Suvaline2,Suvaline3,Suvaline4,Suvaline5,Suvaline6,Suvaline7,Suvaline8,Suvaline9,Suvaline10,Suvaline11,Suvaline12,Suvaline13,Suvaline14,Suvaline15,Suvaline16};
srand(time(0));
std::random_shuffle(&a[0][0],&a[0][0]);
// print matrix 1
for (int i=0; i<4; ++i)
{
for (int j=0; j<4; ++j)
{
std::cout << a[i][j] << "\t";
}
std::cout << "\n";
}
{ // calculate matrix 1 sum
int i,j;
int sumrow11;
sumrow11=0;
for(j=0;j<2;j++)
sumrow11=sumrow11+a[i][j];
int sumrow12;
sumrow12=0;
for(j=1;j<3;j++)
sumrow12=sumrow12+a[i][j];
int sumrow13;
sumrow13=0;
for(j=2;j<4;j++)
sumrow13=sumrow13+a[i][j];
int sumrow14;
sumrow14=0;
for(j=3;j<5;j++)
sumrow14=sumrow14+a[i][j];
int sum1;
sum1==sumrow11+sumrow12+sumrow13+sumrow14;// print matrix 1 sum
cout<<"\n Esimese maatriksi summa "<<sum1;
{ // matrix 2
int Suvaline1 = rand() % 100 ;
int Suvaline2 = rand() % 100 ;
int Suvaline3 = rand() % 100 ;
int Suvaline4 = rand() % 100 ;
int Suvaline5 = rand() % 100 ;
int Suvaline6 = rand() % 100 ;
int Suvaline7 = rand() % 100 ;
int Suvaline8 = rand() % 100 ;
int Suvaline9 = rand() % 100 ;
int Suvaline10 = rand() % 100 ;
int Suvaline11 = rand() % 100;
int Suvaline12 = rand() % 100 ;
int Suvaline13 = rand() % 100 ;
int Suvaline14 = rand() % 100 ;
int Suvaline15 = rand() % 100;
int Suvaline16 = rand() % 100 ;
int a[4][4] = {Suvaline1,Suvaline2,Suvaline3,Suvaline4,Suvaline5,Suvaline6,Suvaline7,Suvaline8,Suvaline9,Suvaline10,Suvaline11,Suvaline12,Suvaline13,Suvaline14,Suvaline15,Suvaline16};
srand(time(0));
std::random_shuffle(&a[0][0],&a[0][0]);
// print matrix 2
cout << "\n---------------------------------------------------------" << endl;
for (int i=0; i<4; ++i)
{
for (int j=0; j<4; ++j)
{
std::cout << a[i][j] << "\t";
}
std::cout << "\n";
}
{ // calculate matrix 2 sum
int i,j;
int sumrow21;
sumrow21=0;
for(j=0;j<2;j++)
sumrow21=sumrow21+a[i][j];
int sumrow22;
sumrow22=0;
for(j=1;j<3;j++)
sumrow22=sumrow22+a[i][j];
int sumrow23;
sumrow23=0;
for(j=2;j<4;j++)
sumrow23=sumrow23+a[i][j];
int sumrow24;
sumrow24=0;
for(j=3;j<5;j++)
sumrow24=sumrow24+a[i][j];
int sum2;
sum2==sumrow21+sumrow22+sumrow23+sumrow24;
cout<<"\n Teise maatriksi summa "<<sum2;// print matrix 2 sum
{ // matrix 3
int Suvaline1 = rand() % 100 ;
int Suvaline2 = rand() % 100 ;
int Suvaline3 = rand() % 100 ;
int Suvaline4 = rand() % 100 ;
int Suvaline5 = rand() % 100 ;
int Suvaline6 = rand() % 100 ;
int Suvaline7 = rand() % 100 ;
int Suvaline8 = rand() % 100 ;
int Suvaline9 = rand() % 100 ;
int Suvaline10 = rand() % 100 ;
int Suvaline11 = rand() % 100;
int Suvaline12 = rand() % 100 ;
int Suvaline13 = rand() % 100 ;
int Suvaline14 = rand() % 100 ;
int Suvaline15 = rand() % 100;
int Suvaline16 = rand() % 100 ;
int a[4][4] = {Suvaline1,Suvaline2,Suvaline3,Suvaline4,Suvaline5,Suvaline6,Suvaline7,Suvaline8,Suvaline9,Suvaline10,Suvaline11,Suvaline12,Suvaline13,Suvaline14,Suvaline15,Suvaline16};
srand(time(0));
std::random_shuffle(&a[0][0],&a[0][0]);
// print matrix 3
cout << "\n---------------------------------------------------------" << endl;
for (int i=0; i<4; ++i)
{
for (int j=0; j<4; ++j)
{
std::cout << a[i][j] << "\t";
}
std::cout << "\n";
}
{ // calculate matrix 3 sum
int i,j;
int sumrow31;
sumrow31=0;
for(j=0;j<2;j++)
sumrow31=sumrow31+a[i][j];
int sumrow32;
sumrow32=0;
for(j=1;j<3;j++)
sumrow32=sumrow32+a[i][j];
int sumrow33;
sumrow33=0;
for(j=2;j<4;j++)
sumrow33=sumrow33+a[i][j];
int sumrow34;
sumrow34=0;
for(j=3;j<5;j++)
sumrow34=sumrow34+a[i][j];
int sum3;
sum3==sumrow31+sumrow32+sumrow33+sumrow34;
cout<<"\n Kolmanda maatriksi summa "<<sum3; // print matrix 3 sum
getchar();
getchar();
}
}
}
}
}
}
}