My program isn\'t producing the outcome i want it to produce. Please modify it f
ID: 3548532 • Letter: M
Question
My program isn't producing the outcome i want it to produce. Please modify it for me so i will produce the outcome i1=.4, i2=-.3,i3=.7. Given that r1= 20 r2=10 r3=10, v1=5 v2=10.#include <iostream> using namespace std;
const int N=3; void eliminate (double a[][N+1], int n, int index); void back_substitute(double a[][N+1],int n,double soln[N]);
int main() { double r1,r2,r3,r4,r5,v1,v2,a[N][N+1],soln[N];
cout<<"Enter resistor values in ohms: "<<"(R1,R2,R3,R4,R5) "; cin>>r1>>r2>>r3>>r4>>r5; cout<<"Enter voltage values in volts: "<<"(V1,V2) "; cin>>v1>>v2;
a[0][0] = r1 + r2; a[0][1] = a[1][0]=-r2; a[0][2] = a[2][0]=a[1][3]=0; a[1][1] = r2+r3+r4; a[1][2] = a[2][1]=-r4; a[2][2] = r4+r5; a[0][3] = v1; a[2][3] = -v2;
for (int index=0; index<N-1;index++) { eliminate(a,N,index); } back_substitute(a,N,soln); cout<<" Solution: "; for (int i=0;i<N;++i) { cout<<"Mesh Current"<<i+1<<":"<<soln[i]<<endl; } return 0; }
/*______________________________________________________________________________________________________*/ void eliminate(double a[][N+1],int n, int index) { double scale_factor; for (int row=index+1; row<n;++row) { scale_factor=-a[row][index]/a[index][index]; a[row][index]=0; for (int col=index+1;col<=n;++col) { a[row][col]+= a[index][col]*scale_factor; } } return; } /*________________________________________________________________________________________________________*/ void back_substitute(double a[][N+1],int n,double soln[]) { soln[n-1]=a[n-1][n]/a[n-1][n-1]; for (int row=n-2;row>=0; --row) { for (int col=n-1;col>=row+1;--col) { a[row][n] -=soln[col]*a[row][col]; } soln[row]=a[row][n]/a[row][row]; }
return; } My program isn't producing the outcome i want it to produce. Please modify it for me so i will produce the outcome i1=.4, i2=-.3,i3=.7. Given that r1= 20 r2=10 r3=10, v1=5 v2=10.
#include <iostream> using namespace std;
const int N=3; void eliminate (double a[][N+1], int n, int index); void back_substitute(double a[][N+1],int n,double soln[N]);
int main() { double r1,r2,r3,r4,r5,v1,v2,a[N][N+1],soln[N];
cout<<"Enter resistor values in ohms: "<<"(R1,R2,R3,R4,R5) "; cin>>r1>>r2>>r3>>r4>>r5; cout<<"Enter voltage values in volts: "<<"(V1,V2) "; cin>>v1>>v2;
a[0][0] = r1 + r2; a[0][1] = a[1][0]=-r2; a[0][2] = a[2][0]=a[1][3]=0; a[1][1] = r2+r3+r4; a[1][2] = a[2][1]=-r4; a[2][2] = r4+r5; a[0][3] = v1; a[2][3] = -v2;
for (int index=0; index<N-1;index++) { eliminate(a,N,index); } back_substitute(a,N,soln); cout<<" Solution: "; for (int i=0;i<N;++i) { cout<<"Mesh Current"<<i+1<<":"<<soln[i]<<endl; } return 0; }
/*______________________________________________________________________________________________________*/ void eliminate(double a[][N+1],int n, int index) { double scale_factor; for (int row=index+1; row<n;++row) { scale_factor=-a[row][index]/a[index][index]; a[row][index]=0; for (int col=index+1;col<=n;++col) { a[row][col]+= a[index][col]*scale_factor; } } return; } /*________________________________________________________________________________________________________*/ void back_substitute(double a[][N+1],int n,double soln[]) { soln[n-1]=a[n-1][n]/a[n-1][n-1]; for (int row=n-2;row>=0; --row) { for (int col=n-1;col>=row+1;--col) { a[row][n] -=soln[col]*a[row][col]; } soln[row]=a[row][n]/a[row][row]; }
return; } #include <iostream> using namespace std;
const int N=3; void eliminate (double a[][N+1], int n, int index); void back_substitute(double a[][N+1],int n,double soln[N]);
int main() { double r1,r2,r3,r4,r5,v1,v2,a[N][N+1],soln[N];
cout<<"Enter resistor values in ohms: "<<"(R1,R2,R3,R4,R5) "; cin>>r1>>r2>>r3>>r4>>r5; cout<<"Enter voltage values in volts: "<<"(V1,V2) "; cin>>v1>>v2;
a[0][0] = r1 + r2; a[0][1] = a[1][0]=-r2; a[0][2] = a[2][0]=a[1][3]=0; a[1][1] = r2+r3+r4; a[1][2] = a[2][1]=-r4; a[2][2] = r4+r5; a[0][3] = v1; a[2][3] = -v2;
for (int index=0; index<N-1;index++) { eliminate(a,N,index); } back_substitute(a,N,soln); cout<<" Solution: "; for (int i=0;i<N;++i) { cout<<"Mesh Current"<<i+1<<":"<<soln[i]<<endl; } return 0; }
/*______________________________________________________________________________________________________*/ void eliminate(double a[][N+1],int n, int index) { double scale_factor; for (int row=index+1; row<n;++row) { scale_factor=-a[row][index]/a[index][index]; a[row][index]=0; for (int col=index+1;col<=n;++col) { a[row][col]+= a[index][col]*scale_factor; } } return; } /*________________________________________________________________________________________________________*/ void back_substitute(double a[][N+1],int n,double soln[]) { soln[n-1]=a[n-1][n]/a[n-1][n-1]; for (int row=n-2;row>=0; --row) { for (int col=n-1;col>=row+1;--col) { a[row][n] -=soln[col]*a[row][col]; } soln[row]=a[row][n]/a[row][row]; }
return; }
Explanation / Answer
#include <iostream>
using namespace std;
const int N=3;
void eliminate (double a[][N+1], int n, int index);
void back_substitute(double a[][N+1],int n,double soln[N]);
int main()
{
double r1,r2,r3,r4,r5,v1,v2,a[N][N+1],soln[N];
cout<<"Enter resistor values in ohms: "<<"(R1,R2,R3,R4,R5) ";
cin>>r1>>r2>>r3;
cout<<"Enter voltage values in volts: "<<"(V1,V2) ";
cin>>v1>>v2;
a[0][0] = r2/r3;
a[0][1] = a[0][2] = -r2/r3;
a[1][0]= a[2][2] =a[0][3]= 0;
a[1][1]= -r2;
a[1][2]= a[2][1] =r3;
a[2][0] = r1;
a[1][3] = v2;
a[2][3] = v1;
for (int index=0; index<N-1;index++)
{
eliminate(a,N,index);
}
back_substitute(a,N,soln);
cout<<" Solution: ";
for (int i=0;i<N;++i)
{
cout<<"Mesh Current"<<i+1<<":"<<soln[i]<<endl;
}
return 0;
}
/*______________________________________________________________________________________________________*/
void eliminate(double a[][N+1],int n, int index)
{
double scale_factor;
for (int row=index+1; row<n;++row)
{
scale_factor=-a[row][index]/a[index][index];
a[row][index]=0;
for (int col=index+1;col<=n;++col)
{
a[row][col]+= a[index][col]*scale_factor;
}
}
return;
}
/*________________________________________________________________________________________________________*/
void back_substitute(double a[][N+1],int n,double soln[])
{
soln[n-1]=a[n-1][n]/a[n-1][n-1];
for (int row=n-2;row>=0; --row)
{
for (int col=n-1;col>=row+1;--col)
{
a[row][n] -=soln[col]*a[row][col];
}
soln[row]=a[row][n]/a[row][row];
}
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.