### Passing Values

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061`` ``````#include //use of cin & cout #include // manipulate decimals/cout #include // math functions using namespace std; double pi = acos(-1.0); void Accircuit (double,double,double,double,double,double,double&,double&, double&,double&,double&) int main() { double R,L,C,E,f_initial,f_final,f,X_L,X_C,Z,I; char Answer; // cout << "Hi this program will"; do { cout << "Please enter R(Resistance in \352): "; cin >> R; cout << "\nEnter a value for L(Inductance in Henries): "; cin >> L; cout << "\nEnter a value for C(Capacitance in Farads): "; cin >> C; cout << "\nEnter a value for E(Volts): "; cin >> E; cout << "\nEnter a value for initial Frequency: "; cin >> f_initial; cout << "\nEnter a value for final Frequency: "; cin >> f_final; system("pause"); system("CLS"); Accircuit (R,L,C,E,f_initial,f_final,f,X_C,X_L,Z,I); cout << "Freq(Hz) XC(\352) XL(\352) Z(\352) I(mA)\n" << "------- ------- ----- ------- -------\n"; cout << f << " "<< setw(14)<< setprecision(6) << X_C << " "<> Answer; system("pause"); system("CLS"); } while (Answer == 'Y' || Answer == 'y'); } void Accircuit(double R,double L,double C,double E,double fi,double ff, double& f,double& X_C, double& X_L,double& Z,double& I) { f = fi; X_C = -1/(2*pi*f*(C/1000000)); X_L = 2*pi*f*(L/1000); Z = sqrt((R*R)+((X_L+X_C)*(X_L+X_C))); I = E/Z*1000; } ``````

I can't seem to figure out what to do. I need to add a loop structure for f (frequency). I need the loop so I can let the program continue calculating for XC,XL, and etc. But my issue is on how to pass the values and such. Thanks in advance.
1) use structures:
 ``1234567891011121314151617`` ``````struct input /*SAMPLE NAME!*/ { double R,L,C,E,f_initial,f_final; } struct output /*SAMPLE NAME!*/ { double f,X_L,X_C,Z,I; } output Accircuit(input in) { output temp; temp.f = in.fi; //... return temp; }``````

2) Give everything a meaningful name. `double voltage `is a way better that `double E`. (rename structs too!)
3) Make yourself more clear. I still didn't get what do you want.

Here is a clarification what I wanted to do. I want to loop f, frequency. The user can input for f_initial and f_final. The loop structure loops and ends after going over f_final. For example, user inputs f_initial as 100. f_final as 1e6 or 1000000 and the loop multiplies f_initial until it reaches 1638400.

I tried this code but it stops right before the 1e6 mark.

`for(f = f_initial; f <= f_final;f*=2)`

I also want to use the values I get from the loop of f to continually calculate XL, XC, and Z, and I.
Last edited on
So you want output to look like:
 ``` Freq(Hz) XC(ê) XL(ê) Z(ê) I(mA) ------- ------- ----- ------- ------- 100 aaa bbb ccc ddd 200 eee fff ggg iii //... 1638400 xxx yyy zzz 000 ```
yes :]

 ``123456789`` `````` cout << "Freq(Hz) XC(\352) XL(\352) Z(\352) I(mA)\n" << "------- ------- ----- ------- -------\n"; cout << f << " "<< setw(14)<< setprecision(6) << X_C << " "<

