// ex_ch10_13.c
#include <stdio.h>
#define SIZE 5
#define NUMBER 3
#define VERY_LOW -1000

void question_user(int rows, int cols, double dest[rows][cols]);
double row_average(int rows, double arr[rows]);
double max_value(int rows, int cols, double arr[rows][cols]);

int main(void)
{
	double user[NUMBER][SIZE];
	double averages[NUMBER];
	int i;

	question_user(NUMBER, SIZE, user);

	for (i = 0; i < NUMBER; i++)
	{
		averages[i] = row_average(SIZE, user[i]);
		printf("Average of row %d: %.2f\n", 
		       i + 1, averages[i]);
	}

	printf("Average of all %.2f\n", row_average(NUMBER, averages));
	printf("Maximum of all %.2f\n", max_value(NUMBER, SIZE, user));
	
	return 0;
}

void question_user(int rows, int cols, double dest[rows][cols])
{
	int r, c;
	
	printf("Enter five float values\n");
	for (r = 0; r < rows; r++)
	{
		printf("Entering row %d: ", r + 1);
		for (c = 0; c < cols; c++)
			/* %lf scans as double, %f scans as float */
			while (scanf("%lf", &dest[r][c]) != 1)
			{
				// warn the user
				printf("Enter a float value.\n");
				// flush the remaining input
				scanf("%*s");
			}
		
	}
}

double row_average(int rows, double arr[rows])
{
	int i;
	double sum = 0.0;
	
	for (i = 0; i < rows; i++)
		sum += arr[i];
	
	return sum / rows;
}

double max_value(int rows, int cols, double arr[rows][cols])
{
	int max = VERY_LOW;
	int r, c;
	
	for (r = 0; r < rows; r++)
		for (c = 0; c < cols; c++)
		{
			if (arr[r][c] > max)
				max = arr[r][c];
		}

	return max;
}
