This program needs to call a function that displays the positive numbers entered into an array. The program is either ignoring the function or the code is wrong, or both. I'm lost. Please help. Thanks.
#18 is the function being called in main, the rest is the function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
18 numpos(a, len); //return positive numbers
1 #include "array.h"
2
3 int numpos(int a[], int len)
4 {
5
6 for (int i = 0; i > 0; i++){
7 if (i % 2)
8 cout << a[i] << endl;
9 else
10 i++;
11 return i;
12 }
13 }
This won't even compile, as it doesn't return if the for-loop doesn't execute. Also, King214, that'd make it an infinite loop (if he removed the return), I think it should be i < len
You don't need to increment i twice, only the once will do. Also, what is if( i % 2 ) supposed to do? Finally, that return statement guarantees your loop will not live past the first loop.
In your if statement, you should have: if( a[ i ] >= 0 ). You don't need an else block for this. Change your for loop condition to: i < len.