Line 19: "input" probably returns the memory address of the array and that is never 0.
1 2 3 4 5 6 7
|
int i = 0; int sum = 0; int value = 0;
while ( i < 100 && cin >> value && 0 != value )
{
input[i] = value;
// other code
++i;
}
|
Both while and for loops can be used. Loop will do at most 100 iterations.
Logical && is lazy; if left side is false, right side is not evaluated.
Operator << returns istream object. istream converts implicitly to boolean. It is true, if stream is good.
If you get to the third test, the read operation has succeeded and 'value' has a new valid number.
0 is not stored to the array.
Line 31: Why 1? The array could be empty.
Line 32: I just did type 100 numbers, so 0 was never written to the array. Your loop can proceed beyond the array, where unknown values can exists.
Overall, you could have a counter in the input loop, so separate function is not needed.
Yes, C-strings (a char array) terminate with 0, but that convention is not usually used for other datatypes.
Line 42: from 1 to 100?
j must be larger than i, and j starts at 99, so 100 is definitely too much.
First iteration. i==1. j==2. This is the smallest j you will get. input[1] > input[2]. You never look at input[0].
Line 51:
* Function returns one value only. The line compiles, because the comma operator returns one value. Do you know what is the result of
(7, 42)
?
* There is no element input[100] in the array.
Line 38:
* Functions take array parameters by reference. 'input' in the function is not a copy; it is the same memory as 'input' in main. Tip: using same name in different context is confusing.
* Sort sorts. Your function both sorts and (attempts to) return maximum value. Two tasks for one function. Simpler is better.
Line 24: integer division discards remainder.
double foo = static_cast<double>(sum) / count;