Using the example from class add two functions that will find and return the max
ID: 3724714 • Letter: U
Question
Using the example from class add two functions that will find and return the maximum and minimum values of a 2_d array:
Example from class
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#define ROWS 20
#define COLS 10
int main() {
unsigned int sum_row, sum_col, max, min, sum, num;
double average;
cout << "TWO DIMENSIONAL ARRAY OPERATIONS" << endl << endl;
cout << "INITIALIZING THE ARRAY" << endl << endl;
cout << "ARRAY CONTENTS" << endl;
// Add all the elements
cout << endl;
cout << "ADD ALL THE ELEMENTS" << endl;
cout << "The sum of the elements in the array = is " << sum << endl;
// Compute the average value
cout << endl;
cout << "COMPUTE AVERAGE" << endl;
cout << "The average of the elements in the array = is " << average << endl;
// The Maximum and minimum valyes are
cout << endl;
cout << "FIND MAX AND MIN VALUES" << endl;
cout << endl << endl << endl;
system("pause");
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
#define ROWS 20
#define COLS 10
int Min(int a[][COLS],int rows,int cols)//method to find the min in 2_d array
{
int i,j;
int min=a[0][0];//setting min to initial value
for(i=0;i<rows;i++)
{for(j=0;j<cols;j++)
if(a[i][j]<min)//updating min
min=a[i][j];
}
return min;//returning min
}
int Max(int a[][COLS],int rows,int cols)//method to find the max in 2_d array
{
int i,j;
int max=a[0][0];//setting max to initial value
for(i=0;i<rows;i++)
for(j=0;j<cols;j++)
if(a[i][j]>max)//updating max
max=a[i][j];
return max;//returning max
}
int main() {
unsigned int sum_row, sum_col, max, min, sum, num;
double average;
cout << "TWO DIMENSIONAL ARRAY OPERATIONS" << endl << endl;
cout << "INITIALIZING THE ARRAY" << endl << endl;
int a[][COLS]={{1,2,3},{4,5,6},{7,8,9}};
cout << "ARRAY CONTENTS" << endl;
// Add all the elements
cout << endl;
cout << "ADD ALL THE ELEMENTS" << endl;
cout << "The sum of the elements in the array = is " << sum << endl;
// Compute the average value
cout << endl;
cout << "COMPUTE AVERAGE" << endl;
cout << "The average of the elements in the array = is " << average << endl;
// The Maximum and minimum valyes are
cout << endl;
cout << "FIND MAX AND MIN VALUES" << endl;
min =Min(a,3,3);
max = Max(a,3,3);
cout<<max<<" "<<min<<endl;
cout << endl << endl << endl;
//system("pause");
}
output:
TWO DIMENSIONAL ARRAY OPERATIONS
INITIALIZING THE ARRAY
ARRAY CONTENTS
ADD ALL THE ELEMENTS
The sum of the elements in the array = is 0
COMPUTE AVERAGE
The average of the elements in the array = is 0
FIND MAX AND MIN VALUES
9 1
Process exited normally.
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.