Thread: Loop, Loop, Loop?!

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    15

    Angry Loop, Loop, Loop?!

    I need desperate help with program control. I'm okay if it's a single loop, but more than that, I"m lost!

    Here's yet another problem I'm trying to work out. This program should compound the interest, but it has to do it for each of the interest rates...

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
     int year;
     double amount, principal = 1000.0, rate1 = .05, rate2 = .06, 
    
    rate3 = .07, rate4 = .08, rate5 = .09, rate6 = .10;
    
     printf( "%ds%21s\n", "Year", "Amount on deposit" );
    
     for ( year = 1; year <= 10; year++ ) {
      amount = principal * pow( 1.0 + rate1, year );
      printf( "%4d%21.2f\n", year, amount );
     }
    
    for ( year = 1; year <= 10; year++ ) {
      amount = principal * pow( 1.0 + rate2, year );
      printf( "%4d%21.2f\n", year, amount );
    }
    for ( year = 1; year <= 10; year++ ) {
      amount = principal * pow( 1.0 + rate3, year );
      printf( "%4d%21.2f\n", year, amount );
    }
    for ( year = 1; year <= 10; year++ ) {
      amount = principal * pow( 1.0 + rate4, year );
      printf( "%4d%21.2f\n", year, amount );
    }
    for ( year = 1; year <= 10; year++ ) {
      amount = principal * pow( 1.0 + rate5, year );
      printf( "%4d%21.2f\n", year, amount );
    
    return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Place all rates in an array and loop through the array.
    Code:
    double rates[6] = {.05, .06, .07, .08, .09, .10 };
    
    for( ... loop through rate array ...)
    {
       for(year = 1; year <= 10; year++)
       {
           /* calculate and print interest */
       }
    }

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    Thanks for your help, but I cannot use arrays.

    I've tried it again, and now I get the message: "ID: unresolved: pow"

    Code:
    #include <stdio.h>
    #include <math.h>
    
    int main()
    {
    
            int year;
            double amount, principal = 1000.0, rate1 = .05, rate2 = .06,   
    rate3 = .07, rate4 = .08;
    
            printf( "%ds%21s\n", "Year", "Amount on deposit" );
            printf( "Enter the EOF character to end input.\n" );
    
            for ( year = 1; year <= 10; year++ ) {
                    amount = principal * pow( 1.0 + rate1, rate2, rate3, rate4,  year );
                    printf( "%4d%21.2f\n", year, amount );
            }
    
            return 0;
    }

  4. #4
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    - pow( 1.0 + rate1, rate2, rate3, rate4, year );

    The pow function only takes 2 parameters and not 5.


    Why can't you use arrays?

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    15
    I can't use arrays because the problem is not that far ahead in the textbook. I'm just supposed to use a "for" statement to vary the interest rate. I'm also wondering if everyone has struggled with this as I am. I'm taking an eccelerated course in this so that is part of my problem, but this is not for the faint of heart. Geeze! *whiping the sweat off of my brow* I'm so thankful for everyone's help. I wish I had found you all weeks ago. This is the only messageboard I've posted on where I've gotten some real constructive help. I'm not trying to dump my homework here, but the help is definitely nice! I've been going around and around on 3 of these programs for weeks. I am also not impressed with the textbook that I'm using which is "How to Program C" by Deitel & Deitel. Their explanations are lacking considerably. Okay...I"m rambling...

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Well using two loops, it would be

    Code:
    double rate;
    for ( rate = 0.05 ; rate <= 0.10 ; rate += 0.01 ) {
            for ( year = 1; year <= 10; year++ ) {
                    amount = principal * pow( 1.0 + rate1, rate2, rate3, rate4,  year );
                    printf( "%4d%21.2f\n", year, amount );
            }
    }

  7. #7
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by Salem
    Well using two loops, it would be

    Code:
    double rate;
    for ( rate = 0.05 ; rate <= 0.10 ; rate += 0.01 ) {
            for ( year = 1; year <= 10; year++ ) {
                    amount = principal * pow( 1.0 + rate1, rate2, rate3, rate4,  year );
                    printf( "%4d%21.2f\n", year, amount );
            }
    }
    Salem, if you copy some code make sure you copy the correct code (see my prev reply).

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Salem, if you copy some code make sure you copy the correct code (see my prev reply).
    Boy someone is dreaming. Salem needs to copy your code? That's got to be the funniest thing I've heard all year. Please, tell me some more jokes.

    Oh no, you're serious? Yeah, "your code" was incomplete, so exactly how is this copying? And let's get real here for a minute. We are talking about a basic nested loop. Just how many ways do you think it can be done?

    Here we go, everyone listen up!

    int x = 5;

    x++;

    See that in bold? That is my code so I don't want any of you copying it!

    HAHAH God you slay me. You really do.

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by quzah
    Boy someone is dreaming.
    Yeh and that's you quzan.....

    In my previous reply I told aprilbiz that the pow function takes 2 and not 5 parameters.
    Salem copied the wrong code from aprilbiz.
    Please read the complete thread before you attack someone...

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeh and that's you quzan.....
    Every day a new spelling...

    Actually, Salem is correct here. The original poster posted a pow function which takes five arguments.

    This is possibly valid. Unless you include "math.h", then you have no 'pow' function. As such, it is possible to write a 'pow' function which takes fifteen arguments if I felt like it.

    My point was this: Salem doesn't "copy" code. The latest code example that was given provided five arguments. Thus, it is my opinion that that is what he based his code off of. IE: Here is what you provided using two loops. It should be easily understood from this code how to get what you need.

    Yes, the math.h header's pow function requires two functions. However, it's possible that I could write my own function that would take five.

    I suspect Salem takes a similar approach to the way I provide 'answers'. I provide enough information to get you started, and actually completed, if you take the time to read the code. I don't babysit.

    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Originally posted by quzah


    Every day a new spelling...

    Actually, Salem is correct here. The original poster posted a pow function which takes five arguments.

    Yes, the math.h header's pow function requires two functions.

    Quzah.
    • Sorry for the typo quzah
    • The original post was the pow function from the math.h with 2 arguments.
      In the second post aprilbiz used the pow function with 5 arguments but got an error message:

      I've tried it again, and now I get the message: "ID: unresolved: pow"

      My point is:
      It was better if Salem used the first post of aprilbiz because the code of the second post was incorrect.
    • And the math.h header's function requires two ...
    • I totally agree with your approach: don't give the complete answer, give pseudo code or parts of the code.
      But the code you give must be correct (and is not in this case).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM