My advice is in my comment to the question. So, your problems are: 1) you declared int function, but you don't return anything; 2) you don't even try to check if the element <= 0, only compare with
ele
which is 0; 3) you should not print anything in the function doing a calculation.
GeorgeGkas wrote:
If the function find negative numbers or number 0 then will return an error message in the main function else if it doesn't then will just proceed the program. Am I clear?
Better.
As I say, don't deal with strings (error message) in a calculating function. Just return an index in the first occurrence of an offending element (<=0), then -1 could denote "no problems". You can issue a message an a calling function, thus making your calculating function more reusable. This message can show this index and/or "bad" value or not.
In that function, first assign -1 to some variable. Have only one loop from <code>0 to
1 - length
. In that loop, check up if the current element is <= 0 and return the index immediately if it is. You can assign the index to the variable and break from the loop. Return the variable. If nothing was found, it will remain -1. You won't need any "if".
—SA