Thread: Hard stuck C Primer Plus example. Any help please :)

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    5

    Question Hard stuck C Primer Plus example. Any help please :)

    Hi guys new here ^.^ I'm working out some examples from C Primer Plus, Fifth Edition By Stephen Prata and I stumbled upon the following example.

    I have managed to do simple and compound interest. But can't seem to do the last part. All help is appreciated.

    John invests 200 dollars at 15% simple interest. (That is, every year, the investment earns an interest equal to 15% of the original investment.) Luke invests 200 dollars at 10% interest compounded annually. (That is, interest is 10% of the current balance, including previous addition of interest.) Write a program that finds how many years it takes for luke's invested sum to overtake John's along with a display showing the two values at that time.


    Code:
    #include<math.h>
    int main()
    {
            float p,q,r,SI,CI;
            int n;
           printf("Enter the value of Principal p = ");
            scanf("%f",&p);
            printf("Enter the value of Rate r = ");
            scanf("%f",&r);
            printf("Enter the value of Period in year n = ");
            scanf("%d",&n);
           SI = ((p*r*n)/100);
        printf("Simple Interest SI=%f \n",SI);
        q = 1+(r/100);
        CI=p*pow(q,n)-p;
           printf("Compound Interest CI=%f \n",CI);
        return 0;       
    }


    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    You just need a loop over n.
    Then use your loop variable in place of n in your calculations.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    What is the input for?
    All numbers should be constants according to the exercise.
    Only the number of years should be variable.

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    Code:
    #include
    Code:
    <stdio.h>
    #include<math.h>
    int main()
    {
        const float p = 200;
    const float r =0.15;
    const float g =0.1;
    float q;
    float SI;
    float CI;
    int n;
    
    printf("Enter the value of Period in year n = ");
    scanf("%d",&n);
    
    
    SI = ((p*r*n)/100);
    printf("Simple Interest SI=%f \n",SI);
    
    q = 1+(g/100);
    CI=p*pow(q,n)-p;
    printf("Compound Interest CI=%f \n",CI);
    return 0;
    }
    Here is what I ended up with after taking your advice. But it isn't running :/
    Salem could you please tell me how and were i should make use of the loop function.
    THanks

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,799
    Read about loops in your book and post an attempt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    Hi salem thanks again, yes i know how to use a loop etc I just cant seem to understand how to use loops here in this case.

  7. #7
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    Could it be

    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        const float p = 200;
    const float r =0.15;
    const float g =0.1;
    float q;
    float SI;
    float CI;
    int n;
    int m;
    
    for (int n = 0; n = m ; m++)
        printf("n is %d \n", n)
    
        SI = ((p*r*n)/100);
    printf("Simple Interest SI=%f \n",SI);
    
    q = 1+(g/100);
    CI=p*pow(q,m)-p;
    printf("Compound Interest CI=%f \n",CI);
    return 0;
    }
    Last edited by Alfred_Schembri; 12-21-2017 at 08:10 AM.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Remember that if you do not use braces, the body of the for loop is only the next statement, i.e., your code is equivalent to:
    Code:
    #include<stdio.h>
    #include<math.h>
    int main()
    {
        const float p = 200;
        const float r =0.15;
        const float g =0.1;
        float q;
        float SI;
        float CI;
        int n;
        int m;
    
        for (int n = 0; n = m ; m++)
        {
            printf("n is %d \n", n);
        }
    
        SI = ((p*r*n)/100);
        printf("Simple Interest SI=%f \n",SI);
    
        q = 1+(g/100);
        CI=p*pow(q,m)-p;
        printf("Compound Interest CI=%f \n",CI);
        return 0;
    }
    Notice that I use indentation to denote scope.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2017
    Posts
    5
    So how should it be then please? And regarding the coding is it right?
    Thanks

  10. #10
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    Maybe like this:
    Code:
    double value_john = 200, value_luke = 200;
    int years = 0;
    do
    {
       // calculate interest for luke and add to value_luke
       // add interest for john to value_john
       // note that interest for john is always the same so can be const
       // years++
    }while (value_john >= value_luke);
    After 9 years luke should have more money than john

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,360
    Quote Originally Posted by Alfred_Schembri
    So how should it be then please?
    My statement about braces hints to you that your lack of braces is wrong.

    Quote Originally Posted by Alfred_Schembri
    And regarding the coding is it right?
    Have you compiled and tested your code? Does it compile? Does it give you the correct output for various valid test input? Bonus: does it handle invalid test input correctly? These are things you should try before asking other people if it is right, otherwise you might end up with the unhelpful but technically correct answer of "no". Once you have figured out that something is wrong, then you can investigate further, and upon finding that you're stuck, that's when you should ask for more specific help in finding/fixing what is wrong.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    Quote Originally Posted by OldGuy2 View Post
    Code:
       // calculate interest for luke and add to value_luke
       // add interest for john to value_john
       // note that interest for john is always the same so can be const
       // years++
    }while (value_john >= value_luke);
    I did it exactly like that. The names and amounts have changed in version six of book.
    Code:
    int main(void) {
      const float original = 100.0f;
      float daphne;
      float deirdre = daphne = original;
      int year = 0;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Primer
    By Imanuel in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2011, 02:38 PM
  2. After C Primer Plus... for fun
    By Huncowboy in forum Programming Book and Product Reviews
    Replies: 7
    Last Post: 11-02-2010, 12:12 PM
  3. C++ Primer
    By gin in forum C++ Programming
    Replies: 2
    Last Post: 06-27-2008, 09:23 AM
  4. C++ Primer Plus
    By curt22 in forum C++ Programming
    Replies: 14
    Last Post: 07-09-2007, 07:19 PM
  5. Stuck Between a Rock and a Hard place
    By kwigibo in forum C++ Programming
    Replies: 2
    Last Post: 05-14-2002, 05:57 PM

Tags for this Thread