# Loop, Loop, Loop?!

This is a discussion on Loop, Loop, Loop?! within the C Programming forums, part of the General Programming Boards category; I need desperate help with program control. I'm okay if it's a single loop, but more than that, I"m lost! ...

1. ## 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. 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. 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. - 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. 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. 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. 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. 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.

9. 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.

10. 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.

11. 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).