-3

很抱歉让我的作业感到厌烦。我正在使用 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();
} 
}  
} 
} 
} 
} 
}
4

2 回答 2

1

我给你一个简短的“TODO”,你应该在阅读一些讲义后完成它(它肯定会涵盖做作业所需的所有内容)。

所以这是你的总体规划:

  • 获得有关计算机上随机数的基本概述random.org 上的 Randomness 然后您将了解,为什么随机数根本不是随机的,以及如何处理它们。
  • 你需要理解函数的概念才能继续,所以再读一遍你的讲义,或者买一本关于编程基础的书。功能/Wikipedia.org
  • 编写一个函数,该函数返回一个包含随机条目的矩阵
  • 编写一个函数来计算给定矩阵的“总和”
  • 把所有东西放在一起……</li>

最后但并非最不重要:

  • 做你的练习,自己做作业,阅读讲义。没有其他方法可以学习编码。
  • 几周后回来,再次查看您的代码。
于 2013-04-21T21:50:51.817 回答
0

有时您可以尝试使用循环,它们确实很有帮助。你的代码太长太复杂了。这是工作程序(但请不要只是复制粘贴它并尝试理解每一行代码,这是为了你好):

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;

struct rndArr
{
    int x[4][4];
    int sum;
};

int main()
{
    int i, j, z, maxSum, maxZ;
    srand(time(0));
    struct rndArr st[3];

    for(z=0;z<3;z++) //looping through matrix
    {
        for(i=0;i<4;i++) //looping through row of each matrix
        {
            for(j=0;j<4;j++) //looping through column of each matrix
            {
                st[z].x[i][j]=rand() % 100; //initializing matrix with random numbers
                //calculating sum of each matrix
                if(i==0 && j==0) st[z].sum = st[z].x[0][0]; 
                else st[z].sum += st[z].x[i][j];
            }
        }
    }

    //printing matrix and its sum
    for(z=0;z<3;z++)
    {
        cout << z+1 << ". matrix:" << endl;
        for(i=0;i<4;i++)
        {
            for(j=0;j<4;j++)
            {
                cout << st[z].x[i][j] << "  ";
            }
            cout << endl;
        }
        cout << "sum = " << st[z].sum;
        cout << endl << endl;
    }
    cout << "\n\n\n---------\n\n\n";

    // finding matrix with max sum
    maxSum = st[0].sum;
    maxZ = 0;
    for(z=1;z<3;z++)
    {
        if(st[z].sum > maxSum)
        {
            maxSum = st[z].sum;
            maxZ = z;
        }
    }

    //printing matrix with largest sum
    cout << "matrix with largest sum is: " << maxZ+1 << " matrix" << endl;
    for(i=0;i<4;i++)
    {
        for(j=0;j<4;j++)
        {
            cout << st[maxZ].x[i][j] << "  ";
        }
        cout << endl;
    }
}
于 2013-04-21T21:58:51.970 回答