Program With Array To Check For Increasing/Decreasing Numbers

Hello everyone,

Beginner C++ student here starting to learn different ways of using arrays. I am trying to put together the program below. It's supposed to ask the user for an input of a set of numbers that will go into an array. This set of numbers may increase or decrease at any point the user wants.

For example:

1 2 3 4 9 8 7 4 5 6 10 11 12 20 19 18 17

The program is to check against that set of numbers and account for every time there is an increase or decrease. Right now the program will return "increase" or "decrease".

For example, running it against the aforementioned set of numbers it returns:

increasing
decreasing
decreasing

However, I have two problems:

1. I think it is not accounting for all the changes (increases and decreases) occurring in the array correctly. Unsure where I have gone wrong.

2. I now have to return the number of changes that occur instead of "decrease"/"increase". Meaning, in the example above it would return "3" because there is 1 true value and 2 false.

If anyone can advise I would immensely appreciate the help.

Thank you so very much!!!

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

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


int main()
{
	int arr[20], n;

	cout << "Enter a set of Increasing/Decreasing numbers (ex. 1 2 3 6 5 4 7 8 9 3 2 1)." << endl;
	cout << "Press 'Enter' to see results" << endl;

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

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

	return 0;
}



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;
	}
}
Last edited on
I don't understand why the output should be 3 regarding the example output.

That being said...

First, your loop bound on line 17 is off-by-one. (less, not less-or-equal).
I would really recommend against using names like x and n when you could use names like n_increasing. You will read the code far more than you will write it (you're reading it now!), so clarity is important. (I had to decipher what each was by context.)

Your function increasing returns a boolean. You're not counting anything.
It looks like x will hold the number of "rising edges" in the input array (what you're looking for), but you're not returning that. Instead you're returning whether or not the entire array increases sequentially or whether it doesn't (which doesn't imply the whole array is "decreasing" as you print).

Try something like this. I prefer to use two iterators (prev and curr) but that's just a preference. I also think that array notation for parameters is terrible (because you can't pass an array to a function*, so I use the pointer notation instead.)
1
2
3
4
5
6
7
8
unsigned count_increasing(int const *const arr, int const sz) { 
  int increasing = 0;
  for (int prev = 0, curr = 1; curr < sz; ++prev, ++curr) { 
    if (arr[curr] > arr[prev]) ++increasing;
  }

  return increasing;
}


*Arrays decay to pointers when they are pushed on the stack. Size information is lost, so the array notation in parameters is a lie. You can pass an alias (reference) or pointer to an array of known size with the syntax void blah(int (&arr) [17]);.
Last edited on
Have you thought about using static local variables?

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
#include <iostream>

void checkIndDecr(int *arr, const int &index);

int main()
{
	int *arr = new int[20]{ 1, 3, 5, 7, 9, 8, 6, 4, 3, 2, 1, 12, 15, 4, 23, 3, 6, 87, 12, 0 };

	for (int i = 0; i < 20; i++)
		checkIndDecr(arr, i);

	delete[] arr;
}

void checkIndDecr(int *arr, const int &index)
{
	static bool increasing = false;
	static int lastValue = INT_MIN;

	//If we were previously increasing and the new value is less than the previous value
	if (increasing && arr[index] < lastValue) {
		std::cout << "Decreasing (starting at index = " << index << ", value = " << arr[index] << ", previousValue = " << lastValue << ")" << std::endl;
		increasing = false;
	}
	//If we were previously decreasing and the new value is greater than the previous
	else if (!increasing && arr[index] > lastValue) {
		std::cout << "Increasing (starting at index = " << index << ", value = " << arr[index] << ", previousValue = " << lastValue << ")" << std::endl;
		increasing = true;
	}

	lastValue = arr[index];
}

Produces
Increasing (starting at index = 0, value = 1, previousValue = -2147483648)
Decreasing (starting at index = 5, value = 8, previousValue = 9)
Increasing (starting at index = 11, value = 12, previousValue = 1)
Decreasing (starting at index = 13, value = 4, previousValue = 15)
Increasing (starting at index = 14, value = 23, previousValue = 4)
Decreasing (starting at index = 15, value = 3, previousValue = 23)
Increasing (starting at index = 16, value = 6, previousValue = 3)
Decreasing (starting at index = 18, value = 12, previousValue = 87)
@JayhawkZombie
Isn't dynamic memory a bit useless for such a tiny array? Use the stack, that's what it's there for. Or at least vector.

I will point out that you're marking &index as const, presumably so you don't mangle the referent, but you didn't mark your pointer as (const) pointer-to-const, which is inconsistent, since you're not changing arr, either.
Last edited on
I should have changed the array to pointer-to-const, to keep it consistent. I haven't gotten used to doing that yet, but I have a habit of using const ...&item for non-pointer types. But, yes, it would be better to keep them consistent. Thanks for pointing that out.

Yeah, it's a little over the top for a tiny array. I just did it because I already had the boiler-plate there from another problem.
Just doing int arr[] = {...}; would suffice just as well, or vector, or whatever :P
Thank you all very much for the advise!!!

@mbozzi To further clarify why in my original example I say the program should return "3" is that the task now calls for the program to count the number of times the entered set of numbers increases and decreases. So for another example:

{1 2 3 4 9 8 7 6 1 4 5 6}

The the first 4 numbers entered are increasing (ex. 1234) then that is "1", then if the next four numbers entered are decreasing ( ex. 9876) that is another change in the decreasing direction therefore a "1" again, then if the last 4 numbers are again increasing (ex 1456) then that's another change and another "1". In total there were 3 changes in increasing/decreasing direction.

Increasing
Decreasing
Increasing


Basically counting the times the whole array changes in either increasing or decreasing counting direction.
Last edited on
dose this help ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>

using namespace std;
void printarray(int thearray[],int sizeofarray);

int main()
{
int king[3]= {22,34,555};
int ahmed [5]={33,54,66,86,34};

printarray (ahmed, 5);

}

void printarray(int thearray[],int sizeofarray){

for (int x=0; x<sizeofarray;x++){

cout<< thearray[x] <<endl;

}

}
Topic archived. No new replies allowed.