not getting expected output; array and fxn

I'm expecting to get 6 as a return value but I keep getting -1 ??

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

int out_of_order(int a[], int size);
const int amount_of_numbers = 5;

int main()
{

	int numbers;


	int a[] = { 2, 7, 6, 8, 12 };

	int number_out_of_order = out_of_order(a, amount_of_numbers);
	cout << number_out_of_order;
}

int out_of_order(int a[], int size)
{
	for (int i = 0; i < size - 1; i++)
	if (a[i] > a[i + 1])
		return i+1;
	else
		return -1;
}


I was hoping to check my original logic:

1
2
3
4
5
for (int i = 0; i < size; i++)
if(a[i] < a[i-1])
   return a[i];
else
   return -1;


against the book logic in the first section of code but something is screwed up and I can't find it.
Last edited on
You're returning a value on the first comparison, so the for loop doesn't run all the way through - I don't think that's what you meant to do?

You are correct I do not want return a value for every comparison. I did notice at some point I was getting -1 -1 -1 ... The logic came straight out of the answers in the book but now I don't even get that output I get the press return... output.
got it. I had commented out the cout after the call. Thanks for your help
Topic archived. No new replies allowed.