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 !
#include <iostream>
usingnamespace 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);
}
elsereturn doubleFour(x/10);
}
@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>
#include <cmath>
#include <iostream>
usingnamespace 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.
*/
unsignedint doubleDigit(unsignedint 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()
{
unsignedint 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");
}
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.
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