In: Computer Science
Write a C++ main program that has the following 5 short independent segments.
6. Show the results of the following power function: pow(-2., 3), pow(-2., 3.0) , pow(-2., 3.00000000001)
7. Show the memory size of the following constants 1. , 1.F, 1 , '1' , and "1"
8. Display 1./3. using 20 digits and show the correct and incorrect digits
9. Display all printable characters of the ASCII table in 3 columns: first column: 32-63, second column: 64-95, third column: 96-127. Each column must include the numeric value and the corresponding character. Following is an example of one of 32 rows in the ASCII table: 33 ! 65 A 97 a
10. Compute sqrt(2.) using your own program for square root function.
#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
//user defind sqrt function
float sqrt(float x)
{
float sqr=x/2.0;
float temp=0;
while (sqr!=temp)
{
temp = sqr;
sqr = ( x/temp + temp) / 2;
}
return sqr;
}
int main()
{
cout<<"Segment 1:"<<endl;
//calculating the power with the math function
cout<<"pow(-2.,3):"<<pow(-2,3)<<endl;
cout<<"pow(-2.,3.0):"<<pow(-2,3.0)<<endl;
cout<<"pow(-2., 3.00000000001)"<<pow(-2., 3.00000000001)<<endl;
cout<<"Segment 2:"<<endl;
//getting the size of each data type
cout<<"sizeof(1.):"<<sizeof(1.)<<endl;//double type
cout<<"sizeof(1.F):"<<sizeof(1.F)<<endl;//float type
cout<<"sizeof(1):"<<sizeof(1)<<endl;//integer
cout<<"sizeof('1'):"<<sizeof('1')<<endl;//character
cout<<"sizeof(\"1\"):"<<sizeof("1")<<endl;//string
cout<<"Segment 3:"<<endl;
//checking how many decimals are getting correct
// as the type is double we can get 16 digit accuracy and remaing four digits are not accurate
cout<<"1./3:"<<setprecision(20)<<1./3<<endl;
cout<<"Segment 4:"<<endl;
//displaying ascii table
for(int i=32,j=64,k=96;i<=63&&j<=95&&k<=127;i++,j++,k++)
{
cout<<i<<" "<<(char)i<<"\t"<<j<<" "<<(char)j<<"\t"<<k<<" "<<(char)k<<endl;
}
//sqrt user defined function
cout<<"Segment 5:"<<endl;
cout<<"sqrt(2):"<<sqrt(2)<<endl;
}
output:
note: if you have any doubt please comment
i executed this file in ubuntu with g++ (GNU) compiler
thankyou