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....
#include<iostream>
#include<iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
int main()
{
// variables
constint MAX_ROW(3);
constint 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
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;