This would be a lot easier if you used
std::vector<>
to store the items. Then you could use
std::min()
and
std::max()
. Have you learned about these things? Will your prof let you use them?
If you have to do it as shown then, here are some comments.
Line 17 (
int j[i+1]
) doesn't do what you think it does. At that point, i==0 so it allocates an array j with 1 item in it. When you input a value for i at line 25, the size of j does NOT change. In other words, the size of an array is fixed when you define it.
Throughout the program you store the i items in j[1] through j[i]. This may seem more natural, but C++ starts arrays at index value 0. So it's better to store the values in j[0] through j[i-1]. This way you aren't wasting an extra value of j.
Your code for the average is returning an int. Should it return a floating point type instead? If so then line 101 should be
double average(int i, int j[])
and line 109 should be
return (double)l/i;
Then you can get rid of line 111.
I suggest that you indent blocks of code and use curly braces, even when they aren't needed. There's nothing worse than having your code execute one statement when you think it's executing a block. For example, in min, change this:
1 2 3 4 5
|
for(int m=1; m<=i; m++)
if(j[m]<l)
l=j[m];
| |
to this:
1 2 3 4 5
|
for(int m=1; m<=i; m++) {
if(j[m]<l) {
l=j[m];
}
}
| |