Reverse number program
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Reverse number program

  1. #1
    Join Date
    Sep 2004
    Posts
    45

    Reverse number program

    I need a lil help. I'm new to C++ and, I have to write a function that takes an integer as a perameter and returns the number with its digits reversed. ex. 5678
    would return 8765. I also have to use at least 2 more functions to read and print the result. I need a little nudge in the right direction. This is what I have so far, and I'm 90% sure it is completely wrong! lol


    #include<iostream>

    using namespace std;

    int reverseDigit(int num)
    void print(int num)
    int read();

    int main()
    {
    int num= read();
    print(int(num));
    return 0;
    }
    int read()
    {
    int a
    cout<<"Enter a number"<<endl;
    cin>>num;
    return a;
    }

    int reverseDigit(int num)
    {
    int
    while(num>0)
    {
    num = num % 10; //// This is where I know I'm waaaay off!
    num = num / 10;
    num= num - 2;
    }
    }


    return 0;

  2. #2
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Reverse number program

    I suggest converting the number to an std::string using the stringstream classes, reversing the order of characters in the string using a simple loop, then converting the string back into numeric form. If you haven't worked with strings or stringstreams yet, post back and I will help you write some code that would do this.

  3. #3
    Join Date
    Sep 2004
    Posts
    45

    Re: Reverse number program

    I've never used those before. I've just started getting into predefined and user defined functions, and for the program I can only use those types of functions.

  4. #4
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Reverse number program

    I've never used those before. I've just started getting into predefined and user defined functions, and for the program I can only use those types of functions.
    I've yet to hear of functions that are neither predefined nor user-defined...

    Anyways, an example code would look like this

    Code:
    #include <iostream>
    #include <string>        // for std::string
    #include <sstream>    // for stringstreams
    using namespace std;
    
    string number_to_string(int number);
    int string_to_number(string str);
    string reverse_string(string str);
    
    int main()
    {
        int num = 5678;
        string str = number_to_string(num);
        string reversed_str = reverse_string(str);
        int new_number = string_to_number(reversed_str);
        
        return 0;
    }
    
    string number_to_string(int number)
    {
        string result;
        ostringstream ostr;
        ostr << number;
        result = ostr.str();
        return result;
    }
    
    int string_to_number(string str)
    {
        int result;
        istringstream istr(str);
        istr >> result;
        return result;
    }
    
    string reverse_string(string str)
    {
         int i, j;
         char temp;
         for (int j = 0; i < str.size(); j < i; i--, j++)
         {
              temp = str[i];
              str[i] = str[j];
              str[j] = temp;
          }
          return str; 
    }

  5. #5
    Join Date
    Sep 2004
    Posts
    45

    Re: Reverse number program

    Quote Originally Posted by soopa1
    I've never used those before. I've just started getting into predefined and user defined functions, and for the program I can only use those types of functions.

    Yeah..I realized that after I typed it..lol


    The thing is I can't use sstream and stringstream,because we have not done those in class. I can kinda understand where you are coming from though.

  6. #6
    Join Date
    Sep 2004
    Posts
    45

    Re: Reverse number program

    Let me post the full question HighCommander4.

    Write a function, reverseDigit, that takes an integer as a perameter and returns the number with its digits reversed. For example, the value of reverseDigit(12345) is 54321. You should use 2 more functions to read and print the result.

    I think my main probelm is this section of my code:

    int reverseDigit(int num)
    {
    int val
    while(num>0)
    {
    num = num % 10; // missing some steps
    num = num / 10;
    num= num - 2;
    }



    return val;
    }

    void out(int num)
    {

    cout<<num<<endl;
    }

  7. #7
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Reverse number program

    You need to keep a sum in your function. Right now, you are destroying your original number right at the very first iteration of the loop (use a debugger to see the steps that are happening when you call your reverseDigits function).

    If you don't know how to use a debugger, now is the time to learn.

    First, look at the following for 8765:

    (Iteration 1)
    0 * 10 + 5 = 5

    (Iteration 2)
    5 * 10 + 6 = 56

    (Iteration 3)
    56 * 10 + 7 = 567

    (Iteration 4)
    567 * 10 + 8 = 5678

    return 5678

    Do you see the pattern? The first number in the equations above is the "current sum". It always starts at 0 and is accumulated in the while loop. Each "subanswer" is the new sum to be used in the next equation.

    I didn't write the while loop, but hopefully you see what is going on to do this correctly. You were somewhat on the right track with the modulus operator, but you needed to do a little bit more work to get the correct calculations for the number.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; November 2nd, 2004 at 05:08 AM.

  8. #8
    Join Date
    Jun 2001
    Location
    Orlando, FL
    Posts
    232

    Re: Reverse number program

    Code:
    #include <iostream>
    using namespace std;
    
    // Prototypes
    void rev(unsigned long N, ostream& out);
    
    int main()
    {
      const unsigned long value = 8951;
    
      cout << value << endl;
      rev(value, cout); cout << endl;
    
      return 0;
    }
    
    // Recursive Reverse Digit Function
    void rev(unsigned long N, ostream& out)
    {
      if (N > 0)
      {
        out << N % 10;
        rev(N / 10, out);
      }
    }

  9. #9
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: Reverse number program

    Based on Paul's excellent suggestion...

    Code:
    #include <iostream>
    using namespace std;
    
    int reverseDigit(int num)
    {
    	int result = 0;
    
    	while(num > 0)
    	{
    		result = result*10 + num%10;
    		num /= 10;
    	}
    
    	return result;
    }
    
    int main()
    {
    	int amount = 12345;
    	cout << amount << endl;
    	cout << reverseDigit(amount) << endl;	
    }

  10. #10
    Join Date
    Oct 2001
    Location
    Dublin, Eire
    Posts
    880

    Re: Reverse number program

    The main error was to work on only one variable instead of using at least one additional variable to make the reversing.
    Elrond
    A chess genius is a human being who focuses vast, little-understood mental gifts and labors on an ultimately trivial human enterprise.
    -- George Steiner

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center