Returns the and of the largest sub-matrices in a two-dimensional loop array

Source: Internet
Author: User

Design ideas

And one-dimensional loop array thinking similar, loop n times, each time the number of the first column to the last column, a new two-dimensional array of the same specification to hold the new matrix, the previous write to find two-dimensional array of the largest sub-matrix of the algorithm slightly modified, add a loop to meet the requirements.

SOURCE program code

#include <iostream>

#include <cstdlib>

#include <ctime>

using namespace Std;

#define M 4

#define N 4

void Main ()

{

int a[m][n],aa[m][n],i,j,b,c,z;

cout<< "Please enter a range of values:" <<endl;

cin>>b>>c;

cout<< "Generate a two-dimensional array:" <<endl;

Srand (Unsigned (time (0)));

for (i=0;i<m;i++)

{

for (j=0;j<n;j++)

{

A[i][j]=rand ()% (c-b+1) +b;

cout<<a[i][j]<< "";

}

cout<<endl;

}

int sum,s[100],k=0,o=-1000,p=-1000,q=-1000;

for (j=0;j<n;j++)

{

s[j]=0;

}

for (int lun=0;lun<n;lun++)

{

for (int lunm=0;lunm<m;lunm++)

{

for (int lunn=0;lunn<n;lunn++)

{

if (lunn+lun<n)

{

Aa[lunm][lunn]=a[lunm][lunn+lun];

}

Else

AA[LUNM][LUNN]=A[LUNM][LUNN+LUN-N];

}

}

for (int ii=0;ii<m;ii++)

{

while (K+II<M)

{

for (j=0;j<n;j++)

{

S[J]=S[J]+AA[K+II][J];

}

for (i=0;i<n;i++)

{

for (j=i;j<n;j++)

{

sum=0;

for (z=i;z<=j;z++)

{

SUM=SUM+S[Z];

}

if (Sum>o) o=sum;

}

if (o>p)

{

P=o;

}

}

k++;

}

if (P>Q)

{

Q=p;

}

k=0;

for (j=0;j<n;j++)

{

s[j]=0;

}

}

}

cout<< "sub-matrix Maximum value:" <<q<<endl;

}

Run results

Programming summary

It can save a lot of time to modify the code based on the original code, so you should be able to read the code and modify the code skillfully.

Returns the and of the largest sub-matrices in a two-dimensional loop array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.