1

I have an assignment ask for a program:

Input:

  1. receive multiple line of input.
  2. each line contains 1-10 floating point numbers and separated by 1 or more white-space.

Output:

  1. the answer is the sum of each line ( it should round up to 4 decimal places) and it can be stored in variable with signed double type.
  2. each answer should be printed in a single line.
  3. there is a blank line between consecutive answer.

The sample input:

1 3 5 7 9
2 4.6 5.01

and the sample output:

25.0000

11.6100

I have tried many time and my program still cannot produce the correct output (I submit it to the online judgment system, which is offered by my school, and get "wrong answer" message.).

Here is my code:

#include <iostream>
#include <iomanip>
#include <cstring>
#include <math.h>

using namespace std;

const int N = 100;
char line[N];

int main () {
    while( fgets( line, N, stdin ) ) {
        char* word = strtok( line, " " );
        double result = 0.0000;

        while( word != NULL ) {
           if( isdigit(word[0]) || word[0] == '-' ) {
                result += atof(word);
           }
           word = strtok( NULL, " " );
        }

        int temp;
        if(result < 0) {
            temp = floor(result * 10000.0);
        } else {
            temp = ceil(result * 10000.0);
        }

        result = (double) temp/10000.0;

        cout << fixed << setprecision(4) << result << endl << endl;
    }

    return 0;
}

Here is my test case:

1 3 5 7 9
2 4.6 5.01
-1 -3 -5 -7 -9
-2 -4.6 -5.01
-1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587

and the corresponding result:

25.0000

11.6100

-25.0000

-11.6100

-15.5559

What is the problem in my code? I guess it may be related to the conversion from char* to double, this process may lead some loss and lead to the "wrong answer". Isn't it? If no, please tell me what wrong with it.

dbush
  • 205,898
  • 23
  • 218
  • 273
chan sum
  • 41
  • 1
  • 6
  • 2
    Consider: what number do you get when your round `-15.3` up? – 1201ProgramAlarm May 04 '20 at 04:16
  • My recommendation is that you convert the full string to a `double`, and adjust precision in your output of the value instead. – Some programmer dude May 04 '20 at 04:16
  • 6
    And if you want to learn C++, then please try to do it properly. The code you show only have two things that are C++-specific: The `#include ` (which isn't used) and `using namespace std;` (which isn't really used either, since you include the C headers, and which you [should not do](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)). I recommend you invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), or take a class or two. – Some programmer dude May 04 '20 at 04:18
  • Is range of input floating point numbers defined? The 100-character buffer (only at most 9 character + at least 1 separator per each 10 numbers) may not be sufficient. Also it is bad to use magic number `101` in `fgets()` while defining the buffer size constant `N`. Moreover, the value `101` is wrong because the buffer is only 100-character long. The size parameter should be `N`. – MikeCAT May 04 '20 at 05:07
  • 1
    Does this answer your question? [Is floating point math broken?](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) – the busybee May 04 '20 at 07:45
  • 2
    As @Someprogrammerdude already says, either use C (stdio.h, fgets, ...) or C++ (cstdio, better, iostream and std::cin). Mixing these languages is a bad idea! – ljrk May 04 '20 at 08:50
  • 1
    I don't understand the question. Some numbers should be rounded up to four decimal places, that's clear. But if those numbers were the sample input, why is the sample output so appearingly unrelated to the sample input? Please clarify the input and the expected output. How do sample input and sample output relate to each other? – Wör Du Schnaffzig May 04 '20 at 10:30
  • @thebusybee At first I thought that this was the issue, but the problem is that OP's code is not rounding the answer in the way that the problem description stipulates. – John Coleman May 04 '20 at 11:32
  • @MikeCAT the question just said floating point numbers (signed double). So I think the range of input floating point numbers should be the range of double. – chan sum May 04 '20 at 11:33
  • @pqans the output of a line is the sum of the corresponding line of the input. e.g. for input line 1, 1 3 5 7 9, the output line 1, should be 25.0000 (1+3+5+7+9). – chan sum May 04 '20 at 11:36
  • @1201ProgramAlarm I got -15.3000 when I input -15.3 to the program – chan sum May 04 '20 at 11:39
  • @JohnColeman Yeah, but understanding the imperfection of size-limited floating point leads directly to the correction. ;-) – the busybee May 04 '20 at 13:14
  • int(sum*10000+0.5)/10000.0 should give you a number rounded up to four decimal places – Wör Du Schnaffzig May 04 '20 at 15:58
  • What I meant was if you round `-15.3` up to an integer, do you get `-15` or `-16`? – 1201ProgramAlarm May 04 '20 at 20:23
  • @1201ProgramAlarm it should be -15, but why is it related to floating point? – chan sum May 05 '20 at 00:29
  • What is the exact value of your last answer? Is the displayed value rounded correctly? – 1201ProgramAlarm May 05 '20 at 00:31
  • @1201ProgramAlarm Sorry, I've forgotten to edit the code above after I modified it. Now, the program will output -15.5559 when I input -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 -1.555587 – chan sum May 05 '20 at 03:26
  • Is it possible to use string instead of char*? – Naseef Chowdhury May 06 '20 at 01:44

1 Answers1

0

The problem is with how you handle rounding. The assignment says it should round up to 4 decimal places but your code is rounding away from zero (ceil for positive, floor for negative).

You should use ceil for all numbers. Get rid of int temp;, all uses of temp, and replace the assignment to result with:

result = ceil(result * 10000.0) / 10000.0;
1201ProgramAlarm
  • 32,384
  • 7
  • 42
  • 56
  • I edit my code based on your command, but it still be "Wrong Answer". Do you think that the problem of my program may not be handling precision? – chan sum May 06 '20 at 01:29