Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Write a function: swapDigits(m,n,x){} that has 3 nonnegative integer arguments, and must return the number that is obtained by swapping the mth and nth digit of a number x. for ex. swapDigits(0,3,123456) returns 126453, the 0th digit of a number is its least significant digit. or another ex. swapDigits(2,3,1234) returns 1324. I must not use strings or arrays. This is my work, but the program doesn't work if m or n=0, if x=10a+b, and for some other numbers like swapDigits(2,4,2412).

What I have tried:

C++
int numberOfDigits(int x) {
	int count=0;
	while(x!=0){
		x/=10;
		count++;
	}
	return count;
}

int swapDigits(int m, int n, int x) {
	int tempa, tempb, temp, temp1,  temp2, a, b, c, g, d;
	temp=x;
	temp1=x;
	temp2=x;
	a= numberOfDigits(x) - m ;
	b= numberOfDigits(x) - n ;
	tempa=a;
	tempb=b;
	int mthDigit, nthDigit;
	while(tempa>=0){
		tempa--;
		temp1/=10;
		mthDigit=temp1%10;
	}
	while(tempb>=0){
		tempb--;
		temp2/=10;
		nthDigit=temp2%10;
	}

	d = (mthDigit*pow(10, a)) + (nthDigit*pow(10, b)) ;
	g = (nthDigit*pow(10, a)) + (mthDigit*pow(10, b)) ;

	return x - d + g;
}
Posted
Updated 26-Sep-16 13:55pm
v3
Comments
[no name] 26-Sep-16 14:40pm    
Sounds like a fine time to learn to run your code using the debugger and examine the variables as it runs.

Start by thinking about it: what is a decimal digit? How do I remove one? How do I add one?
Well, to the 3rd digit of this number: 654321 is "4", and I'd remove it by converting the 3 to a power of ten: 1000.
If I divide the base number by 1000 * 10 I get 65. If I multiply that by 1000 * 10 I get 650000.
If I take the remainder of the original number and 1000, I get 321.
So adding those two together has removed the third digit:
C#
removed = (x / 10000) * 10000 + (x % 1000)

Similarly, I can extract the digit very easily:
C#
digit = (x / 1000) % 10


And just by changing the power-of-ten value I can do the same for the first digit:
C#
removed = (x / 10) * 10 + (x % 1)
digit = (x / 1) % 10


So... if you write a two functions to remove a digit and return a digit given x and the digit power-of-ten all you have to do is call each of them twice and then multiply and add.

Does that make sense?
 
Share this answer
 
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900