With a 3 x 4 integer matrix, using two-dimensional integer array, said the sample is as follows:
Int a [3] [4]={{30,25,23,56}, {12,2,6,46}, {- 12-34,16,28}};
1) for programmable output the value of one of the biggest value of the elements, as well as its place of line and column number, calculate
Method is as follows: put a [0] [0] Max values assigned to variables, and then through the two layers of circulation, a traverse the entire
Array element for each access to the element comparing with Max, if its value is greater than the Max,
The value stored in Max, and record the rows and columns of the element value, repeat the process until the
The last element, the
2) the above algorithm in dealing with multiple maximum array, only accessible output traversed the first largest
Values and their locations, such as for array as follows:
Int a [3] [4]={{27,12,23,56}, {37,2,56,46}, {- 12-34,56,8}};
The output is as follows:
Max=56, row=0, the column=3
Please amend 1) algorithm, and programming requirement still cycle through the two layer (a traverse all elements), looking for
Maximum out all elements and their locations, the final output them, still in the array, for example, lose
Out as follows:
Max=56, row=0, the column=3
Max=56, row=1, the column=2
Max=56, row=2, the column=2
The code is as follows:
#include
using namespace std;
Int main ()
{
Int max1, I, j, num=0, k;
Int a [3] [4]={{27,12,23,56}, {37,2,56,46}, {- 12-34,56,8}};
Int the locate [12] [2];
Max1=a [0] [0];
for(i=0; i<=3; I++)
{
for(j=0; j<=4; J++)
{
If (a [I] [j]==max1)
{
Locate [num] [0]=I;
Locate [num] [1]=j;
num++;
}
Else if (a [I] [j] & gt; Max1)
{
Num=0;
Max1=a, [I] [j].
Locate [num] [0]=I;
Locate [num] [1]=j;
}
}
}
for(k=0; K
cout<& lt;" Max="& lt; & lt; Max1 & lt; & lt;" , roll="& lt; & lt; Locate [k] [0] <& lt;" The column="& lt; & lt; Locate [k] [1] <& lt; endl;
}
return 0;
}
Locate numerical overflow of great should have?
CodePudding user response:
On the basis of your code changes, the basic implementation requirements
#include
using namespace std;
Int main ()
{
Int a [3] [4]={{27,12,23,56}, {37,2,56,46}, {- 12-34,56,8}};
Int Max=a [0] [0], I=0, j=0, num=0, temp=a [0] [0].
Int b [12] [2]={0};
For (I=0; I & lt; 3; I++)
{
For (j=0; j <4. J++)
{
If (Max & lt;=a [I] [j])
{
Max=a, [I] [j].
B [num] [0]=I;
B [num] [1]=j;
num++;
}
}
}
For (I=0; I & lt; Num. I++)
{
If (a [I] [0] [b] [I] [1] [b] & gt; Temp)
{
Cout & lt; & lt; "Max=" & lt; & lt; Max & lt; & lt; "Roll="
& lt; & lt; B [I] [0] <& lt; "The column=" & lt; & lt; B [I] [1] <& lt; endl;
}
}
system("pause");
return 0;
}
CodePudding user response:
Do think the locate array may be very big and overflow is? You can consider to use such as container to store, or create a linked list, when the output can be in accordance with the format prescribed by the you parse it out, so you don't need to pre-book the array size, also won't appear overflow,