Assignment 5.3

Moderators: vinyard, KreepingMonkey

Post Reply
Message
Author
User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Assignment 5.3

#1 Post by vinyard » Wed Oct 03, 2007 2:44 pm

Here is assignment 5.3.
Programming Exercises: Pg. 229, #3 wrote:Daphne invests $100 at 10% simple interest. That is, every year, the investment earns 10% of the original investment, or $10 each and every year:

interest = 0.10 * original balance

At the same time, Cleo invest $100 at 5% compound interest. That is, interest 5% of the current balance, including previous additions of interest:

interest = 0.05 * current balance

Cleo earns 5% of $100 the first year, giving her $105. The next year she earns 5% of $105, or $5.25, and so on. Write a program that finds out how many years it takes for the value of Cleo's investment to exceed the value of Daphne's investment and then displays the value of of both investments at that time.
This will need a loop (for,while,or do) to add the interests of the investments, both Cleo's & Daphne's, and count how many times it loops (years). One loop should do the trick but lets see what you guys can come with.

I will post mine when I am done writing it.
- vinyard.

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: Assignment 5.3

#2 Post by zero86ea » Wed Oct 03, 2007 3:03 pm

I wiill try to do as much as i can...although im sure i'm not going to be able to do it.
So Vinyard i hope you won't mind giving me a hand :P
Image

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Re: Assignment 5.3

#3 Post by vinyard » Wed Oct 03, 2007 8:04 pm

Okay. Here is my work. I added a few more things to the program but that was because I was bored. I can still fix a few things but I got tired. I will edit and refine the code later on. Anyways, here it is:

Code: Select all

/*Assignment 5.3*********
**Code by: David Rivera**
**10.03.2007************/
#include <iostream>

using std::cout;
using std::cin;
using std::endl;

/* Declare Functions */
void getInterestType(int p);
void getName(int p);
void getStartAmount(int p);
void getInterestPercentage(int p);
double compoundInterest(double percentage, double currentAmount);
double simpleInterest(double percentage, int startAmount,  double currentAmount);

const int length = 30;                                                                                                                        //Names longer than 29 characters are not supported.

/* Create the template for each person's info */
struct personInfo
{
       char name[length];                                                                                                                     //Person name
       int startAmount;                                                                                                                       //Users investment start amount
       double interestPercent;                                                                                                                //Users interest percent
       int interestType;                                                                                                                      //Users interest type
       double currentAmount;                                                                                                                  //users current balance
};

personInfo personOne;                                                                                                                         //Create first user info
personInfo personTwo;                                                                                                                         //Create second user info

int main()
{   
    getName(1);                                                                                                                               //Get first user name (In assignment "Daphne")
    getStartAmount(1);                                                                                                                        //Get first users investment start amount (in assignment "$100")
    getInterestPercentage(1);                                                                                                                 //First users interest percentage rate (in assignment "10%")
    getInterestType(1);                                                                                                                       //Get first users investment type [simple or compound ( in assignment "simple")
    
    getName(2);                                                                                                                               //Get second users name (in assignment "Cleo")
    getStartAmount(2);                                                                                                                        //Get second users investment start amount (in assignment "$100")
    getInterestPercentage(2);                                                                                                                 //Second users interest percentage rate (in assignment "5%")
    do                                                                                                                                        //interest type loop
    {
      getInterestType(2);                                                                                                                     //Get second users investment type [simple or compound ( in assignment "compound")
    } while(personOne.interestType == personTwo.interestType);                                                                                //Check if interest type is the same for user one & two, loop if true.
    
    int years = 0;                                                                                                                            //Years it will take
    char personAhead[length];                                                                                                                 //Which person starts with simple interest
    char personBehind[length];                                                                                                                //Which person starts with simple interest
    personOne.currentAmount = personOne.startAmount;                                                                                          //Assign the start amount
    personTwo.currentAmount = personTwo.startAmount;                                                                                          // to current amount
    
    if(personOne.interestType == 1 && personTwo.interestType == 2)                                                                            //Check which user starts with simple interest method
    {
        for(int i =0; i <= length; i++)                                                                                                       //Loop for asigning character array tp another array
        {                   
            personAhead[i] = personOne.name[i];                                                                                               //Assign the person with simple method to be ahead
            personBehind[i] = personTwo.name[i];                                                                                              //assign the person with compound method to be behind 
        }                                                                                                                                                           
        do
        {
             personOne.currentAmount = simpleInterest(personOne.interestPercent, personOne.startAmount, personOne.currentAmount);             //Simple interest calculation
             personTwo.currentAmount = compoundInterest(personTwo.interestPercent, personTwo.currentAmount);                                  //Compound interest calculation
             years++;                                                                                                                         //Add a year
        } while(personOne.currentAmount >= personTwo.currentAmount);                                                                          //Do this loop while person ahead amount is greater or equal than person behind
    } else {                                                                                                                                  //If first check is not true then flip person ahead and person behind                          
        for(int i =0; i <= length; i++)                                                                                                       //Loop for assigning character array to another array
        {
                personAhead[i] = personTwo.name[i];                                                                                           //Assign the person with simple method to be ahead
                personBehind[i] = personOne.name[i];                                                                                          //assign the person with compound method to be behind 
        }
        do
        {
             personOne.currentAmount = compoundInterest(personOne.interestPercent, personOne.currentAmount);                                  //Compound interest calculation
             personTwo.currentAmount = simpleInterest(personTwo.interestPercent, personTwo.startAmount, personTwo.currentAmount);             //Simple interest calculation
             years++;                                                                                                                         //Add a year.
        } while(personOne.currentAmount <= personTwo.currentAmount);                                                                          //Do this loop while person ahead amount is greater or equal than person behind
    }
    
    cout << "It will take " << years << " years for " << personBehind << "'s investment to exceed "                                           //Output how many years it will take for person with                      
         << personAhead << "'s investment." << endl;                                                                                          // compound interest to pass person with simple interest.
    cout << personOne.name << "'s final balance: " << personOne.currentAmount << endl;                                                        //Output person one's final balance
    cout << personTwo.name << "'s final balance: " << personTwo.currentAmount << endl;                                                        //output person two's final balance
    
    system("pause");                                                                                                                          //wait for user to press a key
    return 0;                      
                                  
}

