// Solution of Assignment 11

#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.
*/
int find(float a[][cols],int r, int c, float number);
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], number;

	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);
		
		cout << "Enter a real number to search the array for: ";
		cin >> number;

		cout << number << " appears in the array " 
			<< find(a,r,c,number) << " times.\n";

		// Print array
		print_array(a,r,c);

	}
	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++)
			// Assume numbers are less than 6 digits
			cout << setw(6) << a[i][j];
		cout << endl;
	}
	cout << endl;

}


int find(float a[][cols],int r, int c, float number)
{
	int count = 0;
	for (int i = 0; i < r; i++)
		for (int j = 0; j < c; j++)
			if (a[i][j] == number)
				count++;
	return count;
}
