Thread: How to make this work =(

  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, 07: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, 12:39 PM
  5. cant make it work
    By papous27 in forum Linux Programming
    Replies: 0
    Last Post: 03-05-2002, 09:49 PM