Didn"t forge 2018-11-05 03:08 采纳率: 25%
浏览 240

外汇基金资产负债表摘要及

I can't seem to figure out whats wrong with my code, but I'm receiving incorrect values for simple inputs like 1 or 2 but correct inputs for .41. If someone could help me out It'd be greatly appreciated!

This is my code:

#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)

{
    //Establish Variables
    float amount_owed;
    int c = 0;
    //Get Valid Input from User
    do
    {
        amount_owed = get_float ("Change Owed: ");
    }   while (amount_owed <= 0);

    //Check for quarters, mark  # of quarters that can be used, subtract value from original amount_owed
    do
    {
        (c++);
        (amount_owed = amount_owed - .25);
    }   while (amount_owed >= .25);

    //Check for dimes, mark # of dimes that can be used, subtract value from original amount_owed

    do
    {
        (c++);
        (amount_owed = amount_owed - .1);
    }   while ((amount_owed >= .1) && (amount_owed < .25));

    //Check for Nickels, mark $ of nickels that can be used, subtract value from original amount_owed

    do
    {
        (c++);
        (amount_owed = amount_owed - .05);
    }   while ((amount_owed >= .05) && (amount_owed < .1));

    //Check for Pennies, mark # of pennis that can be used, subtract value from original amount_owed

    do
    {
        (c++);
        (amount_owed = amount_owed - .01);
    }   while ((amount_owed >= .01) && (amount_owed < .05));
    //Print Number of Minimum number of coins that can be used

    {
       if (amount_owed == 0)
       ;
        printf("%d\n", c);
    }
}

转载于:https://stackoverflow.com/questions/53147814/cash-c-expected-18-n-not-22-n

  • 写回答

1条回答 默认 最新

  • Didn"t forge 2018-11-05 07:23
    关注

    To start with you should never use float for somethin that need to be accurate. But let's get back to that later as there is another issue with your program.

    For now just assume that float is actually precise.

    When you write:

    do
    {
        c++;
        ...
    } while(...);
    

    the code in the body will always be executed at least once.

    So with your code if the input is 1.0 the first loop will be executed 4 times and amount_owed will be 0.0.

    When for the next 3 do-while you will still go into the body once and do the c++ (even though amount_owed is zero). Consequently you result will be 7 instead of 4 (that 4 from the first loop + 1 from each of the three following loop).

    The solution is to use regular while instead of do-while. Like:

    #include <stdio.h>
    
    int main(void) {
        float amount_owed = 1.0;
        int c = 0;
    
        while (amount_owed >= 0.25)
        {
            c++;
            amount_owed = amount_owed - 0.25;
        }
    
        while ((amount_owed - 0.1) >= 0)
        {
            c++;
            amount_owed = amount_owed - 0.1;
        }
    
        while (amount_owed >= .05)
        {
            c++;
            amount_owed = amount_owed - .05;
        }
    
        while (amount_owed >= .01)
        {
            c++;
            amount_owed = amount_owed - .01;
        }
    
        printf("%d\n", c);
    
        return 0;
    }
    

    Back to using float : floats can't represent every number 100% accurate. So when doing calculations involving floats, you will likely see some rounding errors. So for any calculation that need an accurate result, you should try to do it using integers.

    For a task like this the "trick" is to consider amount_owed as being in units of the lowest coins you have. Typically that means 100 times the "normal" way of thinking about it. For instance, instead of 1.17 you use 117.

    Then your code could be more like:

    #include <stdio.h>
    
    int main(void) {
        unsigned amount_owed = 100;  // note 100 instead of 1.0
        int c = 0;
    
        while (amount_owed >= 25)  // note 25 instead of .25
        {
            c++;
            amount_owed = amount_owed - 25;
        }
    
        while ((amount_owed - 10) >= 0)
        {
            c++;
            amount_owed = amount_owed - 10;
        }
    
        . . . 
    
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本