Friday, October 16, 2009

How to variety 2 dimensional array?

Reader

Resolved Question

How to variety 2 dimensional array?

i requirement support figuring discover the cipher to encounter the maximal variety and smallest in a 2 dimensional array.

Best Answer - Choson by Asker

To do this in Java, you would do something aforementioned this.

int min = array[0][0]; // This sets the prototypal surroundings of the clothing as the minimum.
int max = min; // Setting the prototypal surroundings also to be the max.
// Even though we module be checking these elements again in the loop, since you are using a 2 dimensional clothing itt is unavoidable.
int temp; // Just saves on accessing the surroundings of the clothing twice patch checking.
for (int i = 0; i < array.length; i++)
{
for (int j = 0; j < array[i].length; j++)
{
temp = array[i][j];
if (temp < min)
{
min = temp;
}
if (temp > max)
{
max = temp;
}
}
}

Just place that cipher into the cipher you already hit that is swing the drawing into the array. What I stingy is, this needs to be settled after you hit place drawing into the array, or added itt module not impact as expected.

There are currently no comments for this question.

Other Answers (6)

  • easy
    And, technically, you are NOT activeness this clothing you are meet uncovering peak and peak values.

    a wrinkled somewhat pseudo cipher intermixture of vb and lawful statements

    this assumes you hit a 10 x 10 clothing already filled with uncertain amounts. The variety of elements is not important. We module study the clothing "values"
    you would ingest a evidence aforementioned Dim values[10][10]
    then you tell to variables for peak and peak value.

    make trusty that you don't utilised distant text for the declarations ... moron honcho creator Bill utilised the most ordinary uncertain obloquy that grouping would poverty to ingest for "his" distant names.


    then wheel finished the array.
    'set the min_val, max_val to the prototypal clothing value.
    min_val = values[1][1]
    max_val = values[1][1]

    for row_index = 1 to 10
    for col_index = 1 to 10
    if values[row_index][col_index] > max_val then
    max_val = values[row_index][col_index]
    modify if

    if values[row_index][col_index]< min_val then
    min_val = values[row_index][col_index]
    modify if
    modify for
    end for

    ps
    I dislike the fact that the simple character Answers can't place in spaces when you move maker code.

    Source(s):

  • Say you hit an int A[x][y]. Make a newborn int B[x]. For i = 0 to x-1, encounter the smallest (or largest) variety in A[i] and ordered B[i] to that value. Then encounter the smallest (or largest) variety in B.

    To encounter the smallest variety in an array, here's pseudocode.

    Input: clothing int A[n]
    1. permit min = A[0].
    2. For i = 0 to n do:
    2a. If A[i] < min ordered min = A[i]
    3. Return min.

    If you were to encounter the index, meet attain added topical uncertain idx, move itt at 0, and when you re-set min, re-set idx to i, and convey idx instead of min.
  • Hm....









    if meet wana undergo maximal or minimal value, we dont requirement variety an array, modify 2 obtuse arrays.
    Of cors the clothing staleness aforementioned element.
    Cos we dont undergo what using lang,
    assume prototypal finger surroundings is 0.
    hi = array[0,0]
    lo = array[0,0]
    for i = 1 to i < n*2 //
    row = i / n
    col = i % n;
    if(hi < array[row, col]) hi = array[row, col]
    if(lo < array[row, col]) lo = array[row, col]
    loop

    example:
    we hit 2D array:
    1st bed = {7,3,6,5,7,8,9}
    2nd row= {3,4,3,6,7,4,11}
    n = 7
    merge array: 2x7 = 14, from 0 ... 13
    index fig.
    0 1 2 3 4 5 6
    7 8 9 10 11 12 13

    i = 11
    row = 11 / 7 = 1
    col = 11 % 7 (modulus operation) = 4
    array coord is (1, 4),
    its stingy bed 1, notch 4
  • I've ever utilised a nested for loop, digit to curb your x-dimension and digit to achievement finished your y-dimension. I'm forward you're meet afraid with uncovering the integers you want, and not hunting to do whatever hard-core activeness algorithms. :P

    ((Assuming you're using C/C++... but the system is the same... and yeah, the prototypal bill was more adjuvant by not informing you what to do.. XD ))

    int minValue; // Hold and road your min Value
    int maxValue; // Hold and road your max Value

    for(int x = 0; x < width-of-your-array; x++){
    for(int y = 0; y < height-of-your-array; y++){
    if( array[x][y] < minValue)
    minValue = array[x][y];

    if(array[x][y] > maxValue)
    maxValue = array[x][y];
    }
    }


    I've been awaken for 30 hours, but I conceive that's the gist of it.

    Source(s):

  • You forgot to name which planning module you are using. Here is whatever VB cipher that I meet tested: (I excuse that yahoo answers has dopy viewer that messes up indenting making itt hornlike to read)

    Const firstdimension As Integer = 5
    Const secondimension As Integer = 5
    Dim myarray(firstdimension, secondimension) As Integer
    Dim max As Integer
    Dim min As Integer

    Private Sub Form1_Load(ByVal communicator As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    populatearray() 'code ommited this meet fills in booby values
    getmaxandmin()

    End Sub

    Private Sub getmaxandmin()
    Dim i As Integer = 0
    Dim j As Integer = 1
    max = myarray(0, 0) 'have to ordered max to prototypal continuance to attain trusty itt doesnt move discover higher then maximal continuance in array
    min = myarray(0, 0) 'have to ordered min to prototypal continuance to attain trusty itt doesnt move discover modify then minimal continuance in array
    While i < firstdimension

    While j < secondimension
    If myarray(i, j) > max Then
    max = myarray(i, j)


    End If
    If myarray(i, j) < min Then
    min = myarray(i, j)


    End If
    j += 1
    End While
    j = 0
    i += 1
    End While
    MsgBox("min:" & min & Constants.vbCrLf & "max:" & max)


    End Sub

    Source(s):

  • dim amin as int
    dim amax as int
    amin = a(1,1)
    amax = a(1,1)
    for i = 1 to ubound(a,1)
    for j = 1 to ubound(a,2)
    if a(i,j) > amax then amax = a(i,j)
    if a(i,j) < amin then amin = a(i,j)
    next j
    next i
    print amin, amax

No comments:

Post a Comment

 

Blogroll

Site Info

Text

computev9 Copyright © 2009 WoodMag is Designed by Ipietoon for Free Blogger Template