# How to check if an array of doubles is sorted?

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-06-2012
Lina_inverse
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[]
• 10-06-2012
Salem
This must be true
if ( array[i] <= array[i+1] )

For all elements of the array
• 10-06-2012
std10093
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 :)
• 10-06-2012
Lina_inverse
Thanks for the help, how do i now do this with malloc? instead of a fixed n (n=6)
• 10-06-2012
whiteflags
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?
• 10-06-2012
Lina_inverse
How do I ask for the size of the array n from the user and dynamically allocate memory for that size of array?
• 10-06-2012
camel-man
Scanf("%d",&n);
int *variable=malloc(n*sizeof(int));
• 10-06-2012
Lina_inverse
the "variable" is the size you assigned to this array correct?
• 10-06-2012
Lina_inverse
int *variable is just the pointer, pointed to that malloc, nvm
• 10-06-2012
camel-man
Variable is your new allocated array.
• 10-06-2012
std10093
Quote:

Originally Posted by camel-man
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 :)
• 10-06-2012
Lina_inverse
can i still use the same logic you posted above, now that the array is malloc? std10093?
• 10-06-2012
std10093
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))
• 10-06-2012
camel-man
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.
• 10-06-2012
std10093
You should hit the enter after typing every input (or you have to change some got in order to read a whole line)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last