Advertisement
Guest User

Untitled

a guest
Apr 26th, 2015
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int    menu();
  6. double squareRoot(double);
  7. double powerOf(double, int);
  8. void   primeNumbers(int);
  9. double hypotenuse(double, double);
  10.  
  11. int main()
  12. {
  13.   int option;
  14.  
  15.   //main loop
  16.   while ((option = menu()) != 5) {
  17.     int    n, y;
  18.     double a, b, p, x;
  19.  
  20.     switch (option) {
  21.       case 1: //Compute the square root of a double using the Babylonian algorithm
  22.         cout << "Enter a value to compute the square root of:";
  23.         cin >> p;
  24.  
  25.         cout << "The square root of " << p << " is " << squareRoot(p) << endl << endl;
  26.  
  27.         break;
  28.       case 2: //Compute X raised to the Y power
  29.         cout << "Enter a value:";
  30.         cin >> x;
  31.         cout << "What value would you like to raise it to?:";
  32.         cin >> y;
  33.  
  34.         cout << x << " raised to the power of " << y << " is " << powerOf(x, y) << endl << endl;
  35.  
  36.         break;
  37.       case 3: //Print out prime numbers up to a pre-specified number
  38.         cout << "Enter a number up to which all prime numbers will be printed:";
  39.         cin >> n;
  40.  
  41.         cout << "Prime numbers up to " << n << ":" << endl;
  42.         primeNumbers(n);
  43.         cout << endl;
  44.  
  45.         break;
  46.       case 4: //The value of the hypotenuse of a right triangle.
  47.         cout << "Enter the length of side A of the triangle:";
  48.         cin >> a;
  49.         cout << "Enter the length of side B of the triangle:";
  50.         cin >> b;
  51.  
  52.         cout << "The length of the hypotenuse of the triangle is " << hypotenuse(a, b) << endl << endl;
  53.  
  54.         break;
  55.     }
  56.   }
  57.  
  58.   return 0;
  59. }
  60.  
  61. int menu()
  62. {
  63.   int a = -999;
  64.  
  65.   do {
  66.     if (a != -999) //if the user enters an invalid input display this message
  67.       cout << "Invalid input, please try again." << endl << endl;
  68.     cout << "MatLab Junior" << endl
  69.          << "  1) Compute the square root of a double using the Babylonian algorithm" << endl
  70.          << "  2) Compute X raised to the Y power" << endl
  71.          << "  3) Print out prime numbers up to a pre-specified number" << endl
  72.          << "  4) The value of the hypotenuse of a right triangle." << endl
  73.          << "  5) Quit the Program" << endl
  74.          << "Please enter a number (1-5) ->";
  75.     cin >> a;
  76.   } while (!(a > 0 && a < 6)); //test for valid input
  77.  
  78.   cout << endl;
  79.   return a;
  80. }
  81.  
  82. double squareRoot(double n)
  83. {
  84.   int BABYLONIAN_ITERATIONS = 10; //number of times to repeat babylonian algorithm
  85.  
  86.   double guess = n;
  87.  
  88.   //compute square root
  89.   for (int i = 0; i < BABYLONIAN_ITERATIONS; i++) {
  90.     double tmp = n / guess;
  91.     guess = (guess + tmp) / 2;
  92.   }
  93.  
  94.   //4 decimal places
  95.   guess *= 10000;
  96.   guess = (int)guess;
  97.   guess /= 10000;
  98.  
  99.   return guess;
  100. }
  101.  
  102. double powerOf(double x, int y)
  103. {
  104.   double p = x;
  105.  
  106.   //multiply by itself y times
  107.   for (int i = 1; i < y; i++)
  108.     p *= x;
  109.  
  110.   return p;
  111. }
  112.  
  113. void primeNumbers(int n)
  114. {
  115.   // all numbers from 2 to n
  116.   for (int i = 2; i < n; i++) {
  117.     bool isPrime = true;
  118.    
  119.     //test if i is prime
  120.     for (int j = 2; j <= squareRoot(i); j++) {
  121.       if (i % j == 0)
  122.         isPrime = false;
  123.     }
  124.  
  125.     //if it is print it
  126.     if (isPrime)
  127.       cout << i << endl;
  128.   }
  129. }
  130.  
  131. double hypotenuse(double a, double b)
  132. {
  133.   //a^2 + b^2 = c^2
  134.   double c;
  135.   c = powerOf(a, 2) + powerOf(b, 2);
  136.   c = squareRoot(c);
  137.   return c;
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement