Exercise URI 1012 - Wrong answer 20%

0

I did the exercise, almost all the outputs are coming out as the URI output is asking, however the first requirement is not as it asks in the program. I used the setprecision, but I'm not sure why the first result does not round 3 decimal places after the comma. to 3.0 b 4.0 c 5.2

TRIANGULO: 7.800
CIRCULO: 84.949
TRAPEZIO: 18.200
QUADRADO: 16.000
RETANGULO: 12.000

My first TRIANGLE output leaves only 7.8 and the others exit perfectly.

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

int main(int argc, char** argv)
{
    double pi = 3.14159;
    double a, b, c;
    double area_triangulo, area_circulo, area_trapezio, 
    area_quadrado,area_retangulo;

    std::cin >> a >> b >> c;

    area_triangulo = a * c / 2;
    area_circulo = pi * pow(c, 2);
    area_trapezio = ((a + b)* c)/2;
    area_quadrado = pow(b, 2);
    area_retangulo = a * b;

    std::cout << "TRIANGULO: " <<area_triangulo << std::setprecision(5)<<           
    std::fixed << std::endl;    
    std::cout << "CIRCULO: " <<area_circulo << std::setprecision(3)<< 
    std::fixed << std::endl;    
    std::cout << "TRAPEZIO: " <<area_trapezio << std::setprecision(3)<< 
    std::fixed << std::endl;
    std::cout << "QUADRADO: " <<area_quadrado << std::setprecision(3)<< 
    std::fixed << std::endl;
    std::cout << "RETANGULO: " <<area_retangulo << std::setprecision(3)<< 
    std::fixed << std::endl;

    return 0;
}
    
asked by anonymous 02.01.2018 / 22:48

1 answer

1

std::fixed indicates that the following writes will be done with fixed-point notation. The number of digits shown after the comma is set with setprecision .

If you want to display 3 digits after the comma, you should use:

std::setprecision(3);

You should use std::fixed and set::setprecision before writing any numeric output and only once:

std::cout << std::fixed << std::setprecision(3);
std::cout << "TRIANGULO: " <<area_triangulo << std::endl;
std::cout << "CIRCULO: " <<area_circulo << std::endl;
std::cout << "TRAPEZIO: " <<area_trapezio << std::endl;
std::cout << "QUADRADO: " <<area_quadrado << std::endl;
std::cout << "RETANGULO: " <<area_retangulo << std::endl;

View the result on Ideone

    
03.01.2018 / 00:57