# reversing digits

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-26-2003
lizardking3
reversing digits
I'm just looking for a hint in the right direction if someone could help me please. The first part works good, but i can't the the call by reference to reverse the digits for me. It outputs a 0 or it outputs the numbers in the same order that i input them.
Code:

```// reversedigits.cpp // written by // cis #include <iostream> #include <iomanip> using namespace std; int revdigits ( int ); // call by value void revdigits1 ( int, int& );// call by reference int width ( int ) ; void width1 ( int, int& ); int main() {         int number = 0,                 r = 0;         cout << "Enter a number between 1 and 9999: " << fixed;         cin >> number;         cout << "The number with its digits reversed is: " << setw ( width ( number ) ) <<  revdigits ( number  ) << endl;         void revdigits1 ( int n, int r );         cout << "The number with its digits reversed is: " ;         void width1 ( int n,int r );         cout << r << endl;         return 0; } int revdigits ( int n ) {         int reverse = 0,                 divisor = 1000,                 multiplier = 1;         while  ( n > 10 )         {                 if  ( n >= divisor )                 {                         reverse += n / divisor * multiplier;                         n %= divisor;                         divisor /= 10;                                         multiplier *= 10;                 }                 else                         divisor /= 10;         }         reverse += n * multiplier;         return reverse; } void revdigits1 ( int n, int &r ) {         int reverse = 0,                 divisor = 1000,                 multiplier = 1;         while  ( n > 10 )         {                 if  ( n >= divisor )                 {                         reverse += n / divisor * multiplier;                         n %= divisor;                         divisor /= 10;                                         multiplier *= 10;                 }                 else                         divisor /= 10;         }         reverse += n * multiplier;         r = reverse; } int width ( int n ) {         if ( n / 1000 )                 return 4;         else if ( n / 100 )                 return 3;         else if ( n / 10 )                 return 2;         else                 return 1; } void width1 ( int n, int &r ) {         if ( n / 1000 )                 r = 4;         else if ( n / 100 )                 r = 3;         else if ( n / 10 )                 r = 2;         else                 r = 1; }```
• 03-26-2003
CheesyMoo
First off you don't need to put void before your function call ( the call not the header). So right here:
Code:

```        cout << "The number with its digits reversed is: " << setw ( width ( number ) ) <<  revdigits ( number  ) << endl;         void revdigits1 ( int n, int r );         cout << "The number with its digits reversed is: " ;         void width1 ( int n,int r );```
The void is unnescecary(sp?), I'll add more later.
• 03-26-2003
CheesyMoo
I don't understand what you're trying to do in this next code portion:
Code:

```int width ( int n ) {         if ( n / 1000 )                 return 4;         else if ( n / 100 )                 return 3;         else if ( n / 10 )                 return 2;         else                 return 1; }```
If n / 10 is a positive number, it will be true. So if someone enters 24 for the n this part:
Code:

```if(n / 1000)  return 4;```
Will return 4 because it is a positive number, not because it's 4 digits long.

I think what you want to do is:
Code:

```if(n / 1000 >= 1)   return 4;```
Unless I'm on drugs everything I said is mostly correct.
• 03-26-2003
lizardking3
Ok, the void is part of my call by reference and i think that it needs to be there. The call by value part of my program seems to work ok. this part of the code
Code:

```int width ( int n ) {         if ( n / 1000 )                 return 4;         else if ( n / 100 )                 return 3;         else if ( n / 10 )                 return 2;         else                 return 1; }```
once the calculations are done in the code this part breaks the number down to a single digits so that it can put in reverse order
now then what i really do not unerstand is this
Code:

`void width1 ( int, int& ); // call by reference if i am correct function prototype`
i'm having problems with the implimintation of this function
it worked ok doing it with call by value but i'm not sure about the call by reference.
• 03-27-2003
R.Stiltskin
There's nothing wrong with your function. The problem is with your function call. The way you wrote it, your function call is trying to re-declare the function. The call should just name the function and send the parameters; it shouldn't declare them. Also, you're introducing a new variable n that has no value.

In main() just make this simple change:

replace
void revdigits1 ( int n, int r );

with:
revdigits1 ( number, r );

and it will compile and run correctly.

One more thing, also in main(), this line:
void width1 ( int n,int r );
is not doing anything at all & should not be there. It's not interfering with anything, but it's not doing anything either. If you comment it out you will see that nothing changes.
• 03-27-2003
lizardking3
thanks for the help. I'm still kind of comfused though in the part of the code that my teacher gave me it had this.

void width1(int, int&);

I thought that maybe it was somehow suppost to be used to reverse the order of the digits.
Code:

``` void width1 ( int n, int &r ) {         if ( n / 1000 )                 r = 4;         else if ( n / 100 )                 r = 3;         else if ( n / 10 )                 r = 2;         else                 r = 1; }```
so is any of this even necessary in my code for reversing the digits?
• 03-27-2003
Depends on the requirements of the program. Sometimes teachers do things illogically to demonstrate a point or provide practice. Without knowing the requirements it is difficult to say whether the steps you have taken are necessary or not. To just show the digits in reverse order without consideration for how you do it, the step isn't necessary. For example, here's code that displays the value of num passed to the function by value without any return or reference necessary. You can't do anything with the reversed digits, but the digits do appear on the screen in reverse order, and the value of num can be any valid int, not just an int with 4 digits.

void revDigits(int num)
{
int mod;
while(num > 9)
{
mod = num % 10;
cout << mod;
num /= 10;
}
}
• 03-27-2003
lizardking3
He wanted us to write a program that can display and number from 1 to 9999 in reverse order by using call by reference and call by value
and gave us the prototypes
Code:

```#include <iostream> #include <iomanip> using namespace std; int reversedigits(int); void reversedigits1(int, int&); int width(int); void width1(int, int&); int main() {         int number = 0;                 cout << "Enter a number between 1 and 9999: ";         cin >> number;         cout << "The number with its digits reversed is: ";         cout << setw(width(number)) << reversedigits(number);         cout << endl;         return 0; } int reversedigits (int n) {         int reverse =0;         int divisor = 1000;         int multiplier = 1;         while( n > 10) {                 if (n >= divisor) {                         reverse += n / divisor * multiplier;                         n %= divisor;                         divisor /= 10;                         multiplier *= 10;                 }                 else                         divisor /= 10;         }         reverse += n * multiplier;         return reverse; } void reversedigits1(int n, int &r) {     int reverse =0;         int divisor = 1000;         int multiplier = 1;         while( n > 10) {                 if (n >= divisor) {                         reverse += n / divisor * multiplier;                         n %= divisor;                         divisor /= 10;                         multiplier *= 10;                 }                 else                         divisor /= 10;         }         reverse += n * multiplier;         r = reverse; } int width(int n) {     if(n / 1000)                 return 4;         else if(n / 100)                 return 3;         else if(n / 10)                 return 2;         else                 return 1; }```
this is the code that he gave us all we had to do was finish it.

thas is why i keep trying to figure out what to do with this part

void width1(int, int&);
• 03-27-2003
If this does what you want:

cout << "The number with its digits reversed is: " << setw ( width ( number ) ) << revdigits ( number ) << endl;

and you want the functions using the reference parameters to mimic the above code then change this:

void revdigits1 ( int n, int r );
cout << "The number with its digits reversed is: " ;
void width1 ( int n,int r );
cout << r << endl;

to this:

revdigits1 (number, r);
int w = 0;
width1 (number, w);
cout << "The number with its digits reversed is: " << setw(w) << r << endl;
• 03-27-2003
lizardking3
thanks
• 03-27-2003
CheesyMoo
Okay sorry I thought that you were trying to find how many digits were in the number.
• 03-27-2003
CheesyMoo:

The way I understand it is that the value 0 will be interpreted as false in any situation where true/false is called for, such as in conditionals, whereas any other value, positive or negative, will be interpretted as true.

if n is an int with the value of 24 then

n/1000 will be 0 by property of integer math

so the when used as a conditional as in

if(n/1000)

the conditional will be false.

Therefore, I believe that width() and width1() will return the number of digits in number, as long as number is less than 9999 and greater than 0. I'm not sure what the instructor wants the students to do with width() and width1() (using them in setw() seems a little trivial; but I've been known to do worse), but I think they are functional as is.
• 03-27-2003
Hammer
Just for your info, here's another number reversing thread
• 03-28-2003
lizardking3
Something that my teacher did tell us though is that there is a bug in the code. If you enter any number that has a zero in it the zero will not be displayed. I don't know why but that seems to hold true. If you enter 0000 it will output just one zero. If you enter 1001 then it outputs 11 or 1203 would output 321. Seems to be a problem.
• 03-28-2003
Hammer
>>If you enter 0000 it will output just one zero
And you consider that to be a problem? In numeric terms, 0000 is the same as 0.

If you want to reverse the extact number as entered, eg keep the leading zeros, you'll have to read it as a string, and reverse that instead. This means you won't actually treat it as a real number.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last