# "nested" for loops for brute force calculating

Discussion in 'Programmer's Corner' started by happykirwan, Feb 28, 2012.

1. ### happykirwan Thread Starter New Member

Oct 4, 2011
2
0
Hello.

I am a first year student in Electrical Engineering and I was having a little bit of trouble with a program I have to write. Without going into too much detail, I am making a program that will take a class A amplifier circuit and calculate gain, input impedance, etc with ALL values of resistors, and spit out answers according to a tolerance. I am having issues with the for loops.

What I have tried is this: I made multiple for loops, each which reference an array of resistor values. When all the calculations are done, it changes one of the resistors, and does all the calculations again. At least, this is how it should work.

Here's what I have so far:
Code ( (Unknown Language)):
1.
2. //FOR THE FOR LOOPS
3.     int count1=0;
4.     int count2=0;
5.     int count3=0;
6.     int count4=0;
7.     int count5=0;
8.
9. for (R1 = E24_Resistor[count1]; count1 < 170; count1++)
10.     {
11.         R1 = E24_Resistor[count1];
12.
13.         for (R2 = E24_Resistor[count2]; count2 < 170; count2++)
14.         {
15.             R2 = E24_Resistor[count2];
16.
17.             for (Rc = E24_Resistor[count3]; count3 < 170; count3++)
18.             {
19.                 Rc = E24_Resistor[count3];
20.
21.                 for (Re = E24_Resistor[count4]; count4 < 170; count4++)
22.                 {
23.                     Re = E24_Resistor[count4];
24.
25.                     for (Vs = VOLTAGE[count5]; count5 < 41; count5++)
26.                     {
27.                         Vs = VOLTAGE[count5];
28. //MY CALCULATIONS AND WHAT NOT GO HERE
29.

Then under this mess I have all my calculations. The problem I am having is that the voltage array is working fine (i've been stepping through line by line), but as soon as the value for count5 = 41, the program just stops.. without dropping into the resistor for loops. I'm sure I am missing something simple, but I simply cannot figure out what it is!

Any help would be GREATLY appreciated. And if the whole source file or the arrays or anything is needed, I would be more than happy to post them.

2. ### MrChips Moderator

Oct 2, 2009
12,636
3,455
You have to initialize the counters in the for loop, for example:

Code ( (Unknown Language)):
1. [FONT=monospace]for (count5 = 0; count5 < 41; count5++)[/FONT]
2. {
3. [FONT=monospace]  Vs = VOLTAGE[count5];[/FONT]
4. }

3. ### happykirwan Thread Starter New Member

Oct 4, 2011
2
0
Works like a charm! Thank you very much for the quick responce!