/* Function - get user names */
void getName(int p)
{
     if(p == 2 )                                                                                                                              //Check which user we are getting the information for
     {
          cout << "Please enter the name of person two." << endl << "Person Two Name: ";                                                      //prompt the user person name
          cin >> personTwo.name;                                                                                                              //Assign value to person 2 name
     } else {
          cout << "Please enter the name of person one." << endl << "Person One Name: ";                                                      //prompt the user
          cin >> personOne.name;                                                                                                              //Assign value to person 1 name
     }
}

/* Function - get user start amounts */
void getStartAmount(int p)
{
     cout << "Enter the starting investment amount." << endl << "Investment Amount: $";                                                      //prompt the user
     if(p == 2 )                                                                                                                             //Check which user we are getting the information for
     {
             cin >> personTwo.startAmount;                                                                                                   //Assign value to person 2 start amount
     } else {
             cin >> personOne.startAmount;                                                                                                   //Assign value to person 1 start amount
     }
}

/* Function - get user interest percentages */
void getInterestPercentage(int p)
{
    cout << "Enter the interest percentage." << endl << "Interest Percentage: ";                                                             //prompt the user
    if(p == 2)                                                                                                                               //Check which user we are getting the information for
    {
         cin >> personTwo.interestPercent;                                                                                                   //Assign value to person 2 interest percent
         personTwo.interestPercent /= 100;                                                                                                   //Convert to decimal value
    } else {
         cin >> personOne.interestPercent;                                                                                                   //Assign value to person 1 interest percent
         personOne.interestPercent /= 100;                                                                                                   //Convert to decimal value
    }
}

/* Function - get user interest types */
void getInterestType(int p)
{
     cout << "Enter the interest type." << endl << "( 1 = simple; 2 = compound )" << endl << "Interest Type: ";                                  //prompt the user
    if(p == 2)                                                                                                                               //Check which user we are getting the information for
    {
         cin >> personTwo.interestType;                                                                                                      //Assign value to person 2 interest type
         if(personTwo.interestType == personOne.interestType)
         {
               cout << personTwo.name << "'s interest type cannot match "                                                                    //If person one & person two interest types match
                    << personOne.name << "'s interest type." << endl;                                                                        //prompt user to choose a different type
               getInterestType(2);                                                                                                           //loop the function
         }                                                                                                   
    } else {
           cin >> personOne.interestType;                                                                                                    //Assign value to person 1 interest type
    }
}

/* function - Calculate Comopund interest */
double compoundInterest(double percentage, double currentAmount)
{
    currentAmount += currentAmount * percentage;                                                                                             //multiply current amount by percentage, then add it to current amount
    return currentAmount;                                                                                                                    // return the value
}

/* function - Calculate simple interest */
double simpleInterest(double percentage, int startAmount, double currentAmount)
{
    currentAmount += startAmount * percentage;                                                                                               //Multiply start amount by percentage the add it to current amount
    return currentAmount;                                                                                                                    // return the value
}
                                                                                     /* PROGRAM END */
It's a bit long because of the extra work I did. Here is the screen shot of the output:
Notice the values are not hard coded into the program, you have to input the values yourself.
Notice the values are not hard coded into the program, you have to input the values yourself.
5_3.jpg (44.6 KiB) Viewed 1330 times
If you guys need any help with this or don't understand the code just post & I'll do my best. ;)

Update: I've fixed an error in the code where if you put the users in flipped, as in Cleo's information first then Daphne's, it give the incorrect balance & years it would take. Also added a check to make sure that both person's interest type differ from one another.

- Vinyard
Last edited by vinyard on Fri Oct 05, 2007 1:33 pm, edited 7 times in total.
Reason: Fixed coments. Updated code.
- vinyard.

User avatar
zero86ea
C.S. Server Admin
Posts: 318
Joined: Tue Jun 19, 2007 12:02 am
Contact:

Re: Assignment 5.3

#4 Post by zero86ea » Fri Oct 05, 2007 12:07 am

Holy shit Vinyard. Tell me the truth, how long did it take you to do this?!
Im going to ask for you help, so hope you don't mind :)
Image

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Re: Assignment 5.3

#5 Post by vinyard » Fri Oct 05, 2007 12:25 am

Not long. The comments took longer than the code. ;)
- vinyard.

User avatar
vinyard
Server Admin
Posts: 301
Joined: Fri Jun 15, 2007 7:43 pm
Operating System: Windows Vista Ultimate 32bit
CPU: Intel C2D E8200 [Stock]
Video Card: EVGA GeForce 8800 GT 512MB SC
Sound Card: Onboard
Memory: Corsair Twin2x2048-6400Pro
Motherboard: EVGA 680i AR1 p32
Contact:

Re: Assignment 5.3 (easy example)

#6 Post by vinyard » Sun Oct 07, 2007 6:58 pm

Here is an easy version of the program. Tell me what you think.

Code: Select all

/*Assignment 5.3 Simple**
**Code by: David Rivera**
**10.07.2007************/
#include <iostream>

using std::cout;
using std::cin;
using std::endl;
using std::string;

/* Declare Functions */
double compoundInterest(double percentage, double currentAmount);
double simpleInterest(double percentage, int startAmount,  double currentAmount);

const int length = 30;                                                                                                  //Names longer than 29 characters are not supported.

/* Create the template for each person's info */
struct personInfo
{
       string name;                                                                                                     //Person name
       int startAmount, interestType;;                                                                                  //Users investment start amount, Users interest type
       double interestPercent, currentAmount;                                                                           //Users interest percent, users current balance                                                                                                                 
};

personInfo Daphne, Cleo;                                                                                                //Create first user info, Create second user info
                                                                                                                        
int main()
{   
    /* Daphne's information (simple interest) */
    Daphne.name = "Daphne";                                                                                             //Get first user name (In assignment "Daphne")
    Daphne.startAmount = 100;                                                                                           //Get first users investment start amount (in assignment "$100")                                                                                                                
    Daphne.interestPercent = 10;                                                                                        //First users interest percentage rate (in assignment "10%")
    
    /* Cleo's information (compund interest) */  
    Cleo.name = "Cleo";                                                                                                 //Get second users name (in assignment "Cleo")
    Cleo.startAmount = 100;                                                                                             //Get second users investment start amount (in assignment "$100")                                                     
    Cleo.interestPercent = 5;                                                                                           //Second users interest percentage rate (in assignment "5%")
    
    Daphne.interestPercent /= 100;                                                                                      //Convert percentage
    Cleo.interestPercent /= 100;                                                                                        // to decimal
    int years = 0;                                                                                                      //Years it will take
                                                                        
    Daphne.currentAmount = Daphne.startAmount;                                                                          //Assign the start amount
    Cleo.currentAmount = Cleo.startAmount;                                                                              // to current amount
                                                                                                                                                              
    do
    {
         Daphne.currentAmount = simpleInterest(Daphne.interestPercent, Daphne.startAmount, Daphne.currentAmount);       //Simple interest calculation
         Cleo.currentAmount = compoundInterest(Cleo.interestPercent, Cleo.currentAmount);                               //Compound interest calculation
         years++;                                                                                                       //Add a year
    } while(Daphne.currentAmount >= Cleo.currentAmount);                                                                //Do this loop while Daphne's amount is greater or equal to Cleo's
    
    cout << "It will take " << years << " years for " << Cleo.name << "'s investment to exceed "                        //Output how many years it will take for person with                      
         << Daphne.name << "'s investment." << endl;                                                                    // compound interest to pass person with simple interest.
    cout << Daphne.name << "'s final balance: " << Daphne.currentAmount << endl;                                        //Output person one's final balance
    cout << Cleo.name << "'s final balance: " << Cleo.currentAmount << endl;                                            //output person two's final balance
    
    system("pause");                                                                                                    //wait for user to press a key
    return 0;                      
                                  
}

/* function - Calculate Comopund interest */
double compoundInterest(double percentage, double currentAmount)
{
    currentAmount += currentAmount * percentage;                                                                        //multiply current amount by percentage, then add it to current amount
    return currentAmount;                                                                                               // return the value
}

/* function - Calculate simple interest */
double simpleInterest(double percentage, int startAmount, double currentAmount)
{
    currentAmount += startAmount * percentage;                                                                          //Multiply start amount by percentage the add it to current amount
    return currentAmount;                                                                                               // return the value
}
                                                                                     /* PROGRAM END */
Last edited by zero86ea on Sun Oct 07, 2007 7:11 pm, edited 1 time in total.
Reason: removed void functions!
- vinyard.

Post Reply