write a function called findsequence.
the function takes a 4-digit integer as parameter, validates and returns 1 when the digits are in increasing order or 2 when the digits are in decreasing order or 0 when the digits are not in any order.
#include <iostream>
int findsequence(unsigned v)
{
int d0 = v % 10; v /= 10;
int d1 = v % 10; v /= 10;
int d2 = v % 10; v /= 10;
int d3 = v % 10; v /= 10;
if(d3 >= d2 && d2 >= d1 && d1 >= d0)
{
return 2;
}
else
if(d3 <= d2 && d2 <= d1 && d1 <= d0)
{
return 1;
}
return 0;
}
int main()
{
std::cout << "Enter 4-digit integer: ";
unsigned value;
std::cin >> value;
if(!std::cin || value > 9999 || value < 1000)
{
std::cout << "Bad input!\n";
return 1;
}
std::cout << "findsequence(" << value << ") = " << findsequence(value) << "\n";
return 0;
}
Comments
Leave a comment