;

;;;
Last edited on
closed account (zb0S216C)
Have you tried an efficient solution?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
    int Array2D[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};

    int *Pos(Array2D[0]);
    int *End((Array2D[0] + (3 * 3)) - 1);

    int Max(*Pos);

    for(; Pos <= End; ++Pos)
        if(Max < *Pos) Max = *Pos;

    std::cout << Max;

    return(0);
}

Pos points to the beginning of Array2D. End is used to point to the end of Array2D. The loops iterates through Array2D. With each cycle of the loop, if Max's value is less than the integer pointed-to by Pos, Max is given that value.

Wazzak
;
Last edited on
closed account (zb0S216C)
Oh, right, sorry. Let's step back a bit.

I, personally, think your approach is somewhat overkill. Since you need to find the highest & lowest value of the array, I suggest that you break highlo() into 2 separate functions: SeekHighest() & SeekLowest().

Since FactoryData is global (I recommend making it local to main()), you don't need to pass it the the function(s).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int SeekLowest();
int SeekHighest();

int main()
{
    // Print the highest value...
    // Print the lowest value...
}

int SeekLowest()
{
    //...
}

int SeekHighest()
{
    int Max(0); // Functional notation.
    for(int Outer(0); Outer < 6; ++Outer)
        for(int Inner(0); Inner < 4; ++Inner)
            if(Max < FactoryData[Outer][Inner]) Max = FactoryData[Outer][Inner];

    return(Max);
}

Compare this to yours. It's far more readable and straight forward. Remember that a function should do only 1 task - not multiple.

Wazzak
Topic archived. No new replies allowed.