Write the following functions in Scheme 1a digitinc4 takes a

Write the following functions in Scheme:

1.a \"digitinc4\" -takes as input a 4-digit integer, and returns another 4-digit integer constructed of the original input integer\'s digits, each incremented. For example, (digitinc4 4833) would return 5944. When a digit is a 9, the corresponding output digit should be a 0. For example:
(digitinc4 4892) would return 5903. You can assume that the input number is at least 1000 - that is, there are no leading zeros.

b. Extend your answer in problem (a), above, to handle input integers with an arbitrary number of digits. This function should be named \"digitinc\" (without the \"4\" in the name). For example:
(digitinc 83) would return 94, and (digitinc 22897) would return 33908.

Solution

Below is the complete working C++ program that includes both the functions of parts (a) and (b) with appropriate comments:

#include <iostream>
#include <cmath>

using namespace std;
int digitinc4(int inputc4){
     int outputc4,digits=4,i=1,outDigit;
     while (i <= digits){
         outDigit = (inputc4 % 10);
         if(outDigit == 9)
         outDigit = 0;
         else
         outDigit++;
         if(i==1)
         outputc4 = outDigit;
         else
         outputc4 += pow(10,i-1)*outDigit;
         inputc4 /= 10;
         i++;
     }
     return outputc4;
}

int digitinc(int inputc){
     int outputc,digits=0,i=1,outDigit, num = inputc;
     do {
         ++digits; //find number of digits
         num /= 10;
        } while (num);
    cout <<\"\ number of digits: \"<<digits;
   
     while (i <= digits){
         outDigit = (inputc % 10); //get the last digit at this stage
         if(outDigit == 9)
         outDigit = 0;
         else
         outDigit++;
         if(i==1)
         outputc = outDigit;
         else
         outputc += pow(10,i-1)*outDigit; calculate the output number at this stage
         inputc /= 10; //trim the last digit
         i++;
     }
     return outputc;
}

int main()
{
    int inputc,inputc4,outputc,outputc4, flag = 0,choice;
    cout << \"\ MENU:\ 1. Input a 4 digit number\ 2. Input any other number\ ENTER YOUR CHOICE: \" << endl;
    cin >> choice;
    switch(choice){
        case 1:
            do{
            cout << \"\ Enter 4 digit number: \" << endl;
            cin >> inputc4;
            if (inputc4 < 1000 || inputc4 > 9999)
                cout << \"Invalid input! Please input a valid 4 digit number!\";
                else
                flag = 1;
            } while (flag != 1);
            outputc4 = digitinc4(inputc4);
            cout << \"The output is: \" << outputc4 << endl;
            break;
        case 2:
            cout << \"\ Enter your number: \" << endl;
            cin >> inputc;
            outputc = digitinc(inputc);
            cout << \"The output is: \" << outputc << endl;
            break;
    }
}

Write the following functions in Scheme: 1.a \
Write the following functions in Scheme: 1.a \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site