1. ## 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;
}```

2. 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.

3. 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.

4. 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.

5. 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.

6. 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?

7. 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;
}
}

8. 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&);

9. 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;

10. thanks

11. Okay sorry I thought that you were trying to find how many digits were in the number.

12. 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.