I need to know the place where is the highest value/lowest value in an array.

I need to know the place where is the highest value/lowest value in an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  for (i = 0; i <= n; i++){
		for (j = 0; j <= m; j++){
			if (a[i][j] > a[0][0]){
				a[0][0] = a[i][j];
				row = i;
				column = j;
			}
		}
	}
	cout << "Highest value is row " << row << " and column " << column;
	
	for (i = 0; i <= n; i++){
		for (j = 0; j <= m; j++){
			if (a[0][0] > a[i][j]){
				a[0][0] = a[i][j];
				row = i;
				column = j;
			}
		}
	}
	cout << "\nLowest value is row " << row << " and column " << column;

Sample Output:
1 2 3
4 5 6
7 8 9
Highest value is row 3 column 3
Lowest value is row 1 column 1

My output:
1 2 3
4 5 6
7 8 9
Highest value is row 3 column 3
Lowest value is row 0 column 1//this part is always fixed no matter the place is
the lowest goes. how to fix this? thank you
Last edited on
lines 4 and 15: Why are you ever changing the value of a[0][0] within these checks?

just because your check for the highest element gives the right answer, doesn't necessarily mean that the code is correct.

consider this algorithm for finding the lowest and highest in a single dimension array.

1
2
3
4
5
6
7
8
9
int lowest = 0;
int highest = 0;
for( int i = 0; i < numElements; i++ )
{
	if( a[i] < a[lowest] )
		lowest = i;
	if( a[i] > a[highest] )
		highest = i;
}
Last edited on
What you have said is the same in my code... and a[lowest] is the same as a[0].. it has the same logic in it and i tried what you did and it's still the same... :)
@kes: I'm not asking for the minimum and maximum and number but its place in an array :) thank you though
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
#include <iostream>

using namespace std;

int main()
{
	int a[3][3];
	int row = 0;
	int column = 0;


	int x = 1;
	for( int i = 0; i < 3; i++ )
	{
		for( int j = 0; j < 3; j++ )
		{
			a[i][j] = x;
			x++;
		}
	}


	for( int i = 0; i <= 2; i++ )
	{
		for( int j = 0; j <= 2; j++ )
		{
			if( a[i][j] > a[0][0] )
			{
				a[0][0] = a[i][j];
				row = i;
				column = j;
			}
		}
	}
	cout << "Highest value is row " << row << " and column " << column;

	row = 0;
	column = 0;

	for( int i = 0; i <= 2; i++ )
	{
		for( int j = 0; j <= 2; j++ )
		{
			if( a[0][0] > a[i][j] )
			{
				a[0][0] = a[i][j];
				row = i;
				column = j;
			}
		}
	}
	cout << "\nLowest value is row " << row << " and column " << column;
}


Had to bodge together some code to fill in what you didn't post, but my results are :

Highest value is row 2 and column 2
Lowest value is row 0 and column 1 


You're shuffling the data in your arrays with lines 4 and 15.

nowhere in the code I posted are any array elements written.
Last edited on
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
int main()
{
	int a[10][10], n, m, i, j, s, tr = 0, row, column;
	cout << "Enter order of matrix: ";
	cin >> n >> m;
	//input
	cout << "\n\nEnter " << n << " x " << m << " matrix\n\n";
	for (i = 1; i <= n; i++){
		for (j = 1; j <= m; j++)
			cin >> a[i][j];
	}
	//given matrix
	cout << " Given matrix: \t\t\t row sum \r\n";
	for (i = 1; i <= n; i++){
		s = 0;
		for (j = 1; j <= m; j++){
			cout << setw(10) << a[i][j];
			s += a[i][j];
		}
		cout << setw(15) << s;
		cout << "\n";
	}
	//col sum
	cout << "Col Sum:\n";
	for (j = 1; j <= n; j++)
	{
		s = 0;
		for (i = 1; i <= m; i++){
			s += a[i][j];
		}
		cout << setw(15) << s;
		cout << "\n";
	}
	//trace
	cout << "Trace:\n";
	for (i = 1; i <= m; i++){
		for (j = 1; j <= n; j++){
			if (i == j){
				tr += a[i][j];
			}
		}
	}
	cout << setw(15) << tr;
	if (m != n){
		cout << "\nNot a perfect matrix!\n";
	}
	{
		int flag;
		for (i = 0; i < n; i++)
		{
			for (j = 0; j < m; j++)
			{
				if (a[i][j] != 1 && a[j][i] != 0)
				{
					flag = 0;
					break;
				}
			}
		}
		if (flag == 1)
			cout << "\nIt is identity matrix \n";
		else
			cout << "\nIt is not an identity matrix \n";
	}
	for (i = 0; i <= n; i++){
		for (j = 0; j <= m; j++){
			if (a[i][j] > a[0][0]){
				a[0][0] = a[i][j];
				row = i;
				column = j;
			}
		}
	}
	cout << "Highest value is row " << row << " and column " << column;
	
	for (i = 0; i <= n; i++){
		for (j = 0; j <= m; j++){
			if (a[0][0] > a[i][j]){
				a[0][0] = a[i][j];
				row = i;
				column = j;
			}
		}
	}
	cout << "\nLowest value is row " << row << " and column " << column;


	system("pause>0");
	return 0;
}

I still don't understand TT_TT I'm kinda new in this kind of thing so it's so hard. i crie
ok one thing to note is that you're correctly starting your indexing at 1 in all the calculations except the highest and lowest checks. There you start at 0, which will throw off your results.

1
2
3
4
5
6
7
8
9
for (i = 0; i <= n; i++){
	for (j = 0; j <= m; j++){
		if (a[i][j] > a[0][0]){
			a[0][0] = a[i][j];
			row = i;
			column = j;
		}
	}
}


The above should look like:
1
2
3
4
5
6
7
8
9
for (i = 1; i <= n; i++){
	for (j = 1; j <= m; j++){
		if (a[i][j] > a[0][0]){
			a[0][0] = a[i][j];
			row = i;
			column = j;
		}
	}
}


see how your result work out then
My output:
1 2 3
4 5 6
7 8 9
Highest value is row 3 column 3
Lowest value is row 1 column 0//Still fixed :P
did you change the lowest calculations to start at 1 also?
I'm also concerned that you're using a[0][0] but you never initialize it.

Also you need to initialize flag at line 48.
At lines 49 & 51, you're using the wrong bounds for the for loops again.
@kes: I'm not asking for the minimum and maximum and number but its place in an array :)

However, std::min_element essentially returns a position, not value.

1
2
3
4
5
6
7
8
9
10
11
size_t row = 0;
size_t col = 0;
for ( size_t i = 0; i < N; ++i ) {
  for ( size_t j = 0; j < M; ++j ) {
    if ( a[i][j] > a[row][col] ) {
      row = i;
      col = j;
    }
  }
}
cout << "Max at (" << 1+row << ',' << 1+col << ")\n";
if you guys read his full code, you'll see what he's using a[0][0] for, and it does work, though it's a pretty strange way to go about it. the only problem that was stopping the code from working was his starting the lowest and highest searches with indexes 0 instead of 1.
Last edited on
if you guys read his full code, you'll see what he's using a[0][0] for, and it does work

No, it only works by accident. Since he isn't initializing a[0][0] it could contain anything. For example, if you add a[0][0] = 100; immediately after defining a, then the output I get is:
 Given matrix:                   row sum
         1         2         3              6
         4         5         6             15
         7         8         9             24
Col Sum:
             12
             15
             18
Trace:
             15
It is not an identity matrix
Highest value is row 2 and column 2293040
Lowest value is row 1 and column 1


Also he needs to initialize flag.
Also the indices at lines 49 & 51 are wrong: they should start from 1.
Also the indices for trace are wrong: i should go to n and j should go to m, not the other way around.
Topic archived. No new replies allowed.