Thread: Switching 2nd and 4th digit in a double(before comma)

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    11

    Switching 2nd and 4th digit in a double(before comma)

    The goal of the program is to switch the 2nd and 4th digit in a double(before comma). Lets say you have 28492.45, it should become 29482.45. The program is working fine. I want to add another option, where the user input is a two digit number(before comma) like 56.78, the 3th and 4th digit should be considered as 5600.78. The switched number should be then 5006. Same goes for a 3 digit number. How can i implement this?

    Code:
    int main() {
    
        
        double number;
        
        
        scanf("%lf", &number);
        
        //printf("%.4f", number);
        
        char arr[sizeof(number)];
        snprintf(arr, sizeof(number) + 1, "%f", number);
        
        char aux = arr[1];
        arr[1] = arr[3];
        arr[3] = aux;
    
    
        number = atof(arr);
        printf("%.4f\n", number);
        
        
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Perhaps keep multiplying number by 10 until it's greater than 1000.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    scanf("%lf", &number);
    The l modifier has no effect on f. It is permitted but does nothing. If you wish to read a long double, you need an uppercase l (i.e. "%Lf").

    However, the variable number is a double. "%f" reads doubles, not floats as its name may imply. There is no identifier for floats because floating-point values are always read as double and then converted to float if that is the type of the variable to store the input.

    So, in your code, regardless of number being a float or double, you just need "%f". And you would need "%Lf" is the variable was long double.

    How can i implement this?
    If the transformation is always from 2 to 4 digits in the integer portion, 56 transforms into 5006 by means of a modulus operation, followed by an integer division, followed by a addition.

    Code:
    int number = 56;
    
    remainder = number % 10; // returns 6
    manifold = number / 10 * 100   // note that this is NOT the same as number * 10. You know why?
    final = manifold + remainder
    The formula is thus: n / 10 * 100 + n % 10, for any any int n < 100

    Then you just need to add the decimal portion. I'll leave to you the task of separating the integer portion from the decimal portion and storing both for use. Then see if you can apply the same principle to a 3 digit integer, 4 digit, etc. and what needs to change. Show some code and we can work from there.
    Last edited by Mario F.; 10-14-2016 at 01:31 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Quote Originally Posted by Mario F. View Post
    The l modifier has no effect on f. It is permitted but does nothing. If you wish to read a long double, you need an uppercase l (i.e. "%Lf").

    However, the variable number is a double. "%f" reads doubles, not floats as its name may imply. There is no identifier for floats because floating-point values are always read as double and then converted to float if that is the type of the variable to store the input.

    So, in your code, regardless of number being a float or double, you just need "%f". And you would need "%Lf" is the variable was long double.
    You're thinking of printf, where floats are converted to double for printing. But with scanf the %lf format is meaningful (and necessary).

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Not sure how I made that mistake. Lack of experience for sure. Thanks for the correction.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    11
    Thanks for helping me out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to display single digit int as four digit C++ Programming
    By Sloganathan in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2012, 11:30 AM
  2. Comma instead of period in Double output
    By trillykins in forum C Programming
    Replies: 1
    Last Post: 11-19-2010, 09:29 AM
  3. Adding a Large number digit by digit
    By mejv3 in forum C Programming
    Replies: 23
    Last Post: 09-21-2007, 03:00 PM
  4. Replies: 4
    Last Post: 07-14-2003, 08:11 AM
  5. writing double quote & comma delimited files
    By emt in forum C++ Programming
    Replies: 5
    Last Post: 06-30-2003, 12:28 AM

Tags for this Thread