0

Problem is : Write a function that as an input argument receives a three-digit positive number and as a result has to get the sum between the largest and the smallest number obtained by the same 3 digits divided by the median digit. Example: input argument to function 438 The largest with the same digits is 843, the smallest is 348, so it should be calculated (843 + 348) / 4.

I have tried it and got the result ok but my code seems to complicated so iam asking is there a better way to do it?

Thanks in advance

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

int check(int x) {

    int a, b, c, biggestNum, smallestNum, medianNum;

    a = x / 100;
    b = (x / 10) % 10;
    c = x % 10;

    if (a > b && a > c && b > c) {

        biggestNum= a * 100 + b * 10 + c;
        smallestNum= c * 100 + b * 10 + a;
        medianNum= b;

    }
    else if (a > b && a > c && b < c) {

        biggestNum= a * 100 + c * 10 + b;
        smallestNum= b * 100 + c * 10 + a;
        medianNum= c;

    }
    else if (b > a && b > c && a < c) {

        biggestNum= b * 100 + c * 10 + a;
        smallestNum= a * 100 + c * 10 + b;
        medianNum= c;

    }
    else if (b > a && b > c && a > c) {

        biggestNum= b * 100 + a * 10 + c;
        smallestNum= c * 100 + a * 10 + b;
        medianNum= a;

    }
    else if (c > a && c > b && a > b) {

        biggestNum= c * 100 + a * 10 + b;
        smallestNum= b * 100 + a * 10 + c;
        medianNum= a;

    }
    else if (c > a && c > b && a < b) {

        biggestNum= c * 100 + b * 10 + a;
        smallestNum= a * 100 + b * 10 + c;
        medianNum= b;

    }   

    cout << "Smallest number is: " << smallestNum<< " ,biggest is: " << biggestNum << " and median is: " << medianNum<< "." << endl;

    return (biggestNum + smallestNum) / medianNum;
}


int main() {

    cout << "Enter one 3 digit positive number: ";
    int x;
    cin >> x;

    float result = check(x);
    cout << "The result is: " << result << "." << endl;

    system("pause");
    return 0;
}
  • 1
    I'd consider rearranging the values in `a`, `b`, and `c` so that `a` has the largest digit, `b` has the next largest digit, and `c` has the smallest digit. Then the three final values can be calculated directly. – Pete Becker Nov 17 '19 at 17:14

4 Answers4

0

The posted code can't really produce the right answer, considering that the result is calculated with integer arithmetic:

int check(int x)  // <- note the type of the returned value
{
    int biggestNum, smallestNum, medianNum;
    // ...
    return (biggestNum + smallestNum) / medianNum;  // <- This is an integer division
}

int main()
{
    int x; 
    // ...   
    float result = check(x);  // Now it's too late to get the right result
}

The logic also doesn't consider all the possible cases, as a matter of fact it ignores duplicated digits and the big if else if construct, lacking a default branch (a final unconditioned else), leaves those uninitialized variables undetermined so that the following operation gives a meaningless result.

Given the assignment restrictions, I'd write something like the following

#include <utility>

// The assignment is about 3-digit numbers, you should check that x is actually in
// the range [100, 999]. Note that one of the extremes is a special case.
// Well, both, actually.
double I_ve_no_idea_how_to_name_this(int x)
{
    constexpr int base = 10;
    int smallest = x % base;
    x /= base;
    int median = x % base;
    x /= base; 
    // Note that this "works" (extracting the third digit) even if
    // x isn't a 3-digit number. If you can assure the input is well
    // defined, you can simplify this.
    int biggest = x % base; 

    // Now we can sort the previous variables.
    using std::swap;
    if ( median < smallest ) {
        swap(median, smallest);
    }
    // Now I know that smallest <= median
    if ( biggest < median ) {
        swap(biggest, median);
    }
    // Now I know that median <= biggest
    // ...
    // Is that enough or am I missing something here?
    // Please think about it before running the code and test it.

    // Once the variables are sorted, the result is easily calculated
    return (biggest + smallest + base * (2 * median + base * (biggest + smallest)))
           / static_cast<double>(median);
}
Bob__
  • 12,361
  • 3
  • 28
  • 42
  • Thank you, I started this year as a part time CS student and the first programming languages that we started learning is C++ and html, is this normal with other Universities or .. i feel like we were better off learning JS or Python as a first language but that's just my opinion :) – Ljupcho Jovanov Nov 20 '19 at 08:03
0

try this...

int check(int x) {
    int a,b,c,temp;
    a = x/100;
    b = (x/10)%10;
    c = x%10;
    if(b>a){
        temp=a;
        a=b;
        b=temp;
    }
    if(c>b){
        temp=b;
        b=c;
        c=temp;
    }
    if(b>a){
        temp=a;
        a=b;
        b=temp;
    }
    cout << "smallest: " << a+(b*10)+(c*100) << "\n";
    cout << "biggest: " << (a*100)+(b*10)+c << "\n";
    cout << "median: " << b << "\n";
    return (((a+c)*100)+(2*b*10)+(a+c))/b;
}
ichikuma
  • 23
  • 3
0

First, you should use more descriptive variable names and should initialize each variable on definition. These two steps help greatly in squashing bugs in complex programs. I know this one isn't complex, but it's a good habit to have. Second, the standard library can help with finding the largest and smallest digit, which then makes the rest simple. So here's an example without any if statements.

Finally, using namespace std; is a bad practice and should be avoided.

double check(int x)
{
    int a = x / 100;
    int b = (x / 10) % 10;
    int c = x % 10;
    int bigdigit = std::max({ a, b, c }); // find largest
    int smalldigit = std::min({ a, b, c }); //find smallest
    int middledigit = a + b + c - bigdigit - smalldigit; // sum of all digits minus largest and smallest gives the remaining one
    int biggest = smalldigit + middledigit * 10 + bigdigit * 100;
    int smallest = smalldigit * 100 + middledigit * 10 + bigdigit;
    std::cout << "biggest: " << biggest << '\n';
    std::cout << "smallest: " << smallest << '\n';
    std::cout << "median: " << middledigit << '\n';
    return (1.0 * biggest + 1.0 * smallest) / (1.0 * middledigit); --using double instead of int, as result could be fractional
}
rsjaffe
  • 5,600
  • 7
  • 27
  • 39
0

check this check function.

int check(int x) {

    if(x >= 1000) x %= 1000;  //or return -1;

    //get digits
    int M = x/100;
    int C = (x/10)%10;
    int m = x%10;

    //unrolled bubble sort.
    if(M < C) swap(M,C);
    if(C < m) swap(C,m);
    if(M < C) swap(M,C);

    //simplified formula
    return ((m+M)*(101))/C + 20;

}

//derivation of formula
B = M*100 + C*10 + m;
s = m*100 + C*10 + M;
B+s = (m+M)*100 + C*20 + (m+M)
    = (m+M)*(100 + 1) + C*20
(B+s)/C = ((m+M)*(100 + 1) + C*20)/C
        = ((m+M)*(101))/C + 20
acegs
  • 2,621
  • 1
  • 22
  • 31