Arithmetic calculation anomaly

  arithmetic-expressions, c++, math, visual-c++

I was trying to solve this exercise: https://www.codeabbey.com/index/task_view/fahrenheit-celsius
However, here is the solution:

#include <iostream>

using std::cout;
using std::cin;

int main()
{
    int n, a;
    cin >> n;
    int* answers = new int[n]; // allocating memory
    for (int i = 0; i < n; i++)
    {
        cin >> a;
        answers[i] = (a - 32) * 5/9;
    }

    for (int i = 0; i < n; i++)
    {
        cout << answers[i] << ' ';
    }

    cout << 'n';
    delete[]answers; //deallocating memory

    system("pause");
    return 0;
}

Now, notice when I change answers[i] = (a - 32) * 5/9; to answers[i] = (a - 32) * (5/9);.
Here, is the difference in the output respectively:

  1. Without the brackets:
    enter image description here
  2. With the brackets:
    enter image description here
    What is this sorcery?

EDIT:
I understand why this can seem as a duplicate. My concern is not why 5/9 outputs 0. That is not my concern. My concern is what is the difference between the two following code:
– `answers[i] = (a – 32) * 5/9;`
– `answers[i] = (a – 32) * (5/9);`
When I do not use brackets, it works. But, when I use brackets it just outputs 0. So, the question is what is the bracket operator changing here? Please read the question carefully. Thanks!
Source: Windows Questions C++

LEAVE A COMMENT