How to make this work =(

This is a discussion on How to make this work =( within the C Programming forums, part of the General Programming Boards category; The question is to input a non-negative real number: and approximate the closest fraction. so i.e. if Nonnegative real number: ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    11

    How to make this work =(

    The question is to input a non-negative real number:
    and approximate the closest fraction.

    so i.e.

    if

    Nonnegative real number: 0.751
    Closest approximation = 3/4


    ( 42.7488 -> approx. 171/4 ...and so on)





    Code:
    #include <stdio.h>
    
    int main() {
        
        double p, pre, cur;
        int i, j, x, y, d;
        
        printf("Nonnegative real number: ");
        scanf("%d.%p", &d, &p);
        
        while(cur>pre){
        
        for (i=1; i<=100; i++){
            
            for (j=1; j<99; j++){
                
                pre = (double)j/i - p;                                          
            }
        }                              
        
        
        for (x=2; x<=100; x++){
    
            for(y=2; y<99; y++){
                     
                cur = (double)y/x - p;  
            }
        }   
        
        if (cur > pre)
           break;
           break;
        }
                                                    
       printf("Closest approximation = %d/%d, ", (x*d)+y, x);                                     
        
    
                                                 
        
    return 0;
    
    }

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I would say the closest fraction for 0.751 is: 751/1000
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this program work? Problem with arrays
    By babe20042004 in forum C++ Programming
    Replies: 1
    Last Post: 12-21-2009, 06:14 PM
  2. Makefile Problem: None rule to make target
    By chris24300 in forum Linux Programming
    Replies: 25
    Last Post: 06-17-2009, 09:45 AM
  3. Game Programming FAQ
    By TechWins in forum Game Programming
    Replies: 5
    Last Post: 09-29-2004, 02:00 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. cant make it work
    By papous27 in forum Linux Programming
    Replies: 0
    Last Post: 03-05-2002, 08:49 PM

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