In: Computer Science
1. Introduction
This assignment will give you some experience working with C input (using scanf()) and output (using printf()), as well as arithmetic operations with variables. You will do some very basic circuit analysis, reading the values of a voltage source and three resistors, and calculating the voltage across and current through each of the resistors using three different circuit configurations.
Test cases for the program can be found at this link.
Remember, in addition to submitting your code, you MUST complete the Blackboard assignment "Program 2 style assessment" to get all of the points for this program. You can complete this "assignment" by typing a short message indicating you submitted your code through the textbook IDE.
2. Specification
Note that this specification refers to several figures, which can be found at the following link: Program 2 figures
In this program, you will deal with the three resistive circuits in Figure 1: a series circuit, a parallel circuit, and a circuit using resistors both in series and in parallel.
Those of you who are familiar with basic DC circuit analysis should be able to easily derive the voltage across and current through each resistor. For more details and equations, please see the Circuit Analysis Supplement in the Program 2 figures document.
Input Specification
Your program should prompt the user to enter the values of Vsource (in volts) and R1, R2, and R3 (in ohms). The source voltage should be entered on one line, while all three resistance values should be entered on a second line. (Note: remember, scanf() ignores whitespace when scanning numbers—you do not have to explicitly worry about varying numbers of spaces.)
The program could produce the first two lines below (the numbers are user inputs, not program outputs):
Enter voltage source value (V): 10 Enter three resistance values (ohms): 5 10 10
All input values should be treated as double-precision floating point values.
Output Specification
Once the voltage and resistance values have been read, you should print the following information for each circuit, as shown below:
See the Circuit Analysis Supplement in the Program 2 figures document for more details on determining the appropriate voltage and current through each resistor if you are unfamiliar with the analysis used. The output lines that would follow the example shown above would be:
SERIES CIRCUIT Current through circuit: 0.400000 A Voltage across R1: 2.000000 V Voltage across R2: 4.000000 V Voltage across R3: 4.000000 V PARALLEL CIRCUIT Voltage across each resistor: 10.000000 V Current through R1: 2.000000 A Current through R2: 1.000000 A Current through R3: 1.000000 A R2 & R3 IN PARALLEL Voltage across R1: 5.000000 V Current through R1: 1.000000 A Voltage across R2: 5.000000 V Current through R2: 0.500000 A Voltage across R3: 5.000000 V Current through R3: 0.500000 A
See the posted test cases for more sample program runs.
3. Hints
Order of operations
C operators follow the same order of operations you likely learned for basic arithmetic operations—multiplication and division take precedence over addition and subtraction. Parentheses have higher precedence than any of these operators. So, for example, if x = 5, y = 2, and z = 3:
Variables and expressions
Variables should be used to store values that are used more than once to avoid repeating calculations. For example, you could improve the following code by creating a variable to hold the value a + 2:
x = (a + 2) * 3; y = 5 / (a + 2); z = (a + 2) – (a + 2);
Values used only for output do not need variables, since printf() can print the value of any expression. Each of the following lines is therefore a valid use of this function. Assume you have variables int n and double x:
4. Grading Rubric
For this assignment, points are assigned as follows:
40 points: Your code uses appropriate coding style such as including appropriate comments, indenting the main() function body, and appropriate variable declarations. You must complete the Blackboard assignment "Program 2 style assessment" to earn any of these points.
60 points: Your code compiles and output matches the
desired test outputs shown below. Each test case has a different
number of points assigned to it, as shown in submit mode.
This section will be auto-graded, while I will assign the other
40 points after inspecting your program.
thanks for the question, here is the fully implemented code in C. I ran for the given example in the question and the output is coming exaclty as given.
Let me know for any question, do appreciate with an up vote please :)
------------------------------------------------------------------------------------------------------------------------------------------------------------
#include<stdio.h>
int main(){
double volts;
double r1,r2,r3;
printf("Enter voltage source value (V): "); scanf("%lf",&volts);
printf("Enter three resistance values (ohms): "); scanf("%lf %lf %lf",&r1,&r2,&r3);
double total_resistance = r1+r2+r3;
printf("SERIES CIRCUIT\n");
double current = volts/total_resistance;
printf("Current through circuit: %lf A\n",current);
printf("Voltage across R1: %lf V\n",current*r1);
printf("Voltage across R2: %lf V\n",current*r2);
printf("Voltage across R3: %lf V\n",current*r3);
printf("\n");
printf("PARALLEL CIRCUIT\n");
printf("Voltage across each resistor: %lf V\n",volts);
printf("Current through R1: %lf A\n",volts/r1);
printf("Current through R2: %lf A\n",volts/r2);
printf("Current through R3: %lf A\n",volts/r3);
printf("\n");
printf("R2 & R3 IN PARALLEL\n");
double eq_resistance = r1+ (r2*r3)/(r2+r3);
double eq_current = volts/eq_resistance;
printf("Voltage across R1: %lf V\n",eq_current*r1);
printf("Current through R1: %lf A\n",eq_current);
double r2_current = eq_current*r2/(r2+r3);
double r3_current = eq_current*r3/(r2+r3);
printf("Voltage across R2: %lf V\n",r2_current*r2);
printf("Current through R2: %lf A\n",r2_current);
printf("Voltage across R3: %lf V\n",r3_current*r3);
printf("Current through R3: %lf A\n",r3_current);
}
==========================================================
thanks so much : )