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

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    50

    How to check if an array of doubles is sorted?

    Hi I wanted to check if an array of integers entered by the user is sorted in ascending order. I wrote the part to store the inputs into an array, but how do I check if they are in asending order? with the array number[]

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    This must be true
    if ( array[i] <= array[i+1] )

    For all elements of the array
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I would suggest to test if they are sorted at the same time you construct the array!Why?Because the complexity of the loop that runs through the whole array takes time O(n) because you check all the array.Imagine you have a large N.This would be such a waste of time.
    So when you get the number from the input-if it is not the first of course-compare it with the last element of the array that has been inserted.If it is bigger than it then it is ok.You do not have to test all the elements,because as the test goes one - if of course the test is true - you are going to be sure that the last element that has been inserted to the array is the biggest in value.In words of code here is what i say
    Code:
    #include <stdio.h>
    
    int main(void)
    {
         int n = 6;
         int array[n];
         int input;
         int i;
    
         printf("Please input %d numbers\n",n);
         /*read the first element*/
         scanf("%d",&input);
         array[0] = input;
         /*begin loop from 1*/
         for( i = 1 ; i < n ; i++)
         {
             scanf("%d",&input);
             /*compare it with the last element inserted*/
             if(input >= array[i-1])
             {
                  array[i] = input;
             }
             else
             {
                 printf("Error\n");
                 return 1;
             }
          }
          printf("Output\n");
          for( i = 0 ; i < n ; i++)
          {
             printf("%d\n",array[i]);
          }
          return 0;
    }
    Hope this helps

  4. #4
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    Thanks for the help, how do i now do this with malloc? instead of a fixed n (n=6)

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Read how malloc works from just about any source on the internet. Googling "malloc" leads to wikipedia and The Open Group, both excellent sources. Or do you have a specific question about malloc?

  6. #6
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    How do I ask for the size of the array n from the user and dynamically allocate memory for that size of array?

  7. #7
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Scanf("%d",&n);
    int *variable=malloc(n*sizeof(int));

  8. #8
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    the "variable" is the size you assigned to this array correct?

  9. #9
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    int *variable is just the pointer, pointed to that malloc, nvm

  10. #10
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Variable is your new allocated array.

  11. #11
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by camel-man View Post
    Scanf("%d",&n);
    int *variable=malloc(n*sizeof(int));
    n is the size of the array
    I also suggest you to check if n is positive of else you are going to declare an array with negative size.You do not want that to happen ,so just check if n>0
    variable points to what malloc has allocated for you In other words it's the array you have now

  12. #12
    Registered User
    Join Date
    Sep 2012
    Posts
    50
    can i still use the same logic you posted above, now that the array is malloc? std10093?

  13. #13
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    yes! you handle the array with exactly same way as before
    edit --> the only difference is in the way we allocated the malloc.Once the array is malloced,there is no difference in the way you handle,access the array etc.
    Of course you have to free what you allocate with malloc once you finish with what you are doing
    Do that with the function free (there you need to pass as parameter the pointer that you used to store what malloc returned to you,in other words free(variable))
    Last edited by std10093; 10-06-2012 at 02:46 PM.

  14. #14
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    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.

  15. #15
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You should hit the enter after typing every input (or you have to change some got in order to read a whole line)

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