The objective of this Exercise is to illustrate the use of formatted output for
ID: 3884417 • Letter: T
Question
The objective of this Exercise is to illustrate the use of formatted output for the irrational number pi (pi) and to demonstrate the difference in accuracy between float variables and double variables. Write a C++ computer program that does the following: 1. Declares a constant float variable for pi. For example: const float PIF = 3.1415926535 2. Prints PIF accurate to 2 decimal places 3. Prints PIF accurate to 4 decimal places 4. Prints PIf accurate to 10 decimal places 1. Declares a constant double variable for pi. For example: const double PId = 3.1415926535 2. Prints PId accurate to 2 decimal places 3. Prints PId accurate to 4 decimal places 4. Prints PId accurate to 10 decimal places Declare a character variable to print the symbol for pi The ascii code for pi is 227 Your output may look something like the following: Things to note: You should use the declarative for constant: const. Variables that do not change their value during the course of the program and or have defined constant values, such as the irrational numbers pi and e should be declared constant. You do not have to use the variable names PIf or PId. A good naming convention to follow is to always use capital letters for a variable name that is constant. Use setprecision to determine the number of decimal places to be printed. In reviewing the output window, note that even though the assigned value for PI is accurate to 10 decimal places in both cases, the printed values differ after 6 decimal places. Can you draw any conclusions?Explanation / Answer
Program :
#include<iostream>
#include<iomanip>
using namespace std;
int main(){
const float PIf = 3.1415926535;
const double PId = 3.1415926535;
//
cout<<"Harriette Roadman"<<" "<<"Excercise 3A"<<" "<<"August 14, 2016"<<" ";
cout<<" PI declare float ";
cout<<"---------------------- ";
cout<<"u03C0 to 2 decimal places ";
std::cout << std::fixed << std::setprecision(2) << PIf<<" ";
cout<<"u03C0 to 4 decimal places ";
std::cout << std::fixed << std::setprecision(4) << PIf<<" ";
cout<<"u03C0 to 10 decimal places ";
std::cout << std::fixed << std::setprecision(10) << PIf<<" ";
cout<<" PI declare double ";
cout<<"---------------------- ";
cout<<"u03C0 to 2 decimal places ";
std::cout << std::fixed << std::setprecision(2) << PId<<" ";
cout<<"u03C0 to 4 decimal places ";
std::cout << std::fixed << std::setprecision(4) << PId<<" ";
cout<<"u03C0 to 10 decimal places ";
std::cout << std::fixed << std::setprecision(10) << PId<<" ";
return 0;
}
//End of program
//here in program value differ after 6 places in case of double and float because
//double has more precision bits than float so double gives more accurate results
//hexadecimal presentation of 3.3232323232
// float - 0x4054AFD7
//double - 0x400A95FAD4093B9A
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.