Skip to content

Instantly share code, notes, and snippets.

@SakshamInABox
Last active May 8, 2019 09:59
Show Gist options
  • Save SakshamInABox/0a2142b3605fa3b50a1533848fffc240 to your computer and use it in GitHub Desktop.
Save SakshamInABox/0a2142b3605fa3b50a1533848fffc240 to your computer and use it in GitHub Desktop.
/**************************
program title: Assaignment 3
Author: Kenneth Gardiner, 18013601
description. program runs functions to find the best resistor for a circuit
*****************************/
#include <iostream>
using namespace std;
double input();
double Calculate(double reqGain, double stdR[], double& R1, double& R2, double& R3, double& R4);
void Output(double reqGain, double ActGain, double& R1, double& R2, double& R3, double& R4);
int main()
{
cout << "Voltage Divider" << endl;
double R1, R2, R3, R4;
double ActGain;
double stdR[] = { 1.0, 1.2, 1.5, 1.8, 2.2, 2.7, 3.3, 3.9, 4.7, 5.6,
6.8, 8.2, 10, 12, 15, 18, 22, 27, 33, 39, 47, 56, 68, 82, 100, 1000000000 };
double reqGain = input();
if (reqGain != 0)
{
ActGain = Calculate(reqGain, stdR, R1, R2, R3, R4);
Output(reqGain, ActGain, R1, R2, R3, R4);
}
cout << "End Of Program" << endl;
system("pause");
}
double input()
{
double gain;
bool error;
do
{
cout << "Enter gain between 0 and 1 " << endl;
cin >> gain;
if (cin.fail())
{
cin.clear();
cin.ignore(1000, '\n');
error = true;
cout << endl << "Input Error, enter a number" << endl;
}
else if (gain >= 0 && gain <= 1)
{
error = false;
}
} while (error == true);
return gain;
}
double Calculate(double reqGain, double stdR[], double& R1, double& R2, double& R3, double& R4)
{
int NUM_RES = 26;
double error;
double Total;
double calcGain;
double P1;
double P2;
double bestR1 = stdR[0];
double bestR2 = stdR[0];
double bestR3 = stdR[0];
double bestR4 = stdR[0];
P1 = (R1 * R2) / (R1 + R2);
P2 = (R3 * R4) / (R3 + R4);
double BestGain = P2 / (P1 + P2);
double bestError = fabs(reqGain - BestGain);
double BestTotal = P2 + P1;
for (int i1 = 0; i1 < NUM_RES - 1; i1++)
{
R1 = stdR[i1];
for (int i2 = i1; i2 < NUM_RES; i2++)
{
R2 = stdR[i2];
for (int i3 = 0; i3 < NUM_RES - 1; i3++)
{
R3 = stdR[i3];
for (int i4 = i3; i4 < NUM_RES; i4++)
{
R4 = stdR[i4];
P1 = (R1 * R2) / (R1 + R2);
P2 = (R3 * R4) / (R3 + R4);
calcGain = P2 / (P1 + P2);
Total = P1 + P2;
error = fabs(reqGain - calcGain);
if (error < bestError)
{
bestR1 = stdR[i1];
bestR2 = stdR[i2];
bestR3 = stdR[i3];
bestR4 = stdR[i4];
bestError = error;
BestTotal = Total;
BestGain = calcGain;
}
else if (error == bestError && Total > BestTotal)
{
bestR1 = stdR[i1];
bestR2 = stdR[i2];
bestR3 = stdR[i3];
bestR4 = stdR[i4];
bestError = error;
BestTotal = Total;
BestGain = calcGain;
}
}
}
}
}
R1 = bestR1;
R2 = bestR2;
R3 = bestR3;
R4 = bestR4;
return BestGain;
}
void Output(double reqGain, double ActGain, double& R1, double& R2, double& R3, double& R4)
{
cout << "Required Gain = " << reqGain << endl;
cout << "Actual Gain = " << ActGain << endl;
cout << "Resistor 1 = " << R1 << " Kohms" << endl;
cout << "Resistor 2 = " << R2 << " Kohms" << endl;
cout << "Resistor 3 = " << R3 << " Kohms" << endl;
cout << "Resistor 4 = " << R4 << " Kohms" << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment