array(min, max, av)

 avatar
Alexmegawin
c_cpp
a year ago
1.2 kB
3
Indexable
Never
#include <iostream>
using namespace std;

int fmax(int* a, int n)
{
	int max = a[0];

	for (int i = 1; i < n; i++) 
	{
		if (a[i] > max)			
		{
			max = a[i];
		}
	}
	return max;
}

int fmax2(int* a, int n)
{
	int max = a[0];
	int max2 = a[1];

	for (int i = 1; i < n; i++)
	{
		if (a[i] < max && a[i] > max2)
		{
			max2 = a[i];
		}
		if (a[i] > max)
		{
			max2 = max;
			max = a[i];
		}
	}
	return max2;
}

int fmin(int* a, int n)
{
	int min = a[0];

	for (int i = 1; i < n; i++)
	{
		if (a[i] < min)
		{
			min = a[i];
		}
	}
	return min;
}

int faverage(int* a, int n)
{
	int sum = 0;

	for (int i = 0; i < n; i++) {
		sum += a[i];
	}
	return sum / n;
}

void fmaxmin(int* a, int n, int &max, int &min)
{
	max = a[0];
	min = a[0];

	for (int i = 1; i < n; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
		if (a[i] < min)
		{
			min = a[i];
		}
	}
}

int main() {
	const int n = 5;
	int max, min;
	int array[n] = {5, 4, 3, 2, 1}; 

	fmaxmin(array, n, max, min);

	cout << fmax(array, n) << endl;
	cout << fmax2(array, n) << endl;
	cout << fmin(array, n) << endl;
	cout << faverage(array, n) << endl;
	cout << max << endl << min;
}