reverse algorithm loop

My homework says to create a function whose input is an integer which returns the reverse of that integer, using a loop. I figured out the algorithm by hand, and I wrote part of the code, but I can't figure what to put into the loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int reverse(int);

int main () 
{
	int n;
	
	cout << "Enter an integer: ";
	cin >> n;
	
	cout << "The reverse of the entered integer is " << reverse(n) << endl;

    return 0;
}

int reverse(int x)
{
	int ;
	
	while ()
	{
		
		
	}
}


Example: If I wanted to reverse 246,
246%10=6 246/10=24 0*10+6=6
24%10=4 24/10=2 6*10+4=64
2%10=2 2/10=0 64*10+2=642 <- now it's reversed. I just don't know how to write this as a code.
Outline what you are doing each step for me. You might see how the loop works if you do that.
1. modulus division of entered number by 10
2. dividing # by 10
3. I'm not sure what to put about the zero, but multiplying by 10, and adding the remainder from the modulus division.
4. quotient 'a' mod division by 10, quotient 'a' divided by 10, then the sum from the first line * 10 + the remainder.
5. continues until you have the reverse.
okay you are on the right path. You'll need to stick a variable in there depending on which digit you are on and you'll need to know the size.

To find the size:
1
2
3
int size = 0;
for (int i = x; i > 0; i /= 10) 
  size++;

Now that we have the size, we can access each digit

Find the ith digit in x.
int digit = (x / pow(10,i-1)) % 10;

Now set the size - i + 1th digit (opposite digit)
output += digit * pow(10,size-i);

For this to work properly (without warnings) you need to have int pow (int, int) available. If your <cmath> doesn't have it, it's easy enough to write.

Now loop through for all of the values of i that are applicable (i = 1 to size)
Refining your outline a bit:
1
2
3
4
5
6
7
8
9
10
11
12
13
/*
Step 1:
    x % 10 = digit 1 of the result
    0*10 + above = current result
    x / 10 = remaining digits to consider
Step 2:
Repeat step 1, letting x be the remaining digits from step 1:
    x % 10 = digit 2 of the result
    1*10 + above + result from previous step = current result
    x / 10 = remaining digits to consider
Step 3:
Repeat step 2 with x = remaining digits from step 2
*/


And so on. Do you follow my process here?
Last edited on
Ok, I'm thinking a for loop would be better than a while loop. I already figured that in the loop I would need x%10 and x/10. I can't figure out how to code the last equation of the algorithm, and I think I need a temporary value to replace but I don't know where. I have a loop that runs now, but it's not giving me the reverse, just the number I input, so it's definitely not right.
There is a very simple solution: use char*! That way you can just print the digits in reverse! Unless ofcourse you need to use the number somewhere else, as a number, but it looks like your code is just for reversing.
This is the reverse function I wrote, but it's not right. For any number I enter it just returns 1969647184.

1
2
3
4
5
6
7
8
9
10
11
12
13
int reverse(int x)
{
	int a, b, c;
	
	for (int i=0; i<0; i++)
	{
		x=a;
		a=x%10;
		b=i*10+a;
		c=x/10;
		
	}
}
1. count tthe number of digits.
2. collect the digits- one by one -and multiply dem with powers of 10 in the reverse order and add dem to a variable 'k'.
3. return

1.
1
2
3
4
5
6
num=number;
count=0;
while(num!=0)
{
num/=10;count++;
}

2.
1
2
3
4
5
6
7
8
9
sum=0;
for(i=1;i<=count;i++) //note that it does not start with i=0; as if we have a three digit number
//then the highest power of 10 we would encounter is 3-1=2 and on simlar grounds
//lowest power would be 3-3=0...
{
k=number%10;
sum+=k*pow(10,count-i);
number/=10;
}

3. return sum as the answer
Last edited on
Your problem above is that you define a, but you don't initialize it. You are setting x to a. This overwrites whatever x used to be with something undefined. I'm guessing you meant to do a=x;
When I do a=x it just gives me the number I entered.
the loop wont execute for more than once ......... just check .............. the conditions uve entered for the loop........
@Fraidy Levilev

What compiler are you using? C++ requires a return from a non-void function, but the for loop looks like C++. As it stands, I can't tell where your value of 1969647184 is coming from.

Your original example looks totally correct. But I would paraphase point 5 of your follow on explanation so it reads "5. continues until you have no more digits to reverse."

This suggests that the loop termination condition (as you orig intended, a while loop is the right way to go here) is something to do with 0.

But, as mentioned by earlier posters, your code's loop isn't quite right, and you're overwriting x with the value of uninitialized valiable, etc. The suggestions to count the numbers first are a bit of a red herring, though.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int reverse(int x)
{
	int a, b = 0, c; // b must be init to zero
	
	//for (int i=0; i<0; i++)
	while(x > 0)
	{
		//x=a; -- not needed
		a=x%10;
		//b=i*10+a; -- not quite right: replace i with b
		b = b * 10 + a; // multiply b by 10, to shift existing
		// digits to the left, and add new units value 
		c=x/10;

		x = c; // ready for the next iteration
	}

	return b;
}


Or, eliminating debris and unnecessary variables

1
2
3
4
5
6
7
8
9
10
11
12
13
int reverse(int x)
{
	int b = 0;

	while(x > 0)
	{
		int a = x % 10;
		b = b * 10 + a;
		x = x / 10; // ready for the next iteration
	}

	return b;
}


Or even more succinctly, and tweaking the termination condition so negative values are also handled.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int reverse(int val)
{
	int rev_val = 0;

	while(val != 0)
	{
		// shift digits in reversed value one left and add on
		// new units value
		rev_val = rev_val * 10 + val % 10;
		// shift value one to right, losing current units value,
		// ready for the next iteration
		val /= 10;
	}

	return rev_val;
}


Last edited on
Topic archived. No new replies allowed.