help - find address of element in 2D array

I've written a small program in native C++ that populates a 2D array with integers from the user. The program has to output the address of each element in the array, which I managed to figure out. The program also has to find the address of the largest integer that was entered by the user, but this is just beyond my skill level. So far I have only managed to get the first address of the array... but not the one containing the largest integer.

here is the complete code to give you and idea of what is going 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
#include<iostream>
#include<iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;

int main()
{
	// variables
	const int MAX_ROW(3);
	const int MAX_COL(4);
	int count_ROW(0);
	int count_COL(0);
	int countDown(12);
	int high(0);
	int count(0);
	// pointer declaration & initialization
	int* pHIGH(nullptr);
	int* pCOL(nullptr);
	// declare 2D array
	int numbers[MAX_ROW][MAX_COL] = {0};

	while(countDown >= 1)
	{
		// navigate array to input user data
		for(count_ROW = 0; count_ROW < 3; count_ROW++)
			for(count_COL = 0; count_COL < 4; count_COL++)
			{
				
				cout << endl << "Enter an integer."
					 << endl << "You have " << countDown << " more to enter. ";
				cout << endl;
				cout << endl;
				cin >> numbers[count_ROW][count_COL];
				--countDown; // tracks how many more integers the user has to input
				system("cls");
			}
	}
	// once array is full, while statement ends, if statement begins and outputs data
	if(countDown < 1)
		pCOL = &count_COL;
	{
		for(count_ROW = 0; count_ROW < 3; count_ROW++)
		{
			for(count_COL = 0; count_COL < 4; count_COL++, pCOL++)
				cout << setw(16) << pCOL;
			cout << endl;				  
		}								  
		// find address of the highest integer
		for(count_ROW = 0; count_ROW < 3; count_ROW++)
		{
			for(count_COL = 0; count_COL < 4; count_COL++)
				if(numbers[count_ROW][count_COL] > high)
					high = numbers[count_ROW][count_COL];
					pHIGH = &count_COL; 

		}								
	}
	cout << endl << "The address of the largest integer entered is: " << pHIGH;
	cout << endl;
}


as you can see, just above is a nested for loop to navigate the array,
originally this loop would find the value of the highest integer, not the address,
basically I tried to get pointer pHIGH to point to
the address of count_COL when the loop found the largest integer.....
and failed miserably

closed account (o3hC5Di1)
Hi there,

perhaps I'm misunderstanding, but to me it seems like you are over-engineering this.

1
2
3
4
5
6
//line 54
if(numbers[count_ROW][count_COL] > high)
{
    high = numbers[count_ROW][count_COL];
    pHIGH = &count_COL;
}


Your highest value will be in the "high" variable at the end of the loop.
So just use:

1
2
//line 60
cout << endl << "The address of the largest integer entered is: " << high;


Hope that helps.

All the best,
NwN
Last edited on
Lines 41-43 make no sense, granted the if statement should run EVERY time anyways, but the { should come directly after the if condition and BEFORE the first statement, as shows:
1
2
3
	// once array is full, while statement ends, if statement begins and outputs data
	if(countDown < 1) {
		pCOL = &count_COL;


And here as well, you have no brackets, so the second statement runs everytime:
1
2
3
				if(numbers[count_ROW][count_COL] > high)
					high = numbers[count_ROW][count_COL];
					pHIGH = &count_COL; 


Should be:
1
2
3
4
				if(numbers[count_ROW][count_COL] > high) {
					high = numbers[count_ROW][count_COL];
					pHIGH = &count_COL; 
				}
Topic archived. No new replies allowed.