QUESTION:
Write a recursive function that parses the elements of a vector and returns one of the following codes:
0 Disordered elements
1 Items sorted in ascending order
2 Constant elements
3 Items ordered in descending order
int AnalisaVetor(int V[], int n);
Use the above prototype where "n" indicates the number of elements present in the array.
Examples:
Para V={1,2,2,5,6,6,7,8,8,9} retorna código 1.
Para V={20,15,11,10,8,8,5,2} retorna código 3.
Para V={1,20,2,5,6,6,7,80,9} retorna código 0.
Para V={8,8,8,8,8,8,8,8,8,8} retorna código 2.
Attempting to resolve the issue I came up with the following code:
int analisa(int v[], int n)
{
if(v[n-1] == v[n-2])
{
if(n == 2)
{
return 2;
}
else
{
return analisa(v, n-1);
}
}
else if(v[n-1] < v[n-2])
{
if(n == 2)
{
return 3;
}
else
{
return analisa(v, n-1);
}
}
else if(v[n-1] > v[n-2])
{
if(n==2)
{
return 1;
}
else
{
return analisa(v, n-1);
}
}
return 0;
}
However, it does not work as expected when, for example, the first two elements of the vector are the same and the rest follow a growing or decreasing sequence. Another case in which there is no expected operation is when the vector is disordered. How can I fix these errors?