Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6.  
  7. // Function prototype (declaration)
  8. int add(int, int);
  9. double doubleadd(double, double);
  10. float squareroot(float);
  11.  
  12. int main()
  13. {
  14. int bignum = 2147483646;
  15.  
  16. //1. Show that for unsigned int a,b and a>0, b>0, we can get a+b < a
  17. //2. Show that for int a,b and a>0, b>0, we can get a+b < 0
  18. cout << "Two positive numbers added = ";
  19. int num = add(bignum , 2 );
  20. cout << num << endl;
  21.  
  22. //3. Show that for int a,b and a<0, b<0, we can get a+b > 0
  23. int bignegnum = bignum * -1;
  24. int num2 = add(bignegnum , -10);
  25. cout << "Two negative numbers added = " << num2 << endl;
  26.  
  27. //4. Show that for double x and x>0 we can get 1. + x = = 1.
  28. double dnum1 = 1.0;
  29. double dnum2 = .00000000000000000001;
  30. dnum1 = doubleadd(dnum1, dnum2);
  31. cout << "Two double numbers added = " << dnum1 << endl;
  32.  
  33. //5. Show that for double a,b,c in some cases (a+b)+c != (c+b)+a
  34. dnum1 = 2000000;
  35. dnum2 = 1e20;
  36. double dnum3 = -1e20;
  37. double dnum4 = (dnum1 + dnum2) + dnum3;
  38. double dnum5 = (dnum3 + dnum2) + dnum1;
  39. cout<< "The two different numbers when added in different order: " << dnum4 << " and " << dnum5 <<endl;
  40.  
  41. //6. Show the results of the following power function: pow(-2., 3), pow(-2., 3.0) , pow(-2., 3.00000000001)
  42. double powBase = -2.;
  43. double powExp = 3;
  44. double powNum = pow(powBase, powExp);
  45. cout << " Pow(-2.3,3) = " << powNum << endl;
  46.  
  47. powExp = 3.0;
  48. powNum = pow(powBase, powExp);
  49. cout << " Pow(-2.,3.0) = " <<powNum << endl;
  50.  
  51. powExp = 3.00000000001;
  52. powNum = pow(powBase, powExp);
  53. cout << " Pow(-2.,3.00000000001) = " <<powNum << endl;
  54.  
  55. //7. Show the memory size of the following constants 1. , 1.F, 1 , '1' , and "1"
  56. cout<<"Size of (1.) : " <<sizeof(1.)<< endl;
  57. cout<<"Size of (1.F) : "<<sizeof(1.F) << endl;
  58. cout<<"Size of (1) : "<<sizeof(1)<< endl;
  59. cout<<"Size of ('1') : "<<sizeof('1')<< endl;
  60. cout<<"Size of (\"1\") : "<<sizeof("1")<< endl;
  61.  
  62. //8. Display 1./3. using 20 digits and show the correct and incorrect digits
  63. cout<<"1./3: " << setprecision(20) << 1./3 <<endl;
  64.  
  65. /* 9. Display all printable characters of the ASCII table in 3 columns:
  66. first column: 32-63, second column: 64-95, third column: 96-127. Each column
  67. must include the numeric value and the corresponding character. Following is
  68. an example of one of 32 rows in the ASCII table:
  69. 33 ! 65 A 97 a */
  70.  
  71. for (int i = 32; i< 63; i++){
  72. cout << i << " " << (char)i <<"\t" << (i+32) <<" " << (char)(i+32) << "\t" << (i+64) << " " << (char)(i+64) <<endl;
  73. }
  74.  
  75. //10. Compute sqrt(2.) using your own program for square root function.
  76. cout<< "Square root of (2.) is " << squareroot(2);
  77.  
  78. return 0;
  79. }
  80.  
  81.  
  82. int add(int a, int b)
  83. {
  84. int sum;
  85. sum = a + b;
  86. return sum;
  87. }
  88.  
  89.  
  90. double doubleadd(double a, double b){
  91.  
  92. double sum;
  93. sum = a + b;
  94. return sum;
  95. }
  96.  
  97. float squareroot(float a)
  98.  
  99. {
  100. float root = a/2.0;
  101. float temp = 0;
  102.  
  103. while (root != temp)
  104. {
  105. temp = root;
  106. root = ( a / temp + temp) / 2;
  107. }
  108. return root;
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement