For Loop if statment

What I am trying to get done is use the if statement in the for loop to test both of the arrays and see if they are equal to zero. I tried doing an if statement with a logical or that looked like this
if (myArray[i] == 0 || myArray2[i] == 0)

and it did not work.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include <iostream>

using namespace std;

    int const arraySize = 10;
    int myArray[arraySize] = {0,0,0,0,1,0,0,0,0,0};
    int myArray2[arraySize] = {1,2,3,4,5,6,0,8,9,0};

class test
{
public:

    void testZero( int i) //Test if the array equals zero.
    {
        for (int i = arraySize - 1; i >=0; i--)
        {
            if (myArray[i] == 0)
            {
                cout << "True" << endl;
            }
            else
            {
                cout << "False" << endl;
            }

        }
    }
};



int main()
{

    test myTest;
    int forLoop;
    myTest.testZero(forLoop);


    return 0;
}  
Why does testZero take an int...? Furthermore, you probably want it to return a bool to indicate whether the array is zero or not.

When you say if an array is "equal to zero", do you mean one element is equal to zero or all elements are equal to zero?

Case one element: Return true on line 19 and false just below line 26. On line 19, it'll have found its one zero element and does not need to continue.

Case all elements: Return false on line 23 and true just below line 26. On line 19, it'll have found one nonzero element, which means that not all the elements are zero.

Just a suggestion. :)

-Albatross
Topic archived. No new replies allowed.