Objective To create a series DC circuit solver that involves voltage, current, r
ID: 3849436 • Letter: O
Question
Objective To create a series DC circuit solver that involves voltage, current, resistance, and power. It will use Ohm's law to determine the voltage, power, and current through individual resistors. Laboratory Checklist: Review Ohm's law. See last page in the document Review C++ Functions Review C++ Vector class Read Problem Specification Please create and implement a simple series circuit solver for one resistor. Use the filename of ohmslaw-a.cpp for GITHUB.UC.EDU submission. Please create and implement a series circuit solver that can take an "unlimited" number of resistors by utilizing the C vector" class. Use the filename of ohmslaw-b.cpp for GITHUB.UC EDU submission. Please add a menu to your series circuit solver to allow for deleting nodes (resistors) in your circuit, display the calculated results, allow for adding nodes (resistors) to your circuit, and changing input voltage. When complete push assignment related files to GITHUB under lab6 folder containing both ohmslaw-a.cpp, ohmslab-b.cpp and ohmslaw-c.cpp. Specification: This assignment is divided into three parts one is a simple implementation of a series DC circuit (a voltage source, resistance, and current plus power calculation). Part two will use the vector class to "unlimit" the number of resistors in the circuit. Part 3 is to create an edit menu so you can make changes to your circuit. If you need a refresher course (or introduction) on ohm's law see the last page for a description. This assignment is divided into three parts so you will have to create three separate projects in the IDE of your choice. Part A:Explanation / Answer
Here is the code for all 3 parts. Output shown at end. Compile and run each program separately
example :
g++ ohmslaw-a.cpp -o partA
./partA
Please don't forget to rate the answer if it helped. Thank you very much.
part A : ohmslaw-a.cpp
#include <iostream>
using namespace std;
double calculate_current(double voltage, double resistance); // function to calculate current
double calculate_power(double voltage, double resistance); //function to calculate power
void display_circuit(double voltage, double resistance); //function to compute and display circuit parameters
int main()
{
double resistance;
double voltage;
//get user input
cout << "Enter Voltage: ";
cin >> voltage;
cout << "Enter Resistance: ";
cin >> resistance;
display_circuit(voltage, resistance);
return 0;
}
// function to calculate current.
// formula for current I, based on voltage V and resistance R is I = V / R and unit is amps
double calculate_current(double voltage, double resistance)
{
return voltage / resistance;
}
// function to calculate power. power is P = V x I
// I is the current in the above formula
double calculate_power(double voltage, double current)
{
return voltage * current;
}
//function to compute and display circuit parameters
void display_circuit(double voltage, double resistance)
{
double current = calculate_current(voltage, resistance);
double power = calculate_power(voltage, current);
cout << " Circuit Parameters: " << endl;
cout << " Resistance: " << resistance << " ohm" << endl;
cout << " Voltage: " << voltage << " volts" << endl;
cout << " Current: " << current << " amps" << endl;
cout << " Power: " << power << " watts" << endl;
}
Part b: ohmslaw-b.cpp
#include <iostream>
#include <vector>
using namespace std;
double calculate_current(double voltage, double resistance); // function to calculate current
double calculate_power(double voltage, double resistance); //function to calculate power
void display_circuit(double voltage, vector<double> resistances); //function to compute and display circuit parameters
double calculate_total_resistance(vector<double> resistances); //function to calculate total of the resitances in the vector
int main()
{
vector<double> resistances;
double voltage;
double res;
//get user input
cout << "Enter Voltage: ";
cin >> voltage;
while(true)
{
cout << "Enter Resistance(0 to exit): ";
cin >> res;
if(res != 0)
resistances.push_back(res);
else
break;
}
display_circuit(voltage, resistances);
}
// function to calculate current.
// formula for current I, based on voltage V and resistance R is I = V / R and unit is amps
double calculate_current(double voltage, double resistance)
{
return voltage / resistance;
}
// function to calculate power. power is P = V x I
// I is the current in the above formula
double calculate_power(double voltage, double current)
{
return voltage * current;
}
//function to calculate total of the resitances in the vector
double calculate_total_resistance(vector<double> resistances)
{
//iterate through the vector and add it to total
double total = 0;
for(int i = 0; i < resistances.size(); i++)
total += resistances[i];
return total;
}
//function to compute and display circuit parameters
void display_circuit(double voltage, vector<double> resistances)
{
double total_resistance = calculate_total_resistance(resistances);
double series_current = calculate_current(voltage, total_resistance);
double total_power = calculate_power(voltage, series_current);
cout << " Circuit Parameters: " << endl;
cout << " Total Resistance: " << total_resistance << " ohm" << endl;
cout << " Input Voltage: " << voltage << " volts" << endl;
cout << " Series Current: " << series_current << " amps" << endl;
cout << " Total Power: " << total_power << " watts" << endl;
cout << endl;
for(int i = 0; i < resistances.size(); i++)
{
//using formula V = I x R and P = V * I
double res = resistances[i]; //current resistance value
double voltage_drop = series_current * res;
double power = calculate_power(voltage_drop, series_current);
cout << " Node " << i << " parameters: " << endl;
cout << " Resistance: " << res << " ohms" << endl;
cout << " Voltage: " << voltage_drop << " volts" << endl;
cout << " Power: " << power << " watts" << endl << endl;
}
}
Part C: ohmslaw-c.cpp
#include <iostream>
#include <vector>
using namespace std;
double calculate_current(double voltage, double resistance); // function to calculate current
double calculate_power(double voltage, double resistance); //function to calculate power
void display_circuit(double voltage, vector<double> resistances); //function to compute and display circuit parameters
double calculate_total_resistance(vector<double> resistances); //function to calculate total of the resitances in the vector
//fucntion to display the vector of resistors
void display_resistors(const vector<double> &resistances);
//function to delete a resistor
bool delete_resistor(vector<double> &resistances, int index);
//function to delete a resistor
bool change_resistor(vector<double> &resistances, int index, int newResistance);
int main()
{
vector<double> resistances;
double voltage = 0;
double res;
int choice = 0;
int index;
while(choice != 7 )
{
cout << "1. Add a resistor" << endl;
cout << "2. Change input voltage " << endl;
cout << "3. Delete resistor" << endl;
cout << "4. Edit resistor" << endl;
cout << "5. Group add a series of resistors" << endl;
cout << "6. Display network" << endl;
cout << "7. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
cout << "Enter resistance: " ;
cin >> res;
resistances.push_back(res);
cout << "Resistor added" << endl;
break;
case 2:
cout << "Enter voltage: " ;
cin >> voltage;
break;
case 3:
cout << "The resistors in the circuit are " << endl;
display_resistors(resistances);
cout << "Enter the index of the resistor to be deleted: " ;
cin >> index;
if(delete_resistor(resistances, index))
cout << "Resistor " << index << " deleted. " << endl;
else
cout << "Index out of range!" << endl;
break;
case 4:
cout << "The resistors in the circuit are " << endl;
display_resistors(resistances);
cout << "Enter the index of the resistor to be changed: " ;
cin >> index;
cout << "Enter the new resistance value: ";
cin >> res;
if(change_resistor(resistances, index, res))
cout << "Resistor " << index << " changed to " << res << endl;
else
cout << "Index out of range!" << endl;
break;
case 5:
cout << "Adding a group of resistors " << endl;
while(true)
{
cout << "Enter resistance (0 to stop): " ;
cin >> res;
if(res == 0)
break;
else
resistances.push_back(res);
}
break;
case 6:
display_circuit(voltage, resistances);
break;
case 7:
break;
default:
cout << "Invalid choice!" << endl;
}
}
}
// function to calculate current.
// formula for current I, based on voltage V and resistance R is I = V / R and unit is amps
double calculate_current(double voltage, double resistance)
{
return voltage / resistance;
}
// function to calculate power. power is P = V x I
// I is the current in the above formula
double calculate_power(double voltage, double current)
{
return voltage * current;
}
//function to calculate total of the resitances in the vector
double calculate_total_resistance(vector<double> resistances)
{
//iterate through the vector and add it to total
double total = 0;
for(int i = 0; i < resistances.size(); i++)
total += resistances[i];
return total;
}
//function to compute and display circuit parameters
void display_circuit(double voltage, vector<double> resistances)
{
double total_resistance = calculate_total_resistance(resistances);
double series_current = calculate_current(voltage, total_resistance);
double total_power = calculate_power(voltage, series_current);
cout << " Circuit Parameters: " << endl;
cout << " Total Resistance: " << total_resistance << " ohm" << endl;
cout << " Input Voltage: " << voltage << " volts" << endl;
cout << " Series Current: " << series_current << " amps" << endl;
cout << " Total Power: " << total_power << " watts" << endl;
cout << endl;
for(int i = 0; i < resistances.size(); i++)
{
//using formula V = I x R and P = V * I
double res = resistances[i]; //current resistance value
double voltage_drop = series_current * res;
double power = calculate_power(voltage_drop, series_current);
cout << " Node " << i << " parameters: " << endl;
cout << " Resistance: " << res << " ohms" << endl;
cout << " Voltage: " << voltage_drop << " volts" << endl;
cout << " Power: " << power << " watts" << endl << endl;
}
}
//function to display the vecotr of resistors
void display_resistors(const vector<double> &resistances)
{
for(int i = 0; i < resistances.size(); i++)
cout << "Resistor " << i << ": " << resistances[i] << endl;
}
//function to delete a resistor
bool delete_resistor(vector<double> &resistances, int index)
{
if(index >=0 && index <resistances.size())
{
resistances.erase(resistances.begin()+index); // erase the element at given index starting from beginning
return true;
}
else
return false;
}
//function to delete a resistor
bool change_resistor(vector<double> &resistances, int index, int newResistance)
{
if(index >=0 && index <resistances.size())
{
//change the value at a given index
*(resistances.begin()+index) = newResistance;
return true;
}
else
return false;
}
output
Output for part A
Enter Voltage: 100
Enter Resistance: 50
Circuit Parameters:
Resistance: 50 ohm
Voltage: 100 volts
Current: 2 amps
Power: 200 watts
=============
Output for Part B
Enter Voltage: 100
Enter Resistance(0 to exit): 50
Enter Resistance(0 to exit): 50
Enter Resistance(0 to exit): 0
Circuit Parameters:
Total Resistance: 100 ohm
Input Voltage: 100 volts
Series Current: 1 amps
Total Power: 100 watts
Node 0 parameters:
Resistance: 50 ohms
Voltage: 50 volts
Power: 50 watts
Node 1 parameters:
Resistance: 50 ohms
Voltage: 50 volts
Power: 50 watts
=============
Output for Part C
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 1
Enter resistance: 50
Resistor added
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 2
Enter voltage: 100
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 6
Circuit Parameters:
Total Resistance: 50 ohm
Input Voltage: 100 volts
Series Current: 2 amps
Total Power: 200 watts
Node 0 parameters:
Resistance: 50 ohms
Voltage: 100 volts
Power: 200 watts
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 1
Enter resistance: 50
Resistor added
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 6
Circuit Parameters:
Total Resistance: 100 ohm
Input Voltage: 100 volts
Series Current: 1 amps
Total Power: 100 watts
Node 0 parameters:
Resistance: 50 ohms
Voltage: 50 volts
Power: 50 watts
Node 1 parameters:
Resistance: 50 ohms
Voltage: 50 volts
Power: 50 watts
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 3
The resistors in the circuit are
Resistor 0: 50
Resistor 1: 50
Enter the index of the resistor to be deleted: 1
Resistor 1 deleted.
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 6
Circuit Parameters:
Total Resistance: 50 ohm
Input Voltage: 100 volts
Series Current: 2 amps
Total Power: 200 watts
Node 0 parameters:
Resistance: 50 ohms
Voltage: 100 volts
Power: 200 watts
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 1
Enter resistance: 75
Resistor added
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 6
Circuit Parameters:
Total Resistance: 125 ohm
Input Voltage: 100 volts
Series Current: 0.8 amps
Total Power: 80 watts
Node 0 parameters:
Resistance: 50 ohms
Voltage: 40 volts
Power: 32 watts
Node 1 parameters:
Resistance: 75 ohms
Voltage: 60 volts
Power: 48 watts
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 4
The resistors in the circuit are
Resistor 0: 50
Resistor 1: 75
Enter the index of the resistor to be changed: 0
Enter the new resistance value: 25
Resistor 0 changed to 25
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 6
Circuit Parameters:
Total Resistance: 100 ohm
Input Voltage: 100 volts
Series Current: 1 amps
Total Power: 100 watts
Node 0 parameters:
Resistance: 25 ohms
Voltage: 25 volts
Power: 25 watts
Node 1 parameters:
Resistance: 75 ohms
Voltage: 75 volts
Power: 75 watts
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 5
Adding a group of resistors
Enter resistance (0 to stop): 10
Enter resistance (0 to stop): 20
Enter resistance (0 to stop): 0
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 6
Circuit Parameters:
Total Resistance: 130 ohm
Input Voltage: 100 volts
Series Current: 0.769231 amps
Total Power: 76.9231 watts
Node 0 parameters:
Resistance: 25 ohms
Voltage: 19.2308 volts
Power: 14.7929 watts
Node 1 parameters:
Resistance: 75 ohms
Voltage: 57.6923 volts
Power: 44.3787 watts
Node 2 parameters:
Resistance: 10 ohms
Voltage: 7.69231 volts
Power: 5.91716 watts
Node 3 parameters:
Resistance: 20 ohms
Voltage: 15.3846 volts
Power: 11.8343 watts
1. Add a resistor
2. Change input voltage
3. Delete resistor
4. Edit resistor
5. Group add a series of resistors
6. Display network
7. Quit
Enter your choice: 7
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.