Comp Sci C++ Error in function call that I don't understand

AI Thread Summary
The discussion centers on a C++ programming error related to checking an array for ascending order. The user encountered an issue where the function call was incompatible due to passing an integer instead of an array, leading to confusion about the expected output. The code logic was also flawed, as it prematurely returned -1 without checking all elements of the array. Participants highlighted the importance of using proper loop conditions and the need for braces in function definitions to avoid logical errors. Ultimately, the user resolved their issue by implementing the correct logic and structure in their code.
burton95
Messages
54
Reaction score
0
Hi there- I'm trying to check an array for ascending order and if not to print out the number that is out of order by using a function. I solved it by not using a function but when I've tried to use a fxn for some reason it tells me that in my call for the fxn the numbers "argument of type int is incompatible with parameter of type int"?

Code:
#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([B]numbers[/B], 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;
}
 
Last edited:
Physics news on Phys.org
numbers is an int but your function is expecting an array of ints:

try using the a array and the error message should vanish:
Code:
int number_out_of_order = out_of_order(a, amount_of_numbers);

Notice also you forgot to specify the amount_of_numbers variable.
 
  • Like
Likes 1 person
Thanks. amount_of_numbers = 5 at the top. Problem: As my current array is arranged 7 > 6 and therefore 6 should be shown on the screen but I keep getting -1. My original idea was to build this as the answer in the book was given
Code:
for (int i = 0; i < size - 1; i++)
	if (a[i] > a[i + 1])
		return i+1;
	else
		return -1;

and see if my logic

Code:
if(a[i] < a[i-1])
   return a[i];
else
   return -1;

was going to give the same answer but I can't even get the book logic to work. Please help me understand why.
 
Whoops. If a mod needs to move this to HW CS section I apolgize
 
burton95 said:
Whoops. If a mod needs to move this to HW CS section I apolgize
Done.


burton95 said:
Thanks. amount_of_numbers = 5 at the top. Problem: As my current array is arranged 7 > 6 and therefore 6 should be shown on the screen but I keep getting -1. but I can't even get the book logic to work. Please help me understand why.
[Post your new code. The code you posted in the opening doesn't even compile. If you are getting a result you must be using some updated code.

Your code in the opening post committed a typical newbie programmer error: Sloppiness. You cannot be sloppy in programming. You declared your variable "numbers" as an int rather than as an array of ints. Next you declared an array of ints "a" and never used it.
 
burton95 said:
Code:
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;
}
In addition to what others have mentioned, this code is only checking the first pair of array elements.
 
Code:
 #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, 9, 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 a[i + 1];
	else
		return -1;
}
 
Last edited:
Pretend you are the computer. Execute your program by hand. What happens inside your function out_of_order? Walk through the loop. What does it do?

I'll let you try this, then if you still need help, I or someone else will help.

Big hint: Read post #6.
 
I must admit I don't understand why the book answer has in the for loop i < size - 1 rather that just size. If I have an array of five spaces I need the loop to execute i 0 through 4.

a[0] = 2
a[1] = 7
a[2] = 9
a[3] = 8
a[4] = 12
1st loop
Step 1: i = 0 so
Step 2: check that i < size(5) - 1 is T or F. T
Step 3: execute body: a[0] = 2 < a[1] = 7 -> -1
Step 4: increment i;

2nd loop
Step 1: i = 1
Step 2: i < 4 True
Step 3: execute body a[1] = 7 < a[2] = 9; True
Step 4: return -1 and increment i.

3 rd loop
step 1: i = 2
step 2: i< 4 true
step 3: execute body a[2] < a[3]. False
step 4: return a[3]What am I missing
 
Last edited:
  • #10
If there are 5 elements in the array, stored in a[0], a[1], a[2], a[3], a[4], you can only compare 4 pairs of elements, not 5 pairs:

a[0] and a[1]
a[1] and a[2]
a[2] and a[3]
a[3] and a[4].

That's why the loop tests for i < size-1, not i < size.
 
  • #11
burton95 said:
I must admit I don't understand why the book answer has in the for loop i < size - 1 rather that just size. If I have an array of five spaces I need the loop to execute i 0 through 4.

a[0] = 2
a[1] = 7
a[2] = 9
a[3] = 8
a[4] = 12

1st loop
Step 1: i = 0 so
Step 2: check that i < size(5) - 1 is T or F. T
Step 3: execute body: a[0] = 2 < a[1] = 7 -> return -1
[strike]Step 4: increment i;

2nd loop
Step 1: i = 1
Step 2: i < 4 True
Step 3: execute body a[1] = 7 < a[2] = 9; True
Step 4: return -1 and increment i.

3 rd loop
step 1: i = 2
step 2: i< 4 true
step 3: execute body a[2] < a[3]. False
step 4: return a[3][/strike]
In addition to your confusion about size vs. size - 1, you are still not recognizing a key point. I added an important word above, which you omitted: return. Return means "exit the function now with the specified return value" so everything I [strike]crossed out[/strike] does not occur.

Can you explain why you want to return -1? How can this ever be a valid value for "number of out of order elements"?
 
  • Like
Likes 1 person
  • #12
Thank you AlephZero and jbunn. Ok, not to sound defensive but the body of function was cut and pasted from the answer in the book. I actually had different code and was trying to understand why they used the code they did. The question directly from the book:

Write a fxn named out of order that takes as parameters an array of double's and an int parameter named size and returns a value of type int. This fxn will test this array for being out of order, meaning that the array violates the following condition:

array[0] <= a[1] <= a[2] <= ...

The function returns -1 if the elements are not out of order; otherwisse, it return the index of the first element of the array that is out of order.

The complete answer in the same format from the back of the book(Savitch, "Problem Solving in C++):

Code:
int out_of_order (double array[], int size)
{
for (int i = 0; i < size - 1; i++)
	if (array[i] > array[i + 1])
	return i + 1;
return -1;
}

A couple things:
1. I changed the double to int
2. I was checking that my code which compared a < a[i-1] would give the same answer so I needed a baseline. Interestingly the "return" for i+1 is not italics or bold like the "return" -1 line is. What say you??
 
Last edited:
  • #13
Can you see the difference between your code and the code in the book? What does the code in the book do if the if condition is false? What does your code do?

It doesn't help that the code in the book is badly written - personally I would write it like this:
Code:
int out_of_order (double array[], int size)
{
	for (int i = 0; i < size - 1; i++) {
		if (array[i] > array[i + 1]) {
			return i + 1;
		}
	}
	return -1;
}

but at the very least it shoud have an extra indent before the return i+1 like this:

Code:
int out_of_order (double array[], int size)
{
for (int i = 0; i < size - 1; i++)
	if (array[i] > array[i + 1])
		return i + 1;
return -1;
}
 
  • Like
Likes 1 person
  • #14
Thanks Anchovy. The increments i and if loop continues running until you drop out of the for loop or a > a[i+1]. I got my original code to work correctly, I used braces for the body of the if loop, but got all out of wack/confused when I tried his code.
 

Similar threads

Replies
8
Views
1K
Replies
3
Views
1K
Replies
15
Views
2K
Replies
2
Views
2K
Replies
1
Views
1K
Replies
12
Views
2K
Replies
2
Views
2K
Replies
7
Views
2K
Back
Top