-
November 15th, 2009, 11:33 PM
#1
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");
}
}
-
November 15th, 2009, 11:37 PM
#2
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.
-
November 15th, 2009, 11:47 PM
#3
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!?!?!
-
November 16th, 2009, 12:46 AM
#4
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.
-
November 16th, 2009, 03:13 AM
#5
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;
}
-
November 16th, 2009, 03:27 AM
#6
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.
-
November 16th, 2009, 09:34 AM
#7
Re: Reversed Integer Using Arrays, My friend told me its to complicated
Originally Posted by treuss
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
-
November 16th, 2009, 09:44 AM
#8
Re: Reversed Integer Using Arrays, My friend told me its to complicated
Originally Posted by Jim_King_2000
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
-
November 16th, 2009, 09:05 PM
#9
Re: Reversed Integer Using Arrays, My friend told me its to complicated
Originally Posted by 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|