CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 15
  1. #1
    Join Date
    Sep 2009
    Posts
    34

    Reversing a Number

    Hi,
    I'm trying to create a program that takes two numbers, finds their average showing decimals, and then finds the reverse value of the first 4 non-decimal digits of the average. I'm writing the program in Emacs on a Linux machine.

    When I try to compile my code in G++, I get an error. Where am I going wrong? I can't figure out how to debug using keyboard commands in Emacs, so I'm not 100% sure where the problem is.


    #include <iostream>
    using namespace std;
    int main ()
    {
    int num1;
    int num2;
    int num3;
    cout << "Please enter two positive integers with at most four digits each: ";
    cin >> num1 >> num2; // User inputs numbers here
    cout << "Their average is " << num3 = (num1 * num2) / 2.0; // Shows the average with decimal places
    int mod1;
    int rev1;
    rev1 = 0
    {
    mod1 = num3 % 1000; // Show the reverse average to 4 digits?
    num3 = num3 / 1000;
    rev1 = (rev1 * 1000) + mod1;
    }
    cout << "The average reversed is " << rev1;
    }
    Last edited by AllYourBase; September 29th, 2009 at 09:18 PM.

  2. #2
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Reversing a Number

    Your problem lies there:

    rev1 = 0
    {
    mod1 = num3 % 1000; // Show the reverse average to 4 digits?
    num3 = num3 / 1000;
    rev1 = (rev1 * 1000) + mod1;
    }

    you need to use some loop or something. maybe recursion if you like egzotic

    use this code

    Code:
           int n=1233345, m;
    
          while(n>0)
          {
    
             m = m * 10;
             m += n%10;
             n /= 10;
          }
    the m is you reverse number
    Share and always try to give back more.

  3. #3
    Join Date
    Sep 2009
    Posts
    34

    Re: Reversing a Number

    Hey, thanks for the reply.
    Is there any way to do it without that kind of a loop? We're supposed to only use concepts we've learned in class, and we haven't learned the "while" function yet.

    Also, would that solution show only 4 digits of the reversed number? I figured that's what using 1000 instead of 10 would do.

  4. #4
    Join Date
    Dec 2007
    Location
    Lithuania
    Posts
    98

    Re: Reversing a Number

    There is, but only for a fixed number size.

    repeat for times
    Code:
    m *= 10;
    m += n%10;
    n /= 10;
    But I dont think that teacher will be mad if you use while loop
    Share and always try to give back more.

  5. #5
    Join Date
    Sep 2009
    Posts
    34

    Re: Reversing a Number

    I tried the code you mentioned, but the program just won't compile.
    Does anyone know where I am going wrong in this, so that it won't compile? I'd debug it, but apparently to debug in emacs you have to compile first.

    #include <iostream>
    using namespace std;
    int main ()
    {
    int num1;
    int num2;
    int num3;
    cout << "Please enter two positive integers with at most four digits each: ";
    cin >> num1 >> num2;
    cout << "Their average is " << num3 = (num1 * num2) / 2.0;
    int rev1;
    rev1 = 0
    {
    num3 * = 10;
    num3 += rev1&#37;10;
    rev1 /= 10;
    }
    cout << "The average reversed is " << rev1;
    }
    Last edited by AllYourBase; September 29th, 2009 at 09:17 PM.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reversing a Number

    I have no idea if emacs allows debugging. Mostly I just use gdb for debugging on linux.

    Anyway, tell us what the compile error is and which line number it's pointing at. (You can jump to a line number in Emacs using "M-x goto-line <n>".)

  7. #7
    Join Date
    Sep 2009
    Posts
    34

    Re: Reversing a Number

    We have to use g++ to compile our programs. This is what g++ says when I try to compile:

    reverseNum.cc:10: error: no match for &#226;operator=&#226; in &#226;((std::basic_ostream<char, std::char_traits<char> >*)std:perator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"Their average is ")))->std::basic_ostream<_CharT, _Traits>:perator<< [with _CharT = char, _Traits = std::char_traits<char>](num3) = (#&#226;float_expr&#226; not supported by dump_expr#<expression error> / 2.0e+0)&#226;
    /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/iosfwd:64: note: candidates are: std::basic_ostream<char, std::char_traits<char> >& std::basic_ostream<char, std::char_traits<char> >:perator=(const std::basic_ostream<char, std::char_traits<char> >&)
    reverseNum.cc:13: error: expected `;' before &#226;{&#226; token

    Is that any kind of help at all? I'm assuming that this doesn't actually tell you anything.

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reversing a Number

    There's a bit of a trick to understanding compile errors, and template compile errors like that one can be trickier than most since they tend to be very verbose and you have to learn what's actually useful and what you can ignore. But there is useful information there.

    On line 10, it's confused about the use of an '=' operator. I assume it means this:
    Code:
    cout << "Their average is " << num3 = (num1 * num2) / 2.0;
    Probably an operator precedence problem----it doesn't realize that you meant to *first* do the assignment, then do the output. You could fix that by adding parentheses, but really it's probably best to do the assignment on a separate line for clarity.

    On line 13, it's complaing about a missing ;. Often errors point to the line *after* where the actual problem is, so let's look back----oh, look, line 12 is indeed missing a semicolon.

    EDIT: Oh, and that's not the formula for an average.

  9. #9
    Join Date
    Sep 2009
    Posts
    34

    Re: Reversing a Number

    Thanks for catching that, I mixed up my plus sign and * sign. It's obviously been a long day.

    Alright, so I dug through it and got it to compile, but there's something wrong with the equation to get the reversed number. I'm using the equations ulumunu mentioned, but the output for the reversed number always ends up being zero. Any idea why?

    #include <iostream>
    using namespace std;
    int main ()
    {
    int num1;
    int num2;
    int num3;
    cout << "Please enter two positive integers with at most four digits each: ";
    cin >> num1 >> num2;
    num3 = (num1 + num2) /2;
    cout << "Their average is " << (num1 + num2 / 2.0) << endl;
    int rev1;
    rev1 = 0;
    {
    num3 *= 10; //To get the one digit?
    num3 += rev1%10;
    rev1 /= 10;
    num3 *= 10; // To get the ten digit?
    num3 += rev1%10;
    rev1 /= 10;
    num3 *= 10; // To get the hundred digit?
    num3 += rev1%10;
    rev1 /= 10;
    num3 *= 10; // To get the thousand digit?
    num3 += rev1%10;
    rev1 /= 10;
    }
    cout << "The average reversed is " << rev1;
    }

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reversing a Number

    Indeed, that certainly won't work as written. Let me lay out the thinking behind what those are supposed to say and then maybe you'll be able to figure out how to fix it.

    n &#37; 10 is "n modulus 10", which if you know anything about the modulus operation, clearly means that the expression is equal to the ones digit of n.

    n /= 10 does integer division by 10---that is, it gives you the whole part of the quotient, dropping any fractional part. Thus it essentially slides over every digit by one place.

    n *= 10 does the opposite---it moves every digit over one place the other way, leaving a 0 in the ones place.

    So the algorithm goes:
    Code:
    output <- 0
    WHILE input > 0
        ADD the ones digit of input to output
        BUMP every digit of the output one place to the left
        BMMP every digit of the input one place to the right
    END WHILE
    Of course, you don't have to do this in a loop if you don't want to. You can simply unroll it and repeat the instructions 4 times.

  11. #11
    Join Date
    Sep 2009
    Posts
    34

    Re: Reversing a Number

    I can't seem to figure out how to "ADD the ones digit of input to output"... what's wrong with the logic I am using below?
    Thanks for all the help so far.

    #include <iostream>
    using namespace std;
    int main ()
    {
    int num1;
    int num2;
    int num3;
    cout << "Please enter two positive integers with at most four digits each: ";
    cin >> num1 >> num2;
    num3 = (num1 + num2) /2;
    cout << "Their average is " << (num1 + num2 / 2.0) << endl;
    int rev1;
    rev1 = 0;
    {
    rev1 += (num3&#37;10); // Is this where I'm going wrong?
    rev1 *= 10;
    num3 /= 10;
    }
    cout << "Four Digits of the average reversed is " << rev1;
    }
    Last edited by AllYourBase; September 29th, 2009 at 11:17 PM.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Reversing a Number

    That's good as far as it goes, but it will of course only shift one digit. So for instance, 1234 would result in an output of 40. That should be simple enough to confirm.

  13. #13
    Join Date
    Sep 2009
    Posts
    34

    Re: Reversing a Number

    Ah, thank you for your help. I've got it now.

  14. #14
    Join Date
    Apr 2008
    Posts
    725

    Re: Reversing a Number

    don't you need to return an int for it to compile?

    eg:
    Code:
    int main()
    {
    
    
    return 0;
    }

  15. #15
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Reversing a Number

    'main' is a special function. IIRC, if there is no return stmt, the function will automatically return '0'.

    Viggy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured