Answer to Question #185290 in C++ for Nethu

Question #185290

Write a function called Findoutput. The function takes a 4 digits integer as parameter validates the integer and returns 0 when the digits are in increasing order or 1 when the digits are in decreasing order or 99 when the digit are not in any order


1
Expert's answer
2021-04-25T03:23:58-0400
#include <iostream>
#include <math.h>
#include <algorithm>
using namespace std;
int findOutput(int x){
    int arr[4], sorted[4];
    if(x > 9999 || x < 1000){
        cout<<"Not a 4 digit integer\n";
        return -1;
    }
    for(int i = 3,j = 0; i >= 0; i--, j++){
        arr[j] = x / (int)pow(10, i);
        sorted[j] = x / (int)pow(10, i);
        x -= arr[j] * (int)pow(10, i);
    }
    sort(sorted, sorted + 4);
    bool flag1 = true, flag2 = true;
    for(int i = 0;i < 4; i++){
        if(sorted[i] != arr[i]) flag1 = false;
    }
    for(int i = 3, j = 0; i >= 0, j < 4; i--, j++){
        if(sorted[i] != arr[j]) flag2 = false;
    }
    if(!flag1 && !flag2) return 99;
    if(flag1) return 0;
    else return 1;
}
int main(){
    int x;
    cout<<"Input a 4 digit integer: ";
    cin>>x;
    cout<<findOutput(x);
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS