Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

a. Write a function named findmax() that finds and displays the maximum values i

ID: 3805390 • Letter: A

Question

a. Write a function named findmax() that finds and displays the maximum values in a two dimensional array of integers. The array should be declared as a 10 row by 20 column array of integers in main() and populated with random numbers between 0 and 100.

b. Modify the function written above so that it also displays the row and column numbers of the element with the maximum value.

I have this so far, and but it's only finding the MAX of the last column/row.... and I need to display the row/column where this MAX value resides/stays/lives....:

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int findmax(int array[][20]);

int main()
{
   srand(time(NULL));

   const int row = 10;
   int array[row][20];

   for (int k = 0; k < row; k++)
   {
       for (int i = 0; i < 20; i++)
       {
           array[k][k] = rand() % (100 + 1);
           cout << array[k][k] << " ";
       }
       cout << endl;
   }
   int max1 = findmax(array);
   cout << "The maximum value is " << max1 << endl;

   system("pause");
   return 0;
}

int findmax(int array[][20])
{
       int i, j;
       int max = array[0][0];

       for (i = 1; i < 10; i++)
           for (j = 1; j < 20; j++)
               if (array[i][j] > max)
               {
                   max = array[i][j];
               }
       return max;
   }

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int findmax(int array[][20],int &maxRow, int &maxCol);
int main()
{
srand(time(NULL));
const int row = 10;
int array[row][20];
int maxRow = 0, maxCol = 0;
for (int k = 0; k < row; k++)
{
for (int i = 0; i < 20; i++)
{
array[k][i] = rand() % (100 + 1);
cout << array[k][i] << " ";
}
cout << endl;
}
int max1 = findmax(array, maxRow,maxCol );
cout << "The maximum value is " << max1 <<" at row: "<<maxRow<<" col: "<<maxCol<< endl;
// system("pause");
return 0;
}
int findmax(int array[][20],int &maxRow, int &maxCol)
{
int i, j;
int max = array[0][0];
for (i = 0; i < 10; i++){
for (j = 0; j < 20; j++){
if (array[i][j] > max)
{
  
max = array[i][j];
maxRow = i
;
maxCol = j;

}
}
}
return max;
}

Output:

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                                   

sh-4.2$ g++ -std=c++11 -o main *.cpp                                                                                                                                                                                                                                   

sh-4.2$ main                                                                                                                                                                                                                                                           

11 57 10 19 56 2 88 68 13 52 53 82 66 23 4 22 28 20 14 53                                                                                                                                                                                                              

74 99 23 27 26 93 49 5 60 17 67 37 75 77 56 97 46 44 30 25                                                                                                                                                                                                             

62 83 7 28 72 11 16 100 98 30 18 37 28 7 64 20 0 79 25 26                                                                                                                                                                                                              

97 93 63 37 35 86 33 81 96 63 6 57 11 80 51 49 91 67 14 54                                                                                                                                                                                                             

63 99 91 58 5 20 78 72 100 3 98 96 96 27 32 30 12 31 78 7                                                                                                                                                                                                              

60 50 64 37 29 15 86 86 48 66 5 11 64 62 69 69 82 46 7 81                                                                                                                                                                                                              

15 4 42 10 31 74 7 9 71 51 83 30 67 47 33 62 28 85 13 42                                                                                                                                                                                                               

50 18 19 13 80 88 49 61 0 22 8 15 26 50 93 24 91 66 33 61                                                                                                                                                                                                              

83 16 58 49 29 91 77 23 76 90 31 25 74 51 5 53 4 20 80 71                                                                                                                                                                                                              

42 54 87 34 4 45 58 95 77 58 21 59 40 79 74 35 70 50 24 11                                                                                                                                                                                                             

The maximum value is 100 at row: 2 col: 7

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote