HELP! Placing an extra copy of the 4th digit.

Hi, my goal is to write a function called doubleFour that places an extra copy of the 4th digit right after that digit in an integer parameter. The problem is after I placed an extra copy of the 4th digit, I'm stuck of adding back the remaining number. I know there has to be some thing to do with x%10 and x/10, but I'm just stuck of putting it in. PLEASE help !



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
using namespace std;

int doubleFour (int x);

int main() {
int x = doubleFour(19683);
cout << x << endl; // prints 196883
cout << doubleFour(271828)<< endl; // prints 2718828
cout << doubleFour(314159)<<endl; // prints 3141159
return 0;
}


int doubleFour(int x){
	int y,z,c, temp=x%100;
	if (x<=9999){
		y=((x/10)*10)+(x%10);
		z=(y*10)+(y%10);
		return (z*10);
	}
	else return doubleFour(x/10);

}
I am not quite sure I understand what you mean. Test this, it might be just what you want.
1
2
3
int doubleFour(int x) {
	return 10 * ( x - x % 1000 ) + x % 10000;
}

Otherwise could you explain in a different way "places an extra copy of the 4th digit right after that digit"?
Last edited on
@snowright - it looks like your code works almost as expected, but there is a catch - you are doubling 4th digit from the right, and the examples show that it is fourth digit from the left that needs doubling.
I have put together a formula that will do just that - double n-th digit from the left, so it is a bit more advanced than requested. But basically, all the calculations are done in single line ;P
Hope this helps <trollface>
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cmath>
#include <iostream>
using namespace std;
/***
 * This will take two parametes: 
 * x - is the number that is to be worked on 
 *     (please mind the int capacity - it will 
 *     not  work for 10+ digit integers!)
 * y - it is which digit (from the left!) is 
 *     going to be doubled. If y is more than
 *     total number of digits, nothing will be
 *     changed.
 */
unsigned int doubleDigit(unsigned int x, int y)
{
	// Function will need the number of digits
	int nDigits = 1;
	while (pow(10.0, nDigits) <= x)
		nDigits++;

	if(nDigits < y)
		return x;

	// Based on number of digits, x and y result is returned:
	return 
		(int)(x/(pow(10.0,nDigits-y)))*(int)pow(10.0,nDigits-y+1)
		+(int)(x/(pow(10.0,nDigits-y)))*(int)pow(10.0,nDigits-y)-
		(int)(x/(pow(10.0,nDigits-y+1)))*(int)pow(10.0,nDigits-(y-1))
		+(x-(int)(x/(pow(10.0,nDigits-y)))*(pow(10.0,nDigits-y)));
}

int main()
{
	unsigned int yourNumber;
	cout << "Enter your number:";
	cin >> yourNumber;

	// Will double each of first 9 digits:
	for(int i = 1; i < 10; i++)
	{
		cout << "Doubling digit #" << i << ": ";
		cout << doubleDigit(yourNumber, i) << endl;
	}
	system("pause");
}
Last edited on
Hi. what i mean is, to repeat whatever digit is in the 4th place of the number.

123456 become 1234456
356768 become 3567768

and so.

Thanks for advance.
Maybe try using the arrays instead?
I'm still some kind of new to C++ thought.
kaye0822, thanks for the clarification, solution will follow.
JockX, I have a better idea, how do you like it?
1
2
3
4
int doubleFour( int x ) {
	int e = floor( log10( x ) );
	return 10 * ( x - x % ( int ) pow( 10, e - 3 ) ) + x % ( int ) pow( 10, e - 2 );
}
Using a collection class:
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
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <vector>

#define STR(x) #x << '=' << x

int doubleFour(unsigned x)
{
  std::vector<unsigned> digits;
  while (x) {
    digits.push_back(x % 10);
    x /= 10;
  }
  std::size_t where = digits.size() + 1 - 4;
  while (digits.size()) {
    x *= 10;
    x += digits.back();
    if (digits.size() == where) {
      x *= 10;
      x += digits.back();
    }
    digits.pop_back();
  }
  return x;
}

int main()
{
  std::cout << STR(doubleFour(0)) << std::endl;
  std::cout << STR(doubleFour(1)) << std::endl;
  std::cout << STR(doubleFour(12)) << std::endl;
  std::cout << STR(doubleFour(123)) << std::endl;
  std::cout << STR(doubleFour(1234)) << std::endl;
  std::cout << STR(doubleFour(12345)) << std::endl;
  std::cout << STR(doubleFour(123456)) << std::endl;
  std::cout << STR(doubleFour(1234567)) << std::endl;
  std::cout << STR(doubleFour(356768)) << std::endl;
  return 0;
}


gives:
doubleFour(0)=0
doubleFour(1)=1
doubleFour(12)=12
doubleFour(123)=123
doubleFour(1234)=12344
doubleFour(12345)=123445
doubleFour(123456)=1234456
doubleFour(1234567)=12344567
doubleFour(356768)=3567768
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int dnum(int x)
{
	int n=0,t=x;int arr[10];int ret=0;
	for(n;t>0;n++)
	{
		arr[n]=t%10;
		t/=10;
	}
	n--;//index of array is 1 less than size of array
	for(int i=0;i<=n;i++)
	{
		ret=ret*10+arr[n-i];
		if(i==3)
		{
			ret=ret*10+arr[n-i];
		}
	}
	cout<<"ret : "<<ret;
	return 0;
}
snowright wrote:
I have a better idea, how do you like it?
1
2
3
4
int doubleFour( int x ) {
    int e = floor( log10( x ) );
    return 10 * ( x - x % ( int ) pow( 10, e - 3 ) ) + x % ( int ) pow( 10, e - 2 );
}


I don' t like it, because it is better, clearer and shorter than mine :P. It's ideal for the task. My only comfort is that it crashes for numbers lower than 1000 and only covers digit at position 4 :)
Well, you know, I tend not to provide working solutions for people often since they are most likely here learning to do things themselves. I only provide ideas. But yes, you would probably want to check e >= 3 given you do not know the number is not large enough already before using something similar I provided straight. Also replacing e - 3 and e - 2 with e - n + 1 and e - n + 2 correspondingly will provide a general solution for n-place digit ( combined with the check e >= n - 1 if that is needed ).

kaye0822, needed you explanation or assist in understanding any solution, it will be provided after asked.
Last edited on
Why don't use std::string?
i means input, l means length and f means four.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

int main() {
    std::string i;

    std::cin >> i;
    int l = i.length();

    while (l < 4) {
        std::cin >> i;
    }

    std::string f = i.substr(3, 1);
    i.insert(3, f);

    std::cout << i;
    return 0;
}

I've learnt how to use std::string and found that one of it's method, insert() can solve this question easily.
Anyway I'm still learning C++ yet. (And also I'm using mobile, which the screen is to small and makes me hard to type the code... :( )
Here's the online example: http://ideone.com/7topbn
Topic archived. No new replies allowed.