This is the prompt and how I solved it:
Program a function that receives a vector v of integers, and its length n, and returns 1 if it is sorted (either increasing or decreasing), and 0 otherwise.
Example:
{1,1,2}
it's sorted ascending and returns 1,{6,4}
it's sorted descending and returns 1,{1,3,2}
it's unordered and returns 0.
I would like to know if with this code the slogan is solved:
int ordenado(int v[], int n){
//Escribir aca el codigo de la funcion ordenado
int i, ordenado;
ordenado= v[0];
for (i=0;i<n;i++){
if(v[i] < ordenado && v[i] > ordenado )
return 1;
else
return 0;
}
return 0;
}
I find several errors in your code:
This is the solution that I would propose:
First, the first position of the vector is compared to see if we will apply the ascending, descending or unordered procedure. Then we are comparing position 1 vs. position 2 in a loop of duration "n".
I remain on the lookout for any questions, greetings!
Recapping the main issues mentioned in the comments:
I suggest you focus on checking one order at a time. You can make a function that checks if it's sorted ascending and another one that checks if it's sorted descending.
Then, you can choose what to do based on the result of executing both functions with the same array.
For example:
I find it convenient to demonstrate the order by logical opposition because it is simpler:
You can try the code here .