Question

In: Computer Science

Write a C++ main program that has the following 5 short independent segments. 6. Show the...

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.

Solutions

Expert Solution

#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


Related Solutions

Write a C++ main program that has the following 5 short independent segments. 1. Show that...
Write a C++ main program that has the following 5 short independent segments. 1. Show that for unsigned int a,b and a>0, b>0, we can get a+b < a 2. Show that for int a,b and a>0, b>0, we can get a+b < 0 3. Show that for int a,b and a 0 4. Show that for double x and x>0 we can get 1. + x = = 1. 5. Show that for double a,b,c in some cases (a+b)+c...
Write a C++ main program that has the following: 9. Display all printable characters of the...
Write a C++ main program that has the following: 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
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt...
c++ please Write and testa C++ main program that: declare an array arrof 6 integers Prompt the user for 6 integer values and store them in arr. Prompt the user for a target integer target. Search for targetin arr. If targetis found to match an element in the arr, then the program prints out a message which contains the address of the found element, otherwise, if no element found then the message “the element target not found” will be printed...
6- Write a C++ program that determines the amount of memory used by char, short int,...
6- Write a C++ program that determines the amount of memory used by char, short int, unsigned short int, float, and double types and display the information on the screen. You program should also display the typical range for each data type.
C++ Write a program that has two functions. The 1st function is the main function. The...
C++ Write a program that has two functions. The 1st function is the main function. The main function should prompt the user for three inputs: number 1, number 2, and an operator. The main function should call a 2nd function called calculate. The 2nd function should offer the choices of calculating addition, subtraction, multiplication, and division. Use a switch statement to evaluate the operator, then choose the appropriate calculation and return the result to the main function.
For the following program segments, write a program that shows the estimated runtime for each piece....
For the following program segments, write a program that shows the estimated runtime for each piece. Run it on your computer when n=1, 10, 100, 1000, 10000, 100000, 1000000 for ten times each so that you can observe the differences in performance among these segments. Segment1:        for (sum=0, i=1; i<=n; i++)                         sum = sum + i; Segment2:        for (sum=0, i=1; i<=n; i++)                                                 for (j=1; j<=i; j++)                                                             sum++; Segment3:        sum= n * (n+1)/2
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for...
C++ Write a program with the following elements: in main() -opens the 2 files provided for input (Lab_HW9_2Merge1.txt and Lab_HW9_2Merge2.txt) -calls a global function to determine how many lines are in each file -creates 2 arrays of the proper size -calls a global function to read the file and populate the array (call this function twice, once for each file/array) -calls a global function to write out the 'merged' results of the 2 arrays *if there are multiple entries for...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each...
Write a program in C (NOT C++ or C#) The program inputs 5 elements into each of 2 integer arrays. Multiply corresponding array elements, that is, arrayOne[0] * arrayTwo[0], etc. Save the product into a third array called prodArray[ ]. Display the product array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT