-5

Can someone explain why the outcome is different in my code?

I was trying out the easy problems in Codechum when I encountered something confusing, this was my code.

#include <iostream>
#include <iomanip>

using namespace std;

int num1, num2, num3;
double avg;

int main(){

    cout << "Enter three numbers: ";
    cin >> num1 >> num2 >> num3;

    avg = (num1+num2+num3) / 3;

    cout << setprecision(2) << fixed << "Average of the three numbers: " << avg; 

    return 0;
}

I was confused as to why I only got 3 out of the 6 test cases correct, I looked everywhere on my code and I still had no idea so I viewed the solution and noticed that there's a ".0" on the divisor of the solution code, so I tried it and it worked on all 6 test cases. This was my code after

#include <iostream>
#include <iomanip>

using namespace std;

int num1, num2, num3;
double avg;

int main(){

    cout << "Enter three numbers: ";
    cin >> num1 >> num2 >> num3;

    avg = (num1+num2+num3) / 3.0;

    cout << setprecision(2) << fixed << "Average of the three numbers: " << avg; 

    return 0;
}

I might have missed something so small or overlooked a very simple idea...

Guillaume Racicot
  • 39,621
  • 9
  • 77
  • 141
Clorky
  • 1
  • 2
  • 5
    integer division against floating division. – Jarod42 Aug 27 '23 at 17:27
  • This is not `C`. Your problem is integer division. When you do something like `3/2` you would expect to get `1.5` but with integer division you loose anything after the decimal point so you are left with `1`. When you use a `double` (your `3.0` is a double literal), this wont happen. – Joel Aug 27 '23 at 17:27
  • I removed the C tag since it's not a C question – Guillaume Racicot Aug 27 '23 at 17:28
  • storing the result into a double variable doesn't change the type of division. only its operand change the type of division. – Jarod42 Aug 27 '23 at 17:28
  • 9
    To be brutally honest, if you don't know the difference between `3` and `3.0` in C++, then you have skipped quite a lot of the basics that you need to actually be able to program in C++. And you should definitely not try to use so-called "judge", "assessment" or "competition" sites as a learning resource, because they are *not*. Instead invest in [some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and take actual classes. – Some programmer dude Aug 27 '23 at 17:31
  • *I might have missed something so small or overlooked a very simple idea* -- This is covered in any C++ book, right at the first or second chapter. `(num1 + num2 + num3) / 3` -- that is all integer division, giving you an integer result. It doesn't matter what the variable type is on the left side of the `=` sign. – PaulMcKenzie Aug 27 '23 at 17:32
  • I don't know CodeChum that well, and can't find any reviews. But good sources to learn cpp from are : A [recent C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) or have a go at https://www.learncpp.com/ (that's pretty decent, and pretty up-to-date). For C++ reference material use : [cppreference](https://en.cppreference.com/w/). And after you learned the C++ basics from those sources, look at the [C++ coreguidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines) regularely to keep up-to-date with the latest guidelines. – Pepijn Kramer Aug 27 '23 at 17:58
  • Before using CodeChum — at all — you should first learn C++. – Eljay Aug 27 '23 at 20:21

2 Answers2

3

In this expression:

(num1+num2+num3) / 3

Both operands of the division operator have integer type, so integer division is performs which truncates any fractional part. While is this expression:

(num1+num2+num3) / 3.0

The constant 3.0 has floating point type, and since this is one of the operands of the division operator, floating point division occurs which retains any fractional portion.

dbush
  • 205,898
  • 23
  • 218
  • 273
2

The difference between this:

(num1+num2+num3) / 3;

and this:

(num1+num2+num3) / 3.0;

is that the latter expressions is (automatically) changed by the compiler to

((double)(num1+num2+num3)) / 3.0;

That is, in the first case the division is done in ints and truncated, and in the second case it's done in doubles.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362