/*
Example on two-dimensional arrays as function parameters/arguments. 
We read the array, print it, find the sum, average, and maximum. 
All of that is done by using functions.
*/
#include <iostream>
#include <iomanip>
using namespace std;
const int rows = 100;
const int cols = 10;

/* We need to pass to functions, the actual number of rows 
and the actual number of columns. But, the number in the 
second set of brackets must be the exact number of columns
and it must be a constant.
*/
float find_max(float a[][cols],int r, int c);
float find_avg(float a[][cols],int r, int c);
void read_array(float a[][cols], int r, int c);
void print_array(float a[][cols], int r, int c);

int main()
{
	int r, c; // r: number of rows; c: number of columns 
	float a[rows][cols];

	cout << "Enter the number of rows: ";
	cin >> r;
	cout << "Enter the number of columns: ";
	cin >> c;
	if ((r >= 1 && r <= 100) && (c >= 1 && c <= 10))
	{
		// Read the array
		read_array(a,r,c);
		
		// Print array
		print_array(a,r,c);

		cout << "The average is: " << find_avg(a,r,c)
			<< endl;

		cout << "The maximum is: " << find_max(a,r,c) 
			<< endl;
	}
	else
		cout << "Inavlid number of rows or columns.\n";

	return 0;
}

// This function reads the array
void read_array(float a[][cols], int r, int c)
{
	cout << "Enter " << r * c << " real numbers "
		<< "distributed on " << r << " rows with "
		<< c << " elements per row:\n";
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			cin >> a[i][j];

}

// This function prints the array
void print_array(float a[][cols], int r, int c)
{
	cout << "The array is: \n";
	for (int i = 0; i < r; i++)
	{
		for (int j = 0; j < c; j++)
			cout << setw(5) << a[i][j];
		cout << endl;
	}
	cout << endl;

}

// This function finds the maximum element of the array
float find_max(float a[][cols],int r, int c)
{
	float max = a[0][0];
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			if (a[i][j] > max)
				max = a[i][j];
	return max;
}

// This function finds the average of the array
float find_avg(float a[][cols],int r, int c)
{
	float sum = 0;
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			sum += a[i][j];
	return sum / (r * c);
}
