Thread: How to check if an array of doubles is sorted?

  1. #16
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by camel-man View Post
    Why are you comparing to array[i-1] why not just i? You're also scanning back to back which I doubt you mean to do.
    If he compares with i what is going to be checked ?array[i] is the element we are testing if it is ok to be inserted (look post number 3 please)

  2. #17
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Ahh ok, I thought he was trying to actually sort the array from beginning.

  3. #18
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    ah ok, right. So std10093, should I put free(array) before return 0? what if I dont put the free statement?

  4. #19
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Yes, as well as one before return 1, or you could have a memory leak.

  5. #20
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    thanks, camel-man and std!

  6. #21
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by camel-man View Post
    Ahh ok, I thought he was trying to actually sort the array from beginning.
    she (i think she we should say ) is trying to test of the input is sorted

    Quote Originally Posted by Lina_inverse View Post
    ah ok, right. So std10093, should I put free(array) before return 0? what if I dont put the free statement?
    Yes,before the return 0 should be perfect
    Well if you do not put it you may get a segmentation fault or a bus error (the 1st more likely) .If you are on windows your program may terminates safely but this is so bad,because this way you can not understand that you have a memory leak ,so you might not spot it and as a result not fixing it!!!

    Why do we free our memory?
    Imagine you have a LARGE program.Your program allocates sources.Sources of what?Of a system,let's say of your pc.As you can imagine the memory is limited in a computer (it is much but not infinite).So imagine that you had many calls of mallocs and you store big blocks of memory but you do not free them when you are done with them.Then you are in danger of running out of available memory!!So next you are going to allocate something,this will be impossible and malloc will return NULL.
    This may be seem not so dangerous if you have a single main,but imagine a function that is called MANY times,allocates some memory for a usual local variable(which dies when the function terminates) and you do not free this memory when the function call ends.So you keep this memory allocated for no reason!Also then you have lost access to that memory.

    So,always free your allocated memory when not needed anymore!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to detect if a array is sorted.
    By just_rookie in forum C++ Programming
    Replies: 14
    Last Post: 09-10-2012, 05:35 AM
  2. 2D array of doubles
    By hencherz in forum C++ Programming
    Replies: 8
    Last Post: 02-26-2012, 10:24 AM
  3. Selecting pivot in sorted array.
    By infantheartlyje in forum General Discussions
    Replies: 4
    Last Post: 10-14-2011, 09:43 AM
  4. how delete 1th element of a sorted array
    By vicky_4040 in forum C Programming
    Replies: 4
    Last Post: 10-11-2009, 06:12 AM
  5. finding max/min in a sorted array
    By Crcullen3916 in forum C++ Programming
    Replies: 9
    Last Post: 09-23-2008, 02:18 AM