Blank Compilation (Strictly Increasing Array)

Hello everyone,

I am trying to test a boolean function that is supposed to return "true" if the a specified array is strictly increasing (e.g., {1,2,3...}), and "false" if it is not. I came up with the following code. However, strangely when I compile I get a blank. There are no compilation errors--it just compiles as a blank.

I would appreciate any feedback. Thanks in advance!

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
42
43
#include <iostream>
#include <cassert>
using namespace std;

bool isStrictlyIncreasing(int a[], int len); //prototype

int main()
{
    int a[] = {0,1,2,3,4,5};
    assert(isStrictlyIncreasing(a,6)== true);

    int b[] = {0,0};
    assert(isStrictlyIncreasing(b,4)== false);

    int c[] = {2,3,4};
    assert(isStrictlyIncreasing(c,4)== true);

    int d[] = {4,3,2,1};
    assert(isStrictlyIncreasing(d,4)== false);

    int e[] = {-9092,-14,1,13,0};
    assert(isStrictlyIncreasing(e,5)== false);

    int f[] = {-1,9800,3,12,-9006};
    assert(isStrictlyIncreasing(f,5)== false);

    int g[] = {-88,0,1,88};
    assert(isStrictlyIncreasing(g,4)== true);

    cout << "All tests have successfully passed." << endl;
}

    bool isStrictlyIncreasing(int a[], int len)
{
    for(int i=1;i<len;++i)
    {
        if(a[i] <= a[--i])
        {
            return false;
        }
    }
    return true;
}
Perhaps due to:
 In function 'bool isStrictlyIncreasing(int*, int)':
37:26: warning: operation on 'i' may be undefined [-Wsequence-point]
line 37: if(a[i] <= a[--i]) isn't doing what you think it is. Try
line 37: if(a[i] <= a[i - 1]) instead.

This is what you've done:
1
2
int i =4;
int x = --i; // after this line, x == 3 and i == 3 

This is what you think you did:
1
2
int i =4;
int x = i - 1; // after this line, x == 3 and i == 4 

Also, in main(), lines 13 and 16, you're using the wrong value for length, and you'll index past the end of the array and crash. Try this instead:
1
2
3
4
5
    line 12:  int b[] = {0,0};
    line 13:  assert(isStrictlyIncreasing(b,2)== false);

    line 15:  int c[] = {2,3,4};
    line 16:  assert(isStrictlyIncreasing(c,3)== true);
Thank you tipaye! That was very helpful!
Topic archived. No new replies allowed.