Find maximum and minimum element in array

Hi there! I need to find the mimimum element in two-dimensional(4,4) array by row and maximum element by column and store them in another array (5,5).Maybe I did not explain properly.

That's how it should look new array (5,5):
1 2 3 4 min
1 2 3 4 min
1 2 3 4 min
m m m m 0

*m - max

So this is the first array:
1
2
3
4
5
6
7
8
int array[4][4];
int i, j;

for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++) {
cout << "\n array[" << i + 1 << "][" << j + 1 <<"]=";
cin >> array[i][j];
        }

With this I try to find the min of each row:
1
2
3
4
5
6
7
8
int min;
for (i = 0; i<4; i++)
	{
		min[i] = array[0][i];
		for (j = 1; j<4; j++)
			if (min[i]>array[i][j])
				min[i] = array[i][j];
	}

Where I'm wrong? My error list is full with "expression must have pointer-to-object type".

And with this i will try to make new array:
1
2
3
4
5
6
7
for(i = 0; i < 4; i++)
for(j = 0; j < 4; j++) 
{newarr[i][j]=array[i][j];
newarr[i][5]=max[i];
}
for(j = 0; j < 4; j++) 
newarr[4][j]=min[j];

Is this okay? There is no way to check it because I just can not find the min and max.
Last edited on
> "expression must have pointer-to-object type"
¿which line?

> newarr[i][5]=max[i];
if newarr is 5x5, then that's out of bounds.
newarr[i][4]=max[i];


Also, learn to indent.
Well that's the whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
using namespace std;
int main() {
	int A[4][4];
	int i, j;

	
	for (i = 0; i < 4; i++)
		for (j = 0; j < 4; j++) {
			cout << "\n A[" << i + 1 << "][" << j + 1 << "]=";
			cin >> A[i][j];
		}

	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++)
			cout << A[i][j] << "\t";

		cout << "\n";
	}
	{
		int min[4];
		for (i = 0; i < 4; i++) {
			min[i] = A[0][i];
			for (j = 1; j < 4; j++) {
				if (min[i] > A[i][j])
					min[i] = A[i][j];
			}
		}
		int newarr[5][5];
		int max[5] = { 1,2,3,4,5 };
		for (i = 0; i < 4; i++) {
			for (j = 0; j < 4; j++) {
				newarr[i][j] = A[i][j];
				newarr[i][5] = max[i];
			}
		}

		for (j = 0; j < 4; j++)
			newarr[5][j] = min[j];
		cout << newarr[5][j] << "\t";
		cout << "\n";

	}

}


I put random elements to max. Because so far I only test. But once I started my program it show correct only the first array. And where should be the new array it shows zero. Here it is the outcome of the debuging:

1
2
3
4
5
5   4   3   1   
5   6   7   9   
4   2   3   9   
4   8   4   6   
0   

Last edited on
This is a new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>

int main()
{
    // Initialised inline to simplify example.
    int A[5][5];

    // Only uses 4x4 of the 5x5 array meaning that you don't need to use two arrays.
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            std::cout << "Enter in value for row " << x << ", column " << y << ".\n";
            std::cin >>  A[x][y];
        }
    }

    std::cout << "Input:" << std::endl;
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            std::cout << A[x][y] << "\t";
        }
        std::cout << "\n";
    }

    // Finds the max down each column in the array
    for (int x = 0; x < 4; ++x)
    {
        for (int y = 0; y < 4; ++y)
        {
            if (A[x][4] < A[x][y])
                A[x][4] = A[x][y];
        }
    }

    // Finds the min across the array (along row) 
    for (int y = 0; y < 4; ++y)
    {
        for (int x = 0; x < 4; ++x)
        {
            // Assign the min to a value in that row.
            A[4][y] = A[1][y];
            if (A[4][y] > A[x][y])
                A[4][y] = A[x][y];
        }
    }

    std::cout << "Output:" << std::endl;
    for (int x = 0; x < 5; ++x)
    {
        for (int y = 0; y < 5; ++y)
        {
            std::cout << A[x][y] << "\t";
        }
        std::cout << "\n";
    }

    std::cin.get();
    std::cin.get();
    return 0;
}


The problem is that the output is not correct:
http://oi65.tinypic.com/n8llz.jpg

How to fix it?
Last edited on
Topic archived. No new replies allowed.