rounding program...s.o.s.

This is a discussion on rounding program...s.o.s. within the C Programming forums, part of the General Programming Boards category; here is the problem.. The function floor is one of the functions available in the C math library (floor(x) rounds ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    rounding program...s.o.s.

    here is the problem..
    The function floor is one of the functions available in the C math library (floor(x) rounds the decimal number x to the largest integer not greater than x). The function floor can be used to round a number to to a specific decimal place. For example, the statement
    y = floor( x*100 + .5)/100;
    rounds x to the hundredths position.
    Write a program that defines a function called roundtohundreths that rounds a number to the nearest hundredth. For each value read, your program should display the original value (a decimal number) and the number rounded. Your program should have a loop prompting the user to enter a "Y," if a number is to be entered, or a "N" if no new number will be entered.

    here is my code so far.. which is not working very well----

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

    int roundtohundreths (int, int);
    int main ()
    {
    int t, b, N, Y;
    float x, y;

    printf( "Type Y for entering a number ( N to end ): " );

    while( ( t = getchar() )!= N ) {
    switch ( t ) {

    case 'Y': case 'y':
    printf( "Enter number: " );
    scanf( "%f", &x);
    y = ( x * 100 + .5 ) / 100;
    printf( "Original value is %.4f\n", y );
    printf( "Rounded number is %d\n", roundtohundreths(x, y ) );
    }

    printf( "Type Y for entering a number ( N to end ): " );
    scanf( "%d", &b );
    }
    return 0;
    }
    int roundtohundreths (int a, int b )

    {
    int roundto = a;
    a = floor( b * 100 + .5 ) / 100;

    return roundto;
    }



    help would be greatly appreciated!!!!!!!!!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Try this:
    Code:
    #include <stdio.h> 
    #include <math.h> 
    
    double roundtohundreths (double); 
    int main () 
    { 
      int t = 'y'; 
      double x;  
      while( t != 'N' && t != 'n' ) { 
        if ( t == 'Y' || t == 'y' ) { 
          printf( "Enter number: " ); 
          scanf( "%lf", &x); while ( getchar() != '\n' );  
          printf( "Original value is %.4lf\n", x ); 
          printf( "Rounded number is %.2lf\n", roundtohundreths( x ) ); 
        } 
        printf( "Type Y for entering a number ( N to end ): " );
        t = getchar();
      } 
      return 0; 
    } 
    double roundtohundreths (double a) 
    { 
      double roundto = floor( a * 100 + .5 ) / 100; 
      return roundto; 
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    22

    Smile thanks a lot!!!



    thanks man....the program works pretty well....
    even though is 100 percent right i am going to try to recode mine based on yours...its because i dont like easy things, i like to figure out...

    thanks again---

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >i like to figure out
    Someone after my own heart. My motto is 'What good is asking for help if you learn better by muddling through on your own?'

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  2. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21