Guest User

Untitled

a guest
Jun 30th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.26 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int checkPaymentAmount(float amount, float balance, float interest); //forward declaration of the function, nasty C necessity
  5.  
  6. int main()
  7. {
  8.     //declaration of the variables we'll be using
  9.     float balance, lowerBound, upperBound, interest, midValue;
  10.     int check;
  11.  
  12.     cin >> balance; //input of the balance
  13.     cin >> interest; //input of the annual interest
  14.     lowerBound = balance / 12; //given in the pdf
  15.     upperBound = (balance + balance * interest) / 12; //given in the pdf (I think lol)
  16.  
  17.     bool inLoop = true; //we will use this boolean later to check if we have found the correct amount
  18.     while (inLoop)
  19.     {
  20.         midValue = lowerBound + (upperBound - lowerBound) / 2; //we calculate the interval size, divide it by two and add that to the lowerBound, this will be our center value
  21.         check = checkPaymentAmount(midValue, balance, interest);
  22.         if (check == 0)
  23.             inLoop = false;
  24.         if (check == 1)
  25.             lowerBound = midValue;
  26.         if (check == -1)
  27.             upperBound = midValue;
  28.     }
  29.  
  30.     printf("%.2f", midValue); //this will print the answer with a precision of 2 decimals
  31.  
  32.     return 0;
  33. }
  34.  
  35. //returns 1 if amount needs to be higher, -1 is it needs to be lower, 0 if it's acceptable
  36. int checkPaymentAmount(float amount, float balance, float interest)
  37. {
  38.     float tempBalance = balance;
  39.  
  40.     //first we need to calculate the amount payed off with the original balance, to see if this will be sufficient
  41.     for (int i = 0; i < 12; i++)
  42.     {
  43.         tempBalance = tempBalance * (1 + interest/12) - amount; //this is also used in the first problem I think
  44.     }
  45.  
  46.     if (tempBalance > 0)
  47.         return 1; //if you still have dept after paying this amount monthly, your amount was not high enough
  48.  
  49.     //this code below will only be executed if we've paid too much. We need to find out if we could've paid less and still pay off.
  50.     //We will do this by checking if the amount - 0.01 gives us an amount in debt
  51.  
  52.     tempBalance = balance;
  53.     amount = amount - 0.01;
  54.     for (int i = 0; i < 12; i++)
  55.     {
  56.         tempBalance = tempBalance * (1 + interest/12) - amount;
  57.     }
  58.  
  59.     if (tempBalance > 0)
  60.         return 0; //We have a dept now, so our initial amount was the littlest amount possible and we need to stop the loop
  61.     else
  62.         return -1; //this value was too high, so we need to search lower values
  63. }
Advertisement
Add Comment
Please, Sign In to add comment