it is producing output as 00000 when giving output like
5
1
2
3
4
5
where i am getting wrong in the code?
it is producing output as 00000 when giving output like
5
1
2
3
4
5
where i am getting wrong in the code?
hello @lovely
u r priniting isdecreasing on every ieteration and because it contains false ,everytime it is printing 0.
u forgot to update ur variables during iteration,pls correct it.
bool increasingDecreasing(int n)
{
int prev;
cin >> prev;
bool isValid = true;
bool isDecreasing = true;
while (--n)
{
int curr;
cin >> curr;
if (curr == prev)
{
isValid = false;
break;
}
else if (curr > prev)
{
isDecreasing = false;
}
else if (!isDecreasing && curr < prev)
{
isValid = false;
break;
}
prev = curr;
}
return isValid;
}
```
bool increasingDecreasing(int n)
what does this line mean ?we have not studied function till now,
else if (!isDecreasing && curr < prev)
and also what does this line mean how can same number is smaller than previousand also not decreasing?
and is it necessary to use two bool variable is valid and is decreasing ?can we not use only single bool variable for all conditions?
ok then ignore first line,it is just taking n from main.
About the input, you need not to take array, one can also perform the question without using array. As every time we just check two consecutive numbers to tell whether the seqeunce is increasing or decreasing, so why should we store all of the numbers.
If a sequence does not meet any of the above criteria , we return “false” for it.
Implementation : First we assume that our sequence is decreasing only. We also assume that our sequence is initially valid and only try to disprove it during the course.
We compare our current element with the previous element and based on that comparison make our decision.
If the curr element is equal to the prev element , we see that the sequence is neither strictly increasing or decreasing so we mark our sequence as invalid.
If the curr element is greater than the prev element, then we realise that the sequence has started increasing and we mark it so using a flag variable “goingUp” which we had initially marked as false based on our assumption that the sequence is decreasing.
If the curr element is less than the previous element and the current sequence is also proceeding as decreasing , we are to do nothing. However if the current sequence is increasing and we get a pair for a decreasing sequence then we mark our sequence as invalid since we cannot have a decreasing part after the increasing sequence.
After each iteration , store the value of the curr element into the prev element so that we can compare it with the next input element.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.