samedi 6 novembre 2021

What condition( if statements) should I make to display non-monotonous for the string of number ? C++

I am looking for a hint to find out strictly the condition for non monotonous. I would not want all the code for this problem. It's my first post here and I'm a beginner and I want to better understand the while and the conditions. Please be lenient.

The string of numbers ends with the value 0, where 0 is not part of the heights (only marks the end of the string). Specify whether the range of heights is ascending, descending, or non-monotonous. Input Output 1 2 5 5 10 11 0 ascending 16 7 3 0 descending 1 2 2 1 0 non-monotonous

Solving as much as possible with while and if and int. Restrictions and clarifications:

  • 0 is not part of the string of heights, only marks the end of the string
  • heights are numbers between 0 and 10,000
  • for simplicity if all numbers are equal or there is no value before 0, the string is considered non-monotonous

How I tried to solve it : int main(){

int   b ,  a   , ascending = 0  , counter = 0 , descending = 0 ;

   cin >> a;
 while( a != 0 ){
    cin >> b;
     ++counter;
if( a && b != 0 ){

    if( a <  b ){
     ++ascending;
    }
    else if(a > b ){
     ++descending;
    }
    }
       a = b;
}
 --counter;
 if(ascending == counter  ){
     cout << "\n ascending";
 }
 else if ( descending > 2 ){
     cout << " non-monotonous ";
 }

 if(descending == counter){
     cout << "\n descending ";
 }
 else if ( ascending > 2 ){
    cout << " non-monotonous ";
 }

Aucun commentaire:

Enregistrer un commentaire