An interesting C++ program problem

Discussion in 'OT Technology' started by Tornado Rex, Sep 19, 2006.

  1. Tornado Rex

    Tornado Rex New Member

    Joined:
    Jul 11, 2006
    Messages:
    8
    Likes Received:
    0
    Alright, so I got this assignment from my instructor. I'll type it out as he wrote it.
    An example output would be:
    Code:
    enter 2 3-digit numbers:
    987 567
    the input numbers are: 987 567
    the digits of the 1st number are: 9, 8, 7
    the digits of the 2nd number are: 5, 6, 7
    the result is 1554
    Now here's the catch. You can't use if statements, arrays, loops, etc. Basically you just have to use math and math functions. I personally couldn't get it. I began ripping my hair out trying to figure it out and said f*ck it and used if statements. Below is the code for both using if statements and the one where I got stuck.

    Anyway, I just figured some of you coders out there might like to try this as it really gets your brain thinking (unless I'm totally missing something).

    Without further ado, the code:


    No if statements:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
        int num1, num2, a1, a2, a3, b1, b2, b3, r1, r2, r3, r4;
    
        cout << "Please input two 3-digit numbers: ";
        cin >> num1 >> num2;
    
        a1 = num1 / 100;
        a2 = (num1 - (a1 * 100)) / 10;
        a3 = (num1 - ((a1 * 100) + (a2 * 10)));
    
        b1 = num2 / 100;
        b2 = (num2 - (b1 * 100)) / 10;
        b3 = (num2 - ((b1 * 100) + (b2 * 10)));
    
        r4 = (a3 + b3) - 10;
        r3 = (((a3 + b3) - r4) - 10) + ((a2 + b2));
    
        cout << a1 << ", " << a2 << ", " << a3 << endl;
        cout << b1 << ", " << b2 << ", " << b3 << endl;
        cout << r4 << ", " << r3 << endl;
    
        return 0;}
    if statements:
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
        int num1, num2, a1, a2, a3, b1, b2, b3, r1, r2, r3, r4;
        r1 = 0;
        r2 = 0;
        r3 = 0;
        r4 = 0;
    
        cout << "Please input two 3-digit numbers: ";
        cin >> num1 >> num2;
    
        a1 = num1 / 100;
        a2 = (num1 - (a1 * 100)) / 10;
        a3 = (num1 - ((a1 * 100) + (a2 * 10)));
    
        b1 = num2 / 100;
        b2 = (num2 - (b1 * 100)) / 10;
        b3 = (num2 - ((b1 * 100) + (b2 * 10)));
    
        r4 = a3 + b3;
        if (r4 > 10) {
      r4 -= 10;
      r3 += 1;}
        r3 += a2 + b2;
        if (r3 > 10) {
      r3 -= 10;
      r2 += 1;}
        r2 += a1 + b1;
        if (r2 > 10) {
      r2 -= 10;
      r1 = 1;}
    
        cout << a1 << ", " << a2 << ", " << a3 << endl;
        cout << b1 << ", " << b2 << ", " << b3 << endl;
        if (r1)
      cout << r1 << r2 << r3 << r4 << endl;
        else
      cout << r2 << r3 << r4 << endl;
    
        return 0;}
     
  2. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    The solution is more simple if you use the modulo operater (%).
     
  3. Tornado Rex

    Tornado Rex New Member

    Joined:
    Jul 11, 2006
    Messages:
    8
    Likes Received:
    0
    Maybe I'm missing some simple logic here (looking at it to complex), but the problem I'm running into is I can't figure out how to do it without the if statements.

    The way I'm thinking is it's an either/or situation. Either you add 1 to the next number or don't. How would the modulus help me in that respect?
     
  4. GOGZILLA

    GOGZILLA Double-Uranium Member

    Joined:
    Jan 16, 2003
    Messages:
    10,760
    Likes Received:
    3
    Location:
    Plantation, FL
    Code:
    #include <iostream>
    using namespace std;
    
    int main() {
    
        int num1, num2, a1, a2, a3, b1, b2, b3, r1, r2, r3, r4;
    
        cout << "Please input two 3-digit numbers: ";
        cin >> num1 >> num2;
    
        a1 = num1 / 100;
        a2 = (num1 - (a1 * 100)) / 10;
        a3 = (num1 - ((a1 * 100) + (a2 * 10)));
    
        b1 = num2 / 100;
        b2 = (num2 - (b1 * 100)) / 10;
        b3 = (num2 - ((b1 * 100) + (b2 * 10)));
    
        
        r1 = (a3 + b3);
        r2 = r1/10;
        r1 = r1%10;
    
        r2 += a2+b2;
        r3 = r2/10;
        r2 = r2%10;
    
        r3 += a3+b3;
        r4 = r3/10;
        r3 = r3%10;
    
    
    
        cout << a1 << ", " << a2 << ", " << a3 << endl;
        cout << b1 << ", " << b2 << ", " << b3 << endl;
        cout << r4 << r3 << r2 << r1 << endl;
    
        return 0;}
    
     
    Last edited: Sep 20, 2006
  5. tokyonights

    tokyonights Believe in me, who believes in you OT Supporter

    Joined:
    Mar 1, 2003
    Messages:
    10,144
    Likes Received:
    14
    Location:
    Chicago
    you sort of have the right idea. In fact if you look back at where you assigned each individual integer you should get a clue, but this might help you get started

    Code:
    r3 = (((a3 + b3) %100)/10) + (a2 + b2)%10;
    Let me know if you still need help


    ps. I did the one without the if statements
     
  6. skinjob

    skinjob Active Member

    Joined:
    Jan 6, 2001
    Messages:
    2,337
    Likes Received:
    0
    Location:
    Aztlán
    r1 = (a1 + b1) % 10;
    c = (a1 + b1) / 10;
    r2 = (a2 + b2 + c) % 10;
    c = (a2 + b2 + c) / 10;
    r3 = (a3 + b3 + c) % 10;
    r4 = (a3 + b3 + c) / 10;

    Correct, you either add 1 or 0 to the next addition. You don't have to check whether c is 1 or 0. Just compute c and add it.
     
  7. Tornado Rex

    Tornado Rex New Member

    Joined:
    Jul 11, 2006
    Messages:
    8
    Likes Received:
    0
    Thank you very much for the help. I was looking at it far too complicated. Now I see how easy it was using the modulus operator.

    The problem wasn't that I couldn't do it. I was having trouble grasping the concept of doing it without using anything more advanced than just simple math expressions.
     

Share This Page