Reversed Integer Using Arrays, My friend told me its to complicated
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Reversed Integer Using Arrays, My friend told me its to complicated

  1. #1
    Join Date
    Sep 2009
    Posts
    37

    Reversed Integer Using Arrays, My friend told me its to complicated

    The assignment everyone has done before..lol...reversing digits. How do i make my code more simple (as my friend stated)?

    Trying to write a program that reverses the digits in an integer. Assuming that the max value of the integer is 99999. And using arrays to solve the problem.

    For example if integer:
    0 was entered, output is 0
    10 was entered, output is 1
    12 was entered, output is 21
    123 was entered, output is 321
    7600 was entered, output is 67
    8015 was entered, output is 5108
    90000 was entered, output is 9

    here is my code:

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
          int intnumber, condition, remainder;
          int counter = 0, i = 0, j = 0, counter1 = 0;
          int reverseint[99999];       
    
    	  printf("Enter an integer number: ");
          scanf_s("%d", &intnumber);
          condition = intnumber;  
    
    	  while (condition > 0)
          {
             condition  = condition /10;
             counter = counter + 1;
             counter1 = counter1 + 1;
          } 
    
    	  counter = counter - 1;
          printf("The number in reverse: ");
          while (counter >= 0)
         {
             remainder = intnumber % (int) pow(10, counter);
             intnumber = intnumber/(int) pow(10,counter);     
    		 reverseint[i] = intnumber;
             i++;
             intnumber = remainder;
             counter = counter - 1;
          }
    
          for(j=counter1-1;j >= 0;j--)
    	  {
             printf("%d ", reverseint[j]);
    		 printf("\n");
    	  }  
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    Are you required to manipulate it *as* an integer? Because it would be much simpler to do it while it's still a string:
    Code:
        string valstr;
        cin >> valstr;
        std::reverse(valstr.begin(),valstr.end());
        int val;
        istringstream strm(valstr);
        strm >> val;
        cout << val;
    Of course, that's without input validation. The conversion from string to int prior to output is there solely to eliminate any leading 0s. This could be done in string form instead if desired.

  3. #3
    Join Date
    Sep 2009
    Posts
    37

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    Thanks! but

    I found out that, I can't use printf but instead use couts.
    I'm in a beginner level's C++ class and my teacher also wants it more simpler..aha

    said i can't use scanf_s, pow, and strings!?!?!

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Fairfax, VA
    Posts
    10,888

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    There's almost a one-to-one mapping between what you can do with printf/scanf and what you can do with cout/cin. That should be fairly trivial to convert.

    You shouldn't need pow. Consider the algorithm in this sense:

    You have two integers
    8765
    1234
    and you want to modify them so that you will instead have
    87654
    123
    How would you do this?

    Then simply take that one step and put it in a loop. Even arrays are unnecessary.

  5. #5
    Join Date
    May 2004
    Posts
    113

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    For simplicity, I ignore error handling.

    Code:
    int reverse(int n)
    {
        assert(n >= 0);
    
        int result = 0;
        while (n > 0)
        {
            int r = n % 10;
            n /= 10;
            result = result * 10 + r;
        }
    
        return result;
    }

  6. #6
    Join Date
    Jan 2004
    Location
    Düsseldorf, Germany
    Posts
    2,401

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    Jim_King_2000: Please read the forum rules on doing homework for other people !!! People here are trying to help, your post was the opposite of helpful!
    More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason - including blind stupidity. --W.A.Wulf

    Premature optimization is the root of all evil --Donald E. Knuth


    Please read Information on posting before posting, especially the info on using [code] tags.

  7. #7
    Join Date
    May 2004
    Posts
    113

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    Quote Originally Posted by treuss View Post
    Jim_King_2000: Please read the forum rules on doing homework for other people !!! People here are trying to help, your post was the opposite of helpful!
    What's wrong with my post? Can't my code work?
    By the way, would please provide the like to the forum rules? I can't find it.

    Thanks,
    Jim

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

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    Quote Originally Posted by Jim_King_2000 View Post
    By the way, would please provide the like to the forum rules? I can't find it.
    Not rules, but guidelines that have been placed here and have been known for at least 10 years now.

    http://www.codeguru.com/forum/showthread.php?t=366302

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    May 2004
    Posts
    113

    Re: Reversed Integer Using Arrays, My friend told me its to complicated

    Quote Originally Posted by Paul McKenzie View Post
    Not rules, but guidelines that have been placed here and have been known for at least 10 years now.

    http://www.codeguru.com/forum/showthread.php?t=366302

    Regards,

    Paul McKenzie
    I read the guidelines. Thank you.

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