|
-
March 1st, 2011, 11:03 PM
#1
Help with Summations in Program
I'm working on an assignment program that will help users find the equivalent resistance of various AC and DC circuits. It involves doing a summation with a for loop. I'm just really stuck on how to implement this loop.
I understand the reason I'm using this is because it's the only loop that will run a certain number if iterations the user specifies, I'm just having trouble translating the math into a code.
So in this case if it's a bunch of resistors in series you just add all the resistances to get the equivalent resistance. What I'm stuck on is how to get the program to continually ask the user the resistance the number of iterations previously established by the user, and have the program continue to sum until the number of iterations is satisfied.
Here's the code I have so far for just this test, not my actual program.
Code:
#include <iostream>
using namespace std;
int main(void)
{
int nRes, r_i, x;
cout << "How many resistors? >";
cin >> nRes;
for (x = 1; x < nRes; x = x + r_i)
cout << "Next resistance? >";
cin >> r_i;
system("PAUSE");
return EXIT_SUCCESS;
}
.
This is really starting to tick me off cause this is the last piece of the code I need, and it should be easy but I'm not gettin it. HINTS PLEASE!?!
-
March 1st, 2011, 11:19 PM
#2
Re: Help with Summations in Program
You need to wrap your cout and cin statements in {}
Your current code is actually...
Code:
for (x = 1; x < nRes; x = x + r_i)
cout << "Next resistance? >";
cin >> r_i;
yet it should be
Code:
for (x = 1; x < nRes; x = x + r_i)
{
cout << "Next resistance? >";
cin >> r_i;
}
Also, your logic is entirely wrong.
You are checking your loop condition based on x and nRes.
nRes is the number of resistors, and x appears to be the total amount of resistance in ohms.
Does that seem correct to be comparing those two?
Last edited by jnmacd; March 1st, 2011 at 11:21 PM.
-
March 2nd, 2011, 12:27 AM
#3
Re: Help with Summations in Program
 Originally Posted by jnmacd
You need to wrap your cout and cin statements in {}
Your current code is actually...
Code:
for (x = 1; x < nRes; x = x + r_i)
cout << "Next resistance? >";
cin >> r_i;
yet it should be
Code:
for (x = 1; x < nRes; x = x + r_i)
{
cout << "Next resistance? >";
cin >> r_i;
}
Also, your logic is entirely wrong.
You are checking your loop condition based on x and nRes.
nRes is the number of resistors, and x appears to be the total amount of resistance in ohms.
Does that seem correct to be comparing those two?
Well the condition is set by the user. That's the whole reason I'm using the for loop, cause it allows the user to specifically set the number of increments the loop goes through. So I thought that as long as x < nRes the summation will continue until the condition is false. Do you have any suggestions on what the conditional statement should entail?
Forgive me if I'm being dumb, I've never done a for loop this complex before.
-
March 2nd, 2011, 04:53 AM
#4
Re: Help with Summations in Program
your condition is nearly correct, but I think you are missing something.
You need:
The number of times to loop - nRes.
a loop counter - so you know when you have looped the correct amount of times
a variable to store the sum of the resistances.
You have seemed to combine the latter two into 'x', which doesnt make sense.
Code:
int sum(0);
for (int count = 0; count < nRes; ++count)
{
cout << "Next resistance? >";
cin >> r_i;
sum += r_i;
}
-
March 2nd, 2011, 08:43 PM
#5
Re: Help with Summations in Program
Beautiful, thanks I think I'm starting to get this stuff. I ran into an issue with adding up resistors in parallel, but I think its because the sum value is continually changing and cannot save the resistance value in memory. I managed to make a work around for that though. Thanks a lot.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|