In: Computer Science
in C++ Write a program to compute the current and the power dissipation in an AC circuit that has four resistors R1, R2, R3, and R4 in parallel. The voltage source is V. Test your solution with various voltage levels and resistor values. Execute and submit the program and the results in screen captures. Please note that the equivalent resistor is given by 1/Rtotal = 1/R1 + 1/R2 + 1/R3 + 1/R4 and the current I is given by I = V/Rtotal. The Power dissipation is given by P = V x I
B) Repeat the problem above when the resistors are connected in series.
Here i am writing the code in C++.
here for the Rtotal our logic will be like
first we take LCM of all denominator then compute the addtion .
so if we want calculation same like this 1/Rtotal=1/R1+1/R2+1/R3+1/R4 then we have to do fraction addition in program so that we will get numerator and denomintor both the value then we compute our power and current
for fraction addition
step 1
find a common denominator by finding the LCM of the denominators
step 2:
chaange the fractions to have the same denominator and add both of terms
step 3:
reduce the final fraction obtained into its simpler form by dividing both numerator and denominator by there largest common factor;
so these logic i will use for Rtotal calcuation in parallel circuit.
i am pasting my code here
#include<iostream>
#include<math.h>
using namespace std;
int gcd(int a,int b) // finding GCD of number
{
if(a==0)
return b;
return gcd(b%a,a);
}
void smallest(int &den3, int &n3) findinf lcm of two number
{
int common_factor=gcd(n3,den3);
den3=den3/common_factor;
n3=n3/common_factor;
}
void add_frac(int n1,int den1,int n2, int den2, int &n3, int &den3) //adding two fraction
{
den3=gcd(den1,den2);
den3=(den1*den2)/den3;
n3=(n1)*(den3/den1) +(n2)*(den3/den2);
smallest(den3,n3);
}
int main()
{
int choice;
int r1,r2,r3,r4,n1=1,n2=1,r5,n3,r6,n4,n5,r7,total; // these all variable will use in program
float req,v,p,i,temp,tem;
while(1)
{
cout<<"Enter your choice:-"<<endl;
cout<<"1 for Parallel"<<endl;
cout<<"2 for Series"<<endl;
cout<<"3 for Quit"<<endl;
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter R1 value ";
cin>>r1;
cout<<"Enter R2 value ";
cin>>r2;
cout<<"Enter R3 value ";
cin>>r3;
cout<<"Enter R4 value ";
cin>>r4;
cout<<"Enter Voltage Value ";
cin>>v;
add_frac(n1,r1,n2,r2,n3,r5); // first we add R1 and R2
add_frac(n1,r3,n2,r4,n4,r6); // then we add R3 and R4
add_frac(n3,r5,n4,r6,n5,r7); // then we will add summation of R1 or R2 and summation of R3 or R4
temp=r7; // we will save denominator of Rtoal in temp
tem=n5; // and numerator in tem then all are simple calculation
req=temp/tem;
i=v/req;
p=v*i;
cout<<"The current is "<<i<<endl;
cout<<"The power is "<<p<<endl;
break;
case 2:
cout<<"Enter R1 value ";
cin>>r1;
cout<<"Enter R2 value ";
cin>>r2;
cout<<"Enter R3 value ";
cin>>r3;
cout<<"Enter R4 value ";
cin>>r4;
cout<<"Enter Voltage Value ";
cin>>v;
total = r1+r2+r3+r4; // in series
req=total;
i=v/req;
p=v*i;
cout<<"The current is "<<i<<endl;
cout<<"The power is "<<p<<endl;
break;
case 3:
exit(0);
}
}
return 0;
}