|
|
|
Right Triangle Calculations - Enhanced
Version
% cat rightTri_1.cpp
// Right triangle calculations
// alternate version
// With: streaming of multiple values using cin and cout
// formatting output with stram
functions and manipulators
// echoing input values
// using an arithmetic expression
in a cout statement
#include <iostream>
#include <cmath> //
math library
#include<iomanip> // output
stream manipulators
using namespace std;
int main()
{
// variable declarations
double leg_a, leg_b, hypotenuse, area;
// get input values
cout << "Enter legs a and b: ";
cin >> leg_a >> leg_b; // streaming
// set format flags
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(4);
// echo the input values
cout << "For legs a = " << leg_a << "
and b = " << leg_b
<< ":\n";
// compute the area in the output stream
// setw(n) sets the output field width
cout << "Area = " << setw(10) << 0.5 *
leg_a *leg_b << endl;
// compute the hypotenuse
hypotenuse = sqrt( leg_a * leg_a + leg_b *
leg_b );
cout << "Hypotenuse = " << hypotenuse <<
endl;
return 0;
}
% g++ rightTri_1.cpp
% a.out
Enter legs a and b: 3 4.987654
For legs a = 3.0000 and b = 4.9877:
Area = 7.4815
Hypotenuse = 5.8204
|
|