The maximum value of a two-dimensional array

Source: Internet
Author: User

I. Title:

Returns the and of the largest subarray in a two-dimensional integer array. Requirements: Enter a two-dimensional shape array with positive and negative numbers in the array. A contiguous sub-matrix in a two-dimensional array consists of a sub-array, each of which has a and. The maximum value for the and of all sub-arrays. Requires a time complexity of O (n).

Two: Pair programming requirements:

The pair completes the programming task. One person is mainly responsible for program analysis, code programming. One person is responsible for code review and Code test plan. Publish a blog post about the process, experience, and how to resolve conflicts between two people (attach a work photo of the development).

Three. Code:

#include <iostream>
#include <cstring>
#include <vector>
#include <cstdio>
#define Ndebug
#include <assert.h>
#define INF-9999
#define N 100
What is the difference between a macro definition and setting a global variable?
const int N = 500;
const int INF =-9999;
using namespace Std;

int Maxsubarray (int a[], int n)
{
ASSERT (A!=null && n>0);
int cur = 0;
int max = INF;

for (int i=0; i<n; i++)
{
Cur +=a[i];
if (cur < 0)
{
cur = 0;
}

if (cur > Max)
{
max = cur;
}
}
return max;
}
int Findmaxsubmatrix (int a[][n], int N)//? Why this is A[][n]
{
int tmpsum[n];
int max = INF;

//Enumerate possible combinations of all rows
for (int i=0; i<n; i++)
{
//will tmpsum clear 0
memset (tmpsum, 0, sizeof (tmpsum));
for (int j=i; j<n; j + +)

//plus elements of the current line
for (int k=0; k<n; k++)
{
Tmpsum[k] + = A[j][k];
}
int tmpmax = Maxsubarray (Tmpsum, N);
if (Tmpmax >max)
{
Max=tmpmax;
}
}
}
return max;
}

int main ()
{
int a[n][n];
int n; The size of the array
cout<< "Please enter the size of n in the array n*n:" <<endl;
while (Cin>>n && N)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j + +)
{
int K=rand ();
a[i][j]= k%2==0 rand ()%100+1: (-rand ()%100+1);
}
}
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j + +)
{
cout<<a[i][j]<< "";
}
cout<<endl;
}
cout<< "The number of the largest sub-arrays is:" <<findmaxsubmatrix (A, N) <<endl;
}

return 0;
}

Four. Thought:

The two functions involved in a program:

1.assert (A!=null && n>0);

The prototype of an Assert macro is defined in <assert.h>, and its function is to terminate the execution of the program if its condition returns an error, and the prototype defines:
#include <assert.h>
void assert (int expression);
The function of an assert is to evaluate expression expressions, if the value is False (that is, 0), then it prints an error message to stderr and then terminates the program by calling abort.
The disadvantage of using assert () is that frequent calls can greatly affect the performance of the program and add additional overhead. After debugging, you can disable the Assert call by inserting the #define NDEBUG before the statement that contains # include <assert.h>, as shown in the following example code:
#include <stdio.h>
#define Ndebug
#include <assert.h>
2.
tmpsum 0 memset (tmpsum, 0, sizeof (tmpsum));

Required header Files
C for <memory.h> or <string.h>
<cstring> in C + +
void * memset (void * ptr, int value, size_t num);
Assign value to num bytes starting with the address PTR, note that each byte in the NUM byte that starts with PTR is assigned value as value.
(1) If PTR points to a char address, value can be any of the character values;
(2) If PTR points to a non-char type, such as an int address, to assign the correct value, value can only be 1 or 0, because 1 and 0 are converted to binary after each bit is the same, set int is 4 bytes, then -1=0xffffffff, 0=0x00000000.
Ideas:

The time complexity of the program is O (n^3). Findmaxsubmatrix () Find the sum of the largest subarray, I control the line, it is a line down to find the largest sub-array, for example, when I equals 2 o'clock, the number in the same column is equivalent to one-dimensional array of a number, each to I plus 1, call Maxsubarray (int a[], int n) Find out the sum of the largest subarray of this one-dimensional array and compare it to Maxnum to see which is larger.

Flaw: The time complexity is still very large, and this largest sub-array also did not think how to output

4. Test:

When n=4

When n=50

The maximum value of a two-dimensional 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.