I have a problem in my Intro to C++ study guide for my final that asks for a user-defined function to find the largest value and its index in a 2D int array. I've managed to write the code to create the array using a random number from 50-100 (yay) but I'm struggling with applying what I know about sorting 1D arrays to a 2D array. I have included the code that I have so far, but I'm not really sure if I'm on the right track. Currently I'm trying to swap the array values until the [0][0] value is the max, but I have no idea if this is the most efficient way to find the max value, and there's a chance it wouldn't be an acceptable solution, as I believe the wording in the study guide says to display the index value of the max value, meaning the original array should probably remain unchanged to provide the correct index.
Any help would be greatly appreciated. I'm a noob at this, so please forgive me if I haven't organized the question or code appropriately.
I have a problem in my Intro to C++ study guide for my final that asks for a user-defined function to find the largest value and its index in a 2D int array.
Okay, how are you trying to compute the index of the 2D array?
but I'm struggling with applying what I know about sorting 1D arrays to a 2D array.
Okay, but first why are you trying to sort the array? Why not just iterate though the array and find the "index" of the maximum value?
there's a chance it wouldn't be an acceptable solution, as I believe the wording in the study guide says to display the index value of the max value, meaning the original array should probably remain unchanged to provide the correct index.
Most likely.
Why are you passing those two ints into the function but never really using the values being passed? What exactly are you trying to pass into that function with those parameters? I would think that you may want to think about passing the sizes of the arrays into the function.
Also this:
1 2 3 4 5 6
int main()
{
int arr[3][5];
int rows = 3;
int columns = 5;
Should really be more like:
1 2 3 4 5 6
int main()
{
constint rows = 3;
constint columns = 5;
int arr[rows][columns];
Lastly for now, you really really need to start using meaningful variable and function names, this will make following the logic much simpler.
I really appreciate the hints. I was feeling pretty lost. Sorry about the confusing var/function names, I'll try to clean that up. Thanks for pointing me in the right direction!
I think I'm starting to pick up what you're putting down lol.
For the user function, is this more in the right ballpark? I know the code is incomplete, I'm just curious if I'm approaching it from a better angle than before.
It looks like your cout is probably at the wrong place, and it is only attempting to print the maximum value, not the "index".
Also the maxnum initialization seem to be in the wrong place. It probably should be placed before the loops, and remember that arrays start at zero and stop at size - 1. You're trying to access the array at size, not size -1.
The "index" will be the index locations where the maximum value is located.
Also don't be afraid of a little consistent white space in your calculations:
I'm trying to account for the out of bounds possibility by placing a couple of for loops within the function. The output is definitely wrong for what I want, am I getting closer or is there a more efficient way to ensure the appropriate variables are accessed?
void findMaxandIndex (int arr[3][5], constint rows, constint columns)
{
int row_index=0;
int column_index=0;
int maxnum= arr[row_index][column_index];
for (int row_index=0; row_index<rows; row_index++)
{
for (int column_index=0; column_index<columns; column_index++)
{
int nextrow=row_index+1;
int nextcolumn = row_index+1;
for (nextrow; nextrow < rows; nextrow++)// as long as next row is less than size
{
if (arr[nextrow][column_index]>arr[row_index][column_index])
{
maxnum= arr[nextrow][column_index];
row_index=nextrow;
}
}
for (nextcolumn; nextcolumn < columns; nextcolumn++)// same but for columns
{
if (nextcolumn<columns && arr[row_index][nextcolumn]>arr[row_index][column_index])
{
maxnum = arr[row_index][nextcolumn];
column_index=nextcolumn;
}
}
}
cout << maxnum<<endl;
cout << row_index<<endl;
cout << column_index<<endl;
}
}
Again your printout appears to be in the wrong place, it should be after all the loops end. Also you should only need two for() loops to accomplish this objective.
I really appreciate it, that makes a lot more sense than what I was trying to do. For clarity, are you suggesting I print the maxnum within the void function still and return the index value for it to the main function for printing, or return both (as a string maybe?) to the main?
No, I'm saying return the "index" to the calling function and do any printing there. The idea is to have your function do one thing, in this case find the maximum value and return the "index" let "someone" else do whatever needs to be done with that "index".