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");
}
}
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.
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!?!?!
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.
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;
}
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! :mad:
Re: Reversed Integer Using Arrays, My friend told me its to complicated
Quote:
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! :mad:
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
Re: Reversed Integer Using Arrays, My friend told me its to complicated
Quote:
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
Re: Reversed Integer Using Arrays, My friend told me its to complicated
Quote:
Originally Posted by
Paul McKenzie
I read the guidelines. Thank you.