1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jul 20, 2014 #1
    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 (Text):

    #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: Jul 20, 2014
  2. jcsd
  3. Jul 20, 2014 #2

    jedishrfu

    Staff: Mentor

    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 (Text):

    int number_out_of_order = out_of_order(a, amount_of_numbers);
     
    Notice also you forgot to specify the amount_of_numbers variable.
     
  4. Jul 21, 2014 #3
    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 (Text):

    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 (Text):

    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.
     
  5. Jul 21, 2014 #4
    Whoops. If a mod needs to move this to HW CS section I apolgize
     
  6. Jul 21, 2014 #5

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Done.


    [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.
     
  7. Jul 21, 2014 #6

    jbunniii

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    In addition to what others have mentioned, this code is only checking the first pair of array elements.
     
  8. Jul 21, 2014 #7
    Code (Text):
     #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: Jul 21, 2014
  9. Jul 21, 2014 #8

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    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.
     
  10. Jul 22, 2014 #9
    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: Jul 22, 2014
  11. Jul 22, 2014 #10

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    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.
     
  12. Jul 22, 2014 #11

    jbunniii

    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    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"?
     
  13. Jul 22, 2014 #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 (Text):



    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: Jul 22, 2014
  14. Jul 22, 2014 #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 (Text):
    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 (Text):
    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;
    }
     
  15. Jul 22, 2014 #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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C++ Error in function call that I don't understand
  1. C++ card class errors (Replies: 28)

  2. C++ ODBC Error (Replies: 3)

  3. Unknown error in c++ (Replies: 3)

  4. Error checking in C (Replies: 8)

Loading...