c++ functions

Well the following program should be change each function for min / max and average I re-write it up to 10 time and isn't works.... I want a tip since my brain stops!!! :P DON"T post the full program answer just give me some details more in one function for example on min and I would let you know.....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//============================================================================
// Name        : COMP113.cpp
// Author      : .....
// Version     :
// Copyright   : Your copyright notice
// Description : COMP223 quiz 2 in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;
int main ()
{
int mn,mx;
const int Numb = 10;
double total;
int a[Numb];
double avg;
cout<<"Enter 10 values:";

for(int i=0;i<10;i++)
{
cout<< "\nEnter value: ";
cin>> a[i];

total = (a[0] + a[1] + a[2] +a[3] + a[4] + a[5] + a[6] + a[7] + a[8] +a[9] );

avg = total / 10;

}

mn=a[0];
mx=a[0];


for(int i=1;i<10;i++)
	{
		if(mn>a[i])
		{
			mn=a[i];
		}
		else if(mx<a[i])
		{
			mx = a[i];
		}
	}


cout<<"Maximum number is: "<< mx << endl;
cout<<"Minimum number is: "<< mn << endl;
cout<<"The average of numbers is: "<<avg<<endl;

return 0;

}


Please be aware that there are max & min -functions in C++ standard library. They are part of STL algorithms and you can use these functions after you have included this header file:

#include <algorithm>

Please see these links for info how to use them:
http://www.cplusplus.com/reference/algorithm/max/
http://www.cplusplus.com/reference/algorithm/min/

And if you are interested in seeing more useful algorithms like max & min, please check this link:
http://www.cplusplus.com/reference/algorithm/

Prefer C++ standard library. Algorithms found there are made by professionals and are well optimized, meaning that they are fast for instance. Creating your average function is OK, though.

Did you find this post helpful?
Last edited on
Topic archived. No new replies allowed.