Display positive numbers in an array

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  }
it's calling the function, just take a look at your "for" loop. try changing your i > 0; into i >= 0
Last edited on
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
Last edited on
Changing it i < len didn't do anything. It compiles just fine. I have a feeling the function isn't doing anything.
closed account (zb0S216C)
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.

Wazzak
Last edited on
I figured it out. Thanks for the help.
Last edited on
Rebecca Bubenheim wrote:
Any suggestions?
Start a new thread in the forum, provide a little more information and maybe some code and we'll be glad to help you!

Bumping old threads is usually not a great move.
Topic archived. No new replies allowed.