Question
Programming Assignment #2: Basic I/O and Operations *Please give me the correct code, Which can compile and Run. 1. Introduction This assignment will give you
Programming Assignment #2: Basic I/O and Operations
*Please give me the correct code, Which can compile and Run.
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.
2. Deliverables
Submit your source file by uploading it directly to your Dropbox folder. Ensure your source file name isprog2_circuit.c. Submit only the .c file. Failure to meet this specification will reduce your grade, as described in the program grading guidelines.
3. Specifications
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 Section 6: Circuit Analysis Supplement.
Input: 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 numbersyou do not have to explicitly worry about varying numbers of spaces.)
The program could produce the first two lines below (user inputs are underlined):
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: Once the voltage and resistance values have been read, you should print the following information for each circuit, as shown below:
A blank line (to separate each circuit description)
A brief circuit description (e.g., SERIES CIRCUIT)
The voltage across each resistor. Note that, in the parallel circuit, the voltage across each resistor is the same and should only be printed once.
The current through each resistor. Note that, in the series circuit, the current through each resistor is the same and should only be printed once.
See Section 6 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 Section 5: Test Cases for more sample program runs. 2
4. Hints
Order of operations: C operators follow the same order of operations you likely learned for basic arithmetic operationsmultiplication and division take precedence over addition and subtraction. Parentheses have higher precedence than any of these operators. So, for example, ifx = 5,y = 2, andz = 3:
x+y*z =5+2*3 =5+6=11
(x+y)*z=(5+2)*3=7*3=21
x+y/2 =5+2/2 =5+1=6
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:
printf("n squared: %d, n cubed: %d ", n * n, n * n * n); printf("17/x + 35n = %lf ", (17 / x) + (35 * n)); printf("Rectangle with length %d, width %lf has area %lf ",
n, x, n * x);
Visual Studio scanf() error: Visual Studio users may encounter an error when using scanf() that says the function is unsafe. The message then suggests using scanf_s() instead, a Windows-specific secure version of scanf().
Id prefer you not use scanf_s(), as its not a portable function (i.e., code that can be run on any system regardless of operating system), and it doesnt behave exactly the same way as scanf(). Instead, to resolve this error, include the following #define statement as shownplacing the define before the given include statement is strictly necessary:
#define _CRT_SECURE_NO_WARNINGS
#include
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started