increasing or decreasing array prblem

Hi.
I have a problem with the bool increasing funtction.
It seems it only compares the first 2 numbers in any situation.
Example input;
10 for int n, then enter 10 numbers

3704 7061 3245 111 4376 8101 1188 9051 818 1187

I should get not increasing from this but I get increasing.

Any help appreciated.

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
44
45
46
47
48
49
50
51
52
53
  #include <iostream>
using namespace std;

bool increasing (int arr [], int n);

int main()
{
    int arr[100], n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }

        cout << (increasing(arr, n) ? "increasing" : "not increasing") << endl;
    }

    return 0;
}

// TODO: function implementation comes here
bool increasing (int arr [], int n)
{

    for(int i = 0; i < 100 - 1; i++)
        {
                if(arr[i + 1] > arr[i])
                   {
                        return true;
                   }
                else
                    {
                        return false;
                    }


                if(arr[i + 1] < arr[i])
                   {
                       return true;
                   }
                else
                    {
                        return false;
                    }


        }

    return 0;

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool increasing( const int arr[], int n ) // const added
{
    if( n>1 )
        for( int i=1 ; i<n ; ++i ) if( arr[i] <= arr[i-1] ) return false ;

    return true ;
}

bool non_decreasing( const int arr[], int n )
{
    if( n>1 )
        for( int i=1 ; i<n ; ++i ) if( arr[i] < arr[i-1] ) return false ;

    return true ;
}
Lets look at your function. Iteration starts. i==0.

IF arr[1] > arr[0]
THEN end function and return true
ELSE end function and return false.

The second if-clause is never evaluated. The loop never proceeds any further.

Conclusion: you cannot return from within the loop since you need to check that every pair is increasing. You can return early for a "fail case" though.
As it is the program runs forever as the while loop waits for cin input from the user.

Do you want the user to enter the numbers? If so you need to begin with something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main() {
	
	int arr[5];
	int n = 0;
	
	cout << "Enter 5 numbers: " << endl;
	
	while(n<5)
	{
		cin >> arr[n];
		n++;
	}
	
	return 0;
}


Also the if else statements in your increasing() function need changing, the second if else is unnecessary and will never be be used as the function will always return from the first if else block (unless the arr[i+1] == arr[i])
Not sure if it's exactly what you want, but this works...

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

const int ARRAY_SIZE = 7;

bool increasing (int arr []);

int main() {
	
	int arr[ARRAY_SIZE];
	int n = 0;
	
	cout << "Enter " << ARRAY_SIZE << " numbers: " << endl;
	
	while(n<ARRAY_SIZE)
	{
		cin >> arr[n];
		n++;
	}
	
	cout << (increasing(arr) ? "increasing" : "not increasing") << endl;
		
	return 0;
}


bool increasing (int arr [])
{
	int x = 0;
	
	for(int i = 0; i < ARRAY_SIZE-1; i++)
    {
		if(arr[i] < arr[i+1]) x++;
	}
	
	if(x == ARRAY_SIZE-1) return true;
	else return false;
}
machinafour wrote:
As it is the program runs forever as the while loop waits for cin input from the user.

That is not a problem.

Yes, the program asks for a number and if it gets one, then it takes as many values and tells something about them. This repeats as many times as the user wants. Giving a non-number or EOF ends the loop. Perfectly fine.
At line 26, you're looping for 99 times, even though the user may have entered N as less than 100. This means you're comparing uninitialized array locations.

 
for(int i = 0; i < 100 - 1; i++)


This should be:
 
for(int i = 0; i < n-1; i++)




As it is the program runs forever as the while loop waits for cin input from the user.

That is not a problem.

Yes, the program asks for a number and if it gets one, then it takes as many values and tells something about them. This repeats as many times as the user wants. Giving a non-number or EOF ends the loop. Perfectly fine.


OK, thanks :) I am a beginner too.
Thanks for the information.

The main portion is supposed to be like this for this.

It is the bool function that I needed to fix .
I put in the changes recommend above.

My output where we return our program in mooshak returns this.

Output was not correct
Difference between obtained and expected output

Obtained output
Expected output
n 1 not increasing n
2 not increasing 1 not increasing
3 not increasing 2 not increasing
4 not increasing 3 not increasing
5 increasing 4 increasing
6 increasing 5 increasing
t t 6 increasing



Last edited on
Show your current version.
if you mean the current version of mooshak then it is shak 1.6b13
No. Show your code.
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
44
45
46
47
48
#include <iostream>
using namespace std;

bool increasing (int arr [], int n);


int main()
{
    int arr[100], n;
    while (cin >> n)
    {
        for (int i = 0; i < n; i++)
        {
            cin >> arr[i];
        }

        cout << (increasing(arr, n) ? "increasing" : "not increasing") << endl;
    }

    return 0;
}

// TODO: function implementation comes here



bool increasing (int arr [], int n)
{
	int x = 0;
	
	for(int i = 0 ; i < n - 1; i++)
    	{
		if(arr[i] < arr[i + 1]) 
		{
		    x++;
		}
	}
	
	if(x == n - 1) 
	{
		return true;
	}
	else 
	{
		return false;
	}
}
Never mind, figured out the problem.

Had to be <= in the if statement within the for loop.

Thank you all for your help. This forum rocks.
Topic archived. No new replies allowed.