如何计算矩阵和并进行比较

时间:2013-04-21 21:29:21

标签: c++ matrix sum

抱歉我的作业很烦人。 我正在研究一个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();
} 
}  
} 
} 
} 
} 
}

2 个答案:

答案 0 :(得分:1)

我给你一个简短的“TODO”,你应该在之后>阅读一些讲义(这肯定会涵盖完成作业所需的一切)。

所以这是你的总体规划:

  • 获取有关计算机上随机数的基本概述Randomness on random.org 然后你会明白,为什么随机数根本不是随机数,以及如何处理它们。
  • 您需要了解要继续执行的功能的概念,因此请再次阅读您的讲义,或者阅读有关编程基础知识的书籍。 Functions / Wikipedia.org
  • 编写一个返回带随机条目的矩阵的函数
  • 编写一个函数来计算给定矩阵的“和”
  • 把所有东西放在一起......

最后但并非最不重要:

  • 做练习,自己做作业并阅读讲义。没有其他方法可以学习编码。
  • 几周后再来看看你的代码。

答案 1 :(得分: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;
    }
}