Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Rounding to the Nearest Quarter

  1. #1
    Join Date
    Mar 2004
    Posts
    232
    Rep Power
    0

    Default Rounding to the Nearest Quarter

    Hey Guys lets see how hard this question really is,write a function to round a number to the nearest quarter in C OR C++,dont know either language ? post an algorithm.Try to be as efficient as possible using the least lines you think is possible.

    example 0.23=0.00
    0.37=0.25 etc

  2. #2
    Join Date
    Jan 2005
    Posts
    3,151
    Rep Power
    0

    Default

    0.25 is a very arbitrary number to round to why wouldnt 0.37 round to 0.50?
    Please clarify the rounding resolution.

  3. #3
    Join Date
    Mar 2004
    Posts
    232
    Rep Power
    0

    Default

    Yes yute that would be correct still.

  4. #4
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Code:
    float RoundToNearestQuarter (float num) {
    
    	int n = num * 4;
    	return (float)n/4;
    }
    that should do it
    Last edited by leoandru; Apr 19, 2005 at 04:57 PM.

  5. #5
    Join Date
    Dec 2004
    Posts
    1,181
    Rep Power
    0

    Talking In your face

    Here is the solution to the problem. I really enjoyed the challenge. If u got any more of these weird programming questions, just throw them my way. Peace out.


    //******************************
    //* Programmed by: psybuck2002us *
    //* Date: April 20, 2005. *
    //* Title: Round To Nearest Quarter *
    //* Compiler: Borland C++ COmpiler 5.02 *
    //******************************


    #include <stdio.h>
    #include <math.h>
    #include <conio.h>

    int main(void)
    {
    float input, down, rnum;

    printf("Enter Value: ");
    scanf("%f", &input);

    down = floor(input);


    if(input > down && input < (down + 0.25))
    {
    (input < (down + (0.25 / 2))) ? rnum = down : rnum = down + 0.25;
    }

    if(input > (down + 0.25) && input < (down + 0.50))
    {
    (input < ((down + 0.25) + (0.25 / 2))) ? rnum = down + 0.25 : rnum = down + 0.50;
    }

    if(input > (down + 0.50) && input < (down + 0.75))
    {
    (input < ((down + 0.50) + (0.25 / 2))) ? rnum = down + 0.50 : rnum = down + 0.75;
    }

    if(input > (down + 0.75) && input < (down + 1))
    {
    (input < ((down + 0.75) + (0.25 / 2))) ? rnum = down + 0.75 : rnum = down + 1;
    }

    printf("\n\n\nThe Nearest Quarter Rounded Value is: %.2f", rnum);

    getch();
    return 0;
    }
    Last edited by psybuck2002us; Apr 20, 2005 at 08:03 PM.

  6. #6
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    I can c that u loved the challenge, so much so that u made it more difficult than it seem.

  7. #7
    Join Date
    Apr 2004
    Posts
    537
    Rep Power
    0

    Default

    Leo most guys code that way ,so you are an exception at your age,any way while he is right your solution is better by far as it would run much quicker.

  8. #8
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    Quote Originally Posted by The good guy
    Leo most guys code that way ,so you are an exception at your age,any way while he is right your solution is better by far as it would run much quicker.
    I am not old if that is what ur trying to say , but i do understand ur point it comes with experience. However a problem like this could be easily solved and made efficient if it was properly analysed before writing any code. Its all a part of the learning process.

  9. #9
    Join Date
    Dec 2004
    Posts
    1,181
    Rep Power
    0

    Thumbs down

    Quote Originally Posted by leoandru
    Code:
    float RoundToNearestQuarter (float num) {
    
    	int n = num * 4;
    	return (float)n/4;
    }
    that should do it

    I tried running the above code you posted and it didn't work for me and logically i don't see how your solution would answer the question. Maybe i'm blind and stupid, i dont know. Anyhow, here is the program i wrote with your function in it.

    #include <stdio.h>
    #include <conio.h>

    float RoundToNearestQuarter(float);

    int main(void)
    {
    float input;
    float answer;

    printf("Enter Number: ");
    scanf("%f", &input);

    answer = RoundToNearestQuarter (input);
    printf("The answer is: ", answer);

    getch();
    return 0;
    }


    float RoundToNearestQuarter (float num) {

    int n = num * 4;
    return (float)n/4;
    }

  10. #10
    Join Date
    Oct 2004
    Posts
    4,814
    Rep Power
    24

    Default

    what do u mean it didnt work? its just a function.. you would have to write a main program that use the function. Anyways lets trace it so that u c the logics:
    lets say you pass 0.37 to the function
    int n = num * 4;

    will assing the value of 1 to n the:


    return (float)n/4;

    will retun 0.25

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •