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[]
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[]
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.
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
Hope this helpsCode:#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; }
Thanks for the help, how do i now do this with malloc? instead of a fixed n (n=6)
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?
How do I ask for the size of the array n from the user and dynamically allocate memory for that size of array?
Scanf("%d",&n);
int *variable=malloc(n*sizeof(int));
the "variable" is the size you assigned to this array correct?
int *variable is just the pointer, pointed to that malloc, nvm
Variable is your new allocated array.
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
can i still use the same logic you posted above, now that the array is malloc? 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))
Last edited by std10093; 10-06-2012 at 02:46 PM.
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.
You should hit the enter after typing every input (or you have to change some got in order to read a whole line)