^ That's the code for it.. It's just that I don't know how to incorporate the values and make it like that.. instead of just 1 line.
 ``123456789101112`` ``````for(f = f_initial; f <= f_final;f*=2){ Accircuit (R,L,C,E,f_initial,f_final,f,X_C,X_L,Z,I); cout << "Freq(Hz) XC(\352) XL(\352) Z(\352) I(mA)\n" << "------- ------- ----- ------- -------\n"; cout << f << " "<< setw(14)<< setprecision(6) << X_C << " "<
Last edited on

 ``123456789101112131415`` ``````for(f = f_initial; f <= f_final;f*=2){ Accircuit (R,L,C,E,f_initial,f_final,f,X_C,X_L,Z,I); cout << f << " "<< setw(14)<< setprecision(6) << X_C << " "<> Answer; system("pause"); system("CLS"); }``````

I rewrote the code like this, but the loop is not ending. I put the "table" before the for loop so it doesn't repeat itself. And it is still not using the value of f to calculate XC,XL,Z, and I.

 ```100 -79577.5 12.6 79721.9 0.188 100 -79577.5 12.6 79721.9 0.188 100 -79577.5 12.6 79721.9 0.188 100 -79577.5 12.6 79721.9 0.188 //.... ```
You probably might want to change Accircuit function.
Dirty fix:
Line 2:`Accircuit(R, L, C, E, f, 0, 0, X_C, X_L, Z, I);`
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465`` ``````#include //use of cin & cout #include // manipulate decimals/cout #include // math functions using namespace std; double pi = acos(-1.0); void Accircuit (double,double,double,double,int,int,int&,double&, double&,double&,double&); int main() { double R,L,C,Volts; int f_initial,f_final,f; double X_L,X_C,Z,I; char Answer; // cout << "Hi this program will"; do { cout << "Please enter R(Resistance in \352): "; cin >> R; cout << "\nEnter a value for L(Inductance in Henries): "; cin >> L; cout << "\nEnter a value for C(Capacitance in Farads): "; cin >> C; cout << "\nEnter a value for E(Volts): "; cin >> Volts; cout << "\nEnter a value for initial Frequency: "; cin >> f_initial; cout << "\nEnter a value for final Frequency: "; cin >> f_final; system("pause"); system("CLS"); cout << "Freq(Hz) XC(\352) XL(\352) Z(\352) I(mA)\n" << "------- ------- ----- ------- -------\n"; for(f = f_initial; f <= f_final;f *= 2) { Accircuit (R,L,C,Volts,f_initial,f_final,f,X_C,X_L,Z,I); cout << f << " "<< setw(14)<< setprecision(6) << X_C << " "<> Answer; system("pause"); system("CLS"); } while (Answer == 'Y' || Answer == 'y'); } void Accircuit(double R,double L,double C,double Volts,int fi,int ff, int& f,double& X_C, double& X_L,double& Z,double& I) { f = f; X_C = -1/(2*pi*f*(C/1000000)); X_L = 2*pi*f*(L/1000); Z = sqrt((R*R)+((X_L+X_C)*(X_L+X_C))); I = Volts/Z*1000; } ``````

Here is my new code for the whole program and I'm almost there!

I just modified f in the reference parameter to f = f. I also changed f_initial, f_final,f to int.

The value for f passed thru and it calculated for XC,XL,Z,I which I wanted, but the last value of the loop is still not 1638400.

 ```Freq(Hz) XC(Ω) XL(Ω) Z(Ω) I(mA) ------- ------- ----- ------- ------- 100 -79577.5 12.6 79721.9 0.188 200 -39788.7 25.1 40076.7 0.374 400 -19894.4 50.3 20464.3 0.733 800 -9947.18 101 11043.4 1.36 1600 -4973.59 201 6912.09 2.17 3200 -2486.8 402 5417.18 2.77 6400 -1243.4 804 5019.25 2.99 12800 -621.699 1.61e+003 5096.45 2.94 25600 -310.849 3.22e+003 5783.22 2.59 51200 -155.425 6.43e+003 8026.22 1.87 102400 -77.7124 1.29e+004 13732.8 1.09 204800 -38.8562 2.57e+004 26179 0.573 409600 -19.4281 5.15e+004 51694.8 0.29 819200 -9.71405 1.03e+005 103055 0.146 Would you like to run the program again? (Enter Y or N):```

enter higher final value. fpr loop will not carry out calculation if f became larger than final value.
What do you mean? I don't understand >.<
...
f becomes 819 200. Checking if it less or equal than 1 000 000, true. Calculating values, outputting it to screen. f=f*2 (1 638 400). Checking if it less or equal than 1 000 000, false. stopping tight here passing control to the statement after loop.
...
That how it works. If you want to calculate values for 1638400, you can input final value of 2 000 000.
Or alternatively you could think of a clever way to force it to calculat that value.
Okay I changed the previous code to

`for(f = f_initial; f <= f_final + f_final;f *= 2)`

 ```Freq(Hz) XC(Ω) XL(Ω) Z(Ω) I(mA) ------- ------- ----- ------- ------- 100 -79577.5 12.6 79721.9 0.188 200 -39788.7 25.1 40076.7 0.374 400 -19894.4 50.3 20464.3 0.733 800 -9947.2 100.5 11043.4 1.358 1600 -4973.6 201.1 6912.1 2.170 3200 -2486.8 402.1 5417.2 2.769 6400 -1243.4 804.2 5019.2 2.988 12800 -621.7 1608.5 5096.4 2.943 25600 -310.8 3217.0 5783.2 2.594 51200 -155.4 6434.0 8026.2 1.869 102400 -77.7 12868.0 13732.8 1.092 204800 -38.9 25735.9 26179.0 0.573 409600 -19.4 51471.9 51694.8 0.290 819200 -9.7 102943.7 103055.4 0.146 1638400 -4.9 205887.4 205943.3 0.073 Would you like to run the program again? (Enter Y or N):```

Before I make the table look pretty and all with usage of loops?
I want to incorporate some error checking and add a loop to let user try inputting the value again.
For example if user enters a negative number for R,L,C, and E the user can reenter another value. F_final cannot be greater than 1e9.
Then I want to add code to figure out the max within the loop for I and its frequency(f).

My question is where should I put the if statement(s) for the error checking and how to incorporate the while loop?

I cant seem to do a loop to error check f_final. Here is the code so far.

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485`` ``````//Prompt user to get value cout << "Enter a value for R(Resistance in \352): "; cin >> R; while (R < 0 ) //Error Checking loop for R { cout << "R must be a positive number!" << endl; cout << "Please enter a positive number: "; cin >> R; system("CLS"); cout << "Enter a value for R(Resistance in \352): " << R << "\n"; } //Prompt user to get value cout << "Enter a value for L(Inductance in Henries): "; cin >> L; while(L < 0 ) //Error Checking loop for L { cout << "L must be a positive number!" << endl; cout << "Please enter a positive number: "; cin >> L; system("CLS"); cout << "Enter a value for R(Resistance in \352): " << R << "\n"; cout << "Enter a value for L(Inductance in Henries): " << L << "\n"; } //Prompt user to get value cout << "Enter a value for C(Capacitance in Farads): "; cin >> C; while (C < 0 ) //Error Checking loop for C { cout << "C must be a positive number!" << endl; cout << "Please enter a positive number: "; cin >> C; system("CLS"); cout << "Enter a value for R(Resistance in \352): " << R << "\n"; cout << "Enter a value for L(Inductance in Henries): " << L << "\n"; cout << "Enter a value for C(Capacitance in Farads): " << C << "\n"; } //Prompt user to get value cout << "Enter a value for E(Volts): "; cin >> Volts; while (Volts < 0 ) //Error Checking loop for E { cout << "E must be a positive number!" << endl; cout << "Please enter a positive number: "; cin >> Volts; system("CLS"); cout << "Enter a value for R(Resistance in \352): " << R << "\n"; cout << "Enter a value for L(Inductance in Henries): " << L << "\n"; cout << "Enter a value for C(Capacitance in Farads): " << C << "\n"; cout << "Enter a value for E(Volts): " << Volts << "\n"; } //Prompt user to get value cout << "Enter a value for Initial Frequency: "; cin >> f_initial; while (f_initial < 1 ) //Error Checking loop for f_initial { cout << "Intial frequency must be >= 1Hz!" << endl; cout << "Please enter another value: "; cin >> f_initial; system("CLS"); cout << "Enter a value for R(Resistance in \352): " << R << "\n"; cout << "Enter a value for L(Inductance in Henries): " << L << "\n"; cout << "Enter a value for C(Capacitance in Farads): " << C << "\n"; cout << "Enter a value for E(Volts): " << Volts << "\n"; cout << "Enter a value for Initial Frequency: " << f_initial << "\n"; } //Prompt user to get value cout << "Enter a value for Final Frequency: "; cin >> f_final; while(f_final >= 1000000000 ) //Error Checking loop for f_final { cout << "Final frequency must be <= 1e9Hz!" << endl; cout << "Please enter another value: "; cin >> f_final; system("CLS"); cout << "Enter a value for R(Resistance in \352): " << R << "\n"; cout << "Enter a value for L(Inductance in Henries): " << L << "\n"; cout << "Enter a value for C(Capacitance in Farads): " << C << "\n"; cout << "Enter a value for E(Volts): " << Volts << "\n"; cout << "Enter a value for Initial Frequency: " << f_initial << "\n"; cout << "Enter a value for Final Frequency: " << f_final << "\n"; } ``````

 ```Enter a value for R(Resistance in Ω): 5 Enter a value for L(Inductance in Henries): 5 Enter a value for C(Capacitance in Farads): 5 Enter a value for E(Volts): 5 Enter a value for Initial Frequency: 5 Enter a value for Final Frequency: 100000000000000000000000 Press any key to continue . . .```

The other error checks work fine and dandy. Except f_final

 ```Freq(Hz) XC(Ω) XL(Ω) Z(Ω) I(mA) ------- ------- ----- ------- ------- Would you like to run the program again? (Enter Y or N): Press any key to continue . . .```

Last edited on
Ok I think I figured it out. Now I want to find the max value of I and its respective frequency from the loop I created. I have no clue on how to get the max from a loop, not a loop where you put a set of individual numbers. For example User enters 10 values and it finds the max value from the 10 entered.

I want to ge the Max value from the loop itself after the calculations and such.. if that makes any sense.

Topic archived. No new replies allowed.