Page 2 of 2 FirstFirst 12
Results 11 to 17 of 17

Thread: Rounding to the Nearest Quarter

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

    Default

    Alrigth heres ur code a bit modified i didn't realize that you posted a code using the fuction.. sorry for the earlier assumption. u left out the %f in the printf function:


    Code:
    #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: %f", answer);
    
    	getch();
    	return 0;
    }
    
    
    float RoundToNearestQuarter (float num) {
    
    	int n = num * 4;
    	return (float)n/4;
    }
    Last edited by leoandru; Apr 23, 2005 at 03:44 PM.

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

    Thumbs up

    Okay, i see now. It seems that i misinterpreted the question. You are good Leandru.

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

    Default

    Thanks for the correction to my code. Silly mistakes like these are what causing me to still be a beginner level programmer.

  4. #14
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    typo
    return n/4.0f;
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  5. #15
    Join Date
    Sep 2004
    Posts
    1,905
    Rep Power
    21

    Default

    ok my bad, it was corrected already.

    return n/4.0f; same as return (float)n/4;
    Let's act on what we agree on now, and argue later on what we don't.
    Black men leave Barbeque alone if Barbeque don't trouble you

  6. #16
    Join Date
    Dec 2002
    Posts
    500
    Rep Power
    0

    Default

    i guess he's saying his (compiler) implementation does the cast operation in a different order from yours. maybe you should try

    #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: %.2f", answer);

    getch();
    return 0;
    }

    float RoundToNearestQuarter (float num)
    {
    return 0.25 * (int) (4*num);
    }

  7. #17
    Join Date
    Jul 2005
    Posts
    27
    Rep Power
    0

    Default

    LOL this is funny leandru don't feel no way, your solution was pretty neat. Icymint did it in one line. Man you guys something else. Programming seem to be you guys niche.

Posting Permissions

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