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

1. ## 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. This must be true
if ( array[i] <= array[i+1] )

For all elements of the array 3. 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;

scanf("%d",&input);
array = 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. Thanks for the help, how do i now do this with malloc? instead of a fixed n (n=6) 5. 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. How do I ask for the size of the array n from the user and dynamically allocate memory for that size of array? 7. Scanf("%d",&n);
int *variable=malloc(n*sizeof(int)); 8. the "variable" is the size you assigned to this array correct? 9. int *variable is just the pointer, pointed to that malloc, nvm 10. Variable is your new allocated array. 11. 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  12. can i still use the same logic you posted above, now that the array is malloc? std10093? 13. 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)) 14. 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. 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 