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

  • Context: Comp Sci 
  • Thread starter Thread starter burton95
  • Start date Start date
  • Tags Tags
    C++ Error Function
Click For Summary

Discussion Overview

The discussion revolves around a C++ programming issue regarding a function designed to check if an array is in ascending order and to identify any out-of-order elements. Participants are exploring the logic of the function, the handling of array parameters, and the implications of certain coding practices.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Homework-related

Main Points Raised

  • One participant describes an error message indicating an argument type mismatch when calling the function, suggesting that the variable 'numbers' should be replaced with the array 'a'.
  • Another participant points out that the function currently only checks the first pair of elements, which may lead to incorrect results.
  • There is confusion about the loop condition 'i < size - 1', with one participant questioning why it is not simply 'size'.
  • Some participants clarify that the loop condition is necessary to avoid accessing out-of-bounds elements in the array.
  • One participant expresses frustration that their implementation returns -1 instead of the expected out-of-order element, indicating a misunderstanding of the function's logic.
  • Another participant highlights the importance of the 'return' statement, explaining that it exits the function immediately, which affects the flow of the loop.
  • A participant shares their experience of modifying the function based on a book's example and discusses differences in logic between their code and the book's code.
  • There is a suggestion to use braces for the if statement to clarify the intended logic and prevent confusion.

Areas of Agreement / Disagreement

Participants express various viewpoints on the function's logic and implementation, with some agreeing on the need for clearer coding practices while others remain uncertain about specific aspects of the function's behavior. The discussion does not reach a consensus on the best approach to the problem.

Contextual Notes

Some participants note that the original code does not compile due to variable declaration issues, and there are unresolved questions regarding the logic of returning -1 as a valid output for identifying out-of-order elements.

Who May Find This Useful

Readers interested in C++ programming, particularly those learning about function implementation, array handling, and debugging techniques.

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   Reactions: 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   Reactions: 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   Reactions: 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 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K