Question

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

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;
   }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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

Add a comment
Know the answer?
Add Answer to:
a. Write a function named findmax() that finds and displays the maximum values in a two...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • I am trying to write this code which asks "Write a program that ask the user,...

    I am trying to write this code which asks "Write a program that ask the user, the question: What is a^b? //The program generates two signed integers and gives the user four attempts to get the correct answer //a=- , b = + //a= + , b = - " So far this what I wrote. I am not sure how to do this correctly. #include<iostream> #include<cstdlib> #include<ctime> using namespace std; int main() {    srand(time(0));    int guess,a,ans,b, k;...

  • can someone help me fix this. The function that finds the minimum and maximum values of...

    can someone help me fix this. The function that finds the minimum and maximum values of the array and outputs the value and index doesnt find the maximum value of the array. #include <iostream> #include <fstream> #include<iomanip> using namespace std; void readArray(ifstream& inF, int arr[], int&ArrSize); void writeArray(ofstream & outF, int arr[], int & ArrSize); void MaxAndMin(ofstream & outF, int arr[], int & ArrSize, int &high, int &low); int main() {    int arr[100];    int i = 0;   ...

  • Hello, I am working on a C++ pick 5 lottery game that gives you the option...

    Hello, I am working on a C++ pick 5 lottery game that gives you the option to play over and over. I have everything working right except that every time the game runs it generates the same winning numbers. I know this is an srand or rand problem, (at least I think it is), but I can't figure out what my mistake is. I've tried moving srand out of the loop, but I'm still getting the same random numbers every...

  • My following program has an array which holds 1000 random integers between 1-1000. Now I need...

    My following program has an array which holds 1000 random integers between 1-1000. Now I need help to create an array that holds 10,000 random integer between 1-1000 in my following program. The main goal of this program is time analysis by using bubble sort and binary search algorithms. Please do the following task; 1. Replace the 1000 random integers with 10,000 random integers After change please answer the following question 2. what will be happen, if an array holds...

  • C++ Look the source code and, 1. the function findMaxVisits is empty and one of its...

    C++ Look the source code and, 1. the function findMaxVisits is empty and one of its parameters does not behave as it should. supply the missing statements and repair the damaged parameter. do not use any non-local constants or variables. 2. the fill function has some errors. repair the function without changing its parameter list. do not use any non-local constants or variables. 3. the show function has some errors. repair the function without changing its parameter list. One error...

  • Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN...

    Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() {    int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false;    srand(seed); // call the getRandom function below       tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...

  • c++ /*This is the starter file for your final proficiency test This program has a function...

    c++ /*This is the starter file for your final proficiency test This program has a function that will generate a list of integers using the rand function. Your job is to fill the insertNum and oddCount functions.*/ #include <iostream> #include <ctime> using namespace std; //constants and function prototypes const int CAP = 100; int buildList(int[], int size); void printList(int[], int size); //your functions to implement /*This function finds even numbers in the list, and inserts a number before the even...

  • Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should ge...

    Problem: Write a function named coinToss that simulates the tossing of a coin. When you call the function, it should generate a random number in the range of 1 through 2. If the random number is 1, the function should display “heads.” If the random number is 2, the function should display “tails.” Demonstrate the function in a program that asks the user how many times the coin should be tossed and then simulates the tossing of the coin that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT