Thread: Can any tell me what is wrong with this?

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    Can any tell me what is wrong with this?

    Code:
    int sorted( int a[], int b)
    {
        
        for(int c = 0; c < b - 1; ++c)
        {
            if(a[c]<a[c+1])
            return 1;
            else
            return 0;
        }
    }
    I don't know why but this keeps giving me return 1

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Do you really think your loop is going to execute more than once?

    Unless a[] changes, it will always return the same value.

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    28
    Yes it will loop until the array is over, but somehow it keeps returning 1 even though it is wrong.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,458
    Yes it will loop until the array is over
    No will also stop if a[c] < a[c+1]. If a[c] is less than a[c+1] it will return 1.

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    I can only assume that you don't understand what a return statement does.

    The return statement terminates the execution of a function and returns control to the calling function.

    So, do you still think your loop has any effect?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Your problem is you want to return when all elements are checked - I assume you want to see if the array is in sorted order.
    Currently the function terminates after the first pair of elements is compared.

  7. #7
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Let me talk you through what is happening.

    On the first iteration of the loop, it tests whether a[0]<a[1]. If that's true then "return 1" happens, which halts the "sorted" function with a return value of 1. If a[0]<a[1] is false, then "return 0" happens, which halts the "sorted" function with a return value of zero.

    Under no circumstances can you ever get to the second iteration of the loop, because you exit the "sorted" function at the first iteration, no matter what.

    You want to return zero if the array is not sorted, which means returning zero at the first occurrence of a non-sorted pair of elements. So having a "return 0" inside the loop is fine. But you don't want to "return 1" until you've checked whether the entire array is sorted, which means you want the "return 1" outside the loop and after it, so that the loop can run to completion.
    Code:
    while(!asleep) {
       sheep++;
    }

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by AFCKING View Post
    Code:
    int sorted( int a[], int b)
    {
        
        for(int c = 0; c < b - 1; ++c)
        {
            if(a[c]<a[c+1])
            return 1;
            else
            return 0;
        }
    }
    I don't know why but this keeps giving me return 1
    maybe it would help to think of it this way:

    Code:
    function do_something(arg1, arg2){
        if (true):
           do_this()
    
       else: //if false
           do_that()
    }
    so whatever is in that first if statment seems to be true and that's why it keeps returning one. If you know that then you can trace the values for your agruments and see if maybe you can't pin point it.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Turn your warning levels up and fix the warnings. Control currently reaches the end of a non-void funtion.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-14-2011, 06:35 PM
  2. wrong wrong with my xor function?
    By Anddos in forum C++ Programming
    Replies: 5
    Last Post: 04-26-2009, 01:38 PM
  3. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  4. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  5. What am I doing wrong?
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-26-2003, 11:39 PM