Assignment title: Information
CSE1PES: PROGRAMMING FOR ENGINEERS AND SCIENTISTS
ASSIGNMENT 2 (10%)
TOOLS
Unix server through Putty for compiling and testing
Notepad ++ for writing solutions
LMS for submission
Microsoft word (or similar) for writing written answers
SUBMISSION
The task is split into two submissions:
The code
o Submitted as a .c file in the normal assignment submission portal. (Do not submit the
executable.)
The written portion
o Submitted as a PDF to the turnitin assignment portal.
Each has a separate submission portal, ensure to submit both.
There is NO late submissions for this assignment. No assignment will be accepted after the due date, without a
legitimate extension granted by the lecturer before the due date.
ACADEMIC INTEGRITY
Plagiarism is the submission of somebody else's work in a manner that gives the impression that the work is your
own. For individual assignments, plagiarism includes the case where two or more students work collaboratively on
the assignment. The School of Engineering and Mathematics treats plagiarism very seriously. When it is detected,
penalties are strictly imposed.
http://www.latrobe.edu.au/students/academic-integrityIMPEDANCE, VOLTAGE AND CURRENT CALCULATOR FOR SIMPLE AC CIRCUIT
BACKGROUND
An Alternating Current (AC) circuit is one in which the output of the voltage source changes with time. When we
add components to the circuit, the voltage and current will be different at different points in the circuit. The
components we will be looking at are resistors, capacitors and inductors. Further information can be found here:
https://en.wikipedia.org/wiki/Alternating_current, https://en.wikipedia.org/wiki/Inductor,
https://en.wikipedia.org/wiki/Resistor, https://en.wikipedia.org/wiki/Capacitor.
Impedance is the measure of the resistance and reactance of a component or circuit. Resistance is a measure of
friction against the motion of electrons, whereas reactance is the inertia against the motion of electrons.
Impedance introduces a phase shift between the voltage and current through a component, and hence is
expressed as a complex number. Do not worry too much if this doesn't make sense, you can just think of this as
complex resistance. For further information, please read https://en.wikipedia.org/wiki/Electrical_impedance.
To calculate the impedance, voltage and current for each component in the circuit, we can use ohms law. The
equation for ohms law is as follows: (Note we are dealing with complex numbers, however we will develop all
the necessary functions you will need in lab 5)
� = � ∗ � , � =
� �
, � =
� �
Where V, I and Z represent voltage, current and impedance respectively, and are measured in volts, amps and
ohms respectively.
For series circuits the current is the same through all components.
For parallel circuits the voltage is the same through all components.
The impedance calculation depends on the component. Details are as follows:
Resistors: The impedance for a resistor can be found as:
� = � where R is the resistance measured in ohms.
Capacitors: The impedance for a capacitor can be found as:
�
� =
1
2∗�∗�∗�
where C is the capacitance measured in Farads and f is the frequency
measured in herz
� = 0 − � ∗ �� where �� is the reactance and j is the imaginary unit.
Inductors: The impedance for an inductor can be found as:
�� = 2 ∗ � ∗ � ∗ � where L is the inductance measured in Henrys
� = 0 + � ∗ �� where �� is the reactance and j is the imaginary unit.
Components will either be arranged in parallel or series. Depending on this, the total impedance calculation will
vary.The total impedance between two components in series is:
�� = �1 + �2
Hence, if we have more components in series, the total impedance is:
��2 = ��2 + �3
The total impedance between two components in parallel is:
�� = (�1 ∗ �2)/( �1 + �2)
Which we can then extend to calculate any number of components by:
��2 = (�� ∗ �3)/( �� + �3)
PROBLEM
You are to create a program that allows a user to view the voltage, current and impedance for each component
and the total in either the parallel circuit (a) or series circuit (b). The program will ask the user for the circuit type,
the source voltage, the frequency, the number of components, and the values of each component. The two circuits
are shown below:
a. Parallel circuit b. Series circuitWorked Example:
Given a parallel circuit (a), with a source voltage of 12Vpp, a frequency of 60Hz, with 3 components in parallel.
The components are a 40 ohm resistor, a 0.0003 farad capacitor and a 0.2 henry inductor.
Firstly we calculate the impedance of each component:
��(��������� �� ��������) = 40 �ℎ��
��(��������� �� ���������) = 0 −
1
2 ∗ � ∗ 60 ∗ 0.0003 ∗ � = 0 − 8.842� �ℎ��
��(��������� �� ��������) = 0 + 2 ∗ � ∗ 60 ∗ 0.2 ∗ � = 0 + 75.4� �ℎ��
We now calculate the total impedance. As we are using a parallel circuit we use the formula as above.
We calculate the total impedance between the resistor and capacitor first, then the total of this with the inductor
to get the total resistance. This is because we can only calculate between two components at a time (in this
problem).
��� = (�� ∗ ��)/( �� + ��)
=
(40) ∗ (−8.842�)
(40) + (−8.842�)
= 1.8635 − 8.43j
(complex numbers will be presented in lab5)
�� = (��� ∗ ��)/( ��� + ��)
=
(1.8635 − 8.43j) ∗ (75.4�)
(1.8635 − 8.43j) + (75.4�)
= 2.36 − 9.425� �ℎ��
We then calculate the total current using Ohms law
� =
� �
=
12
2.36 − 9.425� = 0.3 + 1.198� ����
We then know that voltage is the same throughout the circuit because all the components are in parallel.
So we only need to calculate the current for each component:
��
=
12
40 = 0.3 ����
�� =
12
−8.842� = 1.357� ����
��
=
12
75.4� = −0.159� ����
Hence we can now generate the table:
COMPONENTS R C L TOTAL
Z ( Ohms) 40+0j 0-8.842j 0+75.4j 2.36 − 9.425�
I (Amps) 0.3+0j 0+1.357j 0-0.159j 0.3+1.198j
V (Volts) 12+0j 12+0j 12+0j 12+0jFUNCTION PROTOTYPES AND INCLUDES
1. You must include the stdio.h and stdlib libraries
2. The following function prototypes are to be used:
a. The first two are described below
b. The last four will be developed in lab5, you need to alter the types from floats to doubles
c. You can change the naming
d. You cannot change the types
FUNCTION - MAIN
1. The program prints to the screen the student number, student name and the assignment number. This is
enclosed by double asterisks. Ensure you follow the below format.
2. The program asks the user which circuit they want to evaluate, the frequency in hertz, the source voltage
and the number of components. And stores the results appropriately. All of these values are positive
integers. When asking for the number of components, the type of circuit should be specified as shown
below.
a. You can assume the user enters valid values for frequency, voltage and number of components.
b. You must handle the case where the user enters an incorrect value for the type of circuit and
give a message to the user. (No example given)
3. The program creates an array of pointers to doubles, the array is of length 6
a. Then dynamically allocate memory for each of the pointers as an array of size number of
components + 1 (hint. malloc)
b. This is the result table
c. The program stores the source voltage in the array as shown below (hint. complex part is 0)
Impedance component 1 –
Real part
Impedance component 2 –
Real part
… Impedance component n – Real part Total Impedance - Real part
Impedance component 1 –
Complex part
Impedance component 2 –
Complex part
… Impedance component n – Complex
part
Total Impedance - Complex
part
Current component 1 – Real
part
Current component 2 – Real
part
… Current component n – Real part Total Current – Real part
Current component 1 –
Complex part
Current component 2 –
Complex part
… Current component n – Complex part Total Current – Complex part
Voltage component 1 – Real
part
Voltage component 2 – Real
part
… Voltage component n – Real part Total Votage/Source Voltage
– Real part
Voltage component 1 -
Complex part
Voltage component 2 –
Complex part
… Voltage component n – Complex part Total Votage/Source Voltage
– Complex part4. The program creates a pointer to ints and dynamically allocates memory for an array the size number of
components. This will store the component types.
5. The program creates a pointer to doubles and dynamically allocates memory for an array the size number
of components. This will store the size of the components.
6. For each component, the program asks the user to enter the type of component and the value of that
component. When asking for the size, the correct units must be shown (Ohms, Farads and Henrys).
a. The storing of the component types and values must be done using pointer arithmetic in this
section.
b. You can assume the user enters valid values components (1,2 or 3) and their values (positive
values)
7. Calculate the impedance values using the calculate_impedance function. (Described in section below)
8. Calculate the total current values for both the real and complex parts and store appropriately
9. Calculate the voltages and currents of all components, and store appropriately. (Hint. Refer to background
information – Ohms Law. Parallel and series will have different equations)
10. Call the print_results function to print out the results table (Described in section below)
11. End the program appropriately.
FUNCTION - CALCULATE_IMPEDANCE
1. Calculate the impedance for both real and complex part for each component. (Hint. Refer to background
information – Impedance. Resistors, Capacitors and Inductors have different equations)
2. Calculate the total impedance for both real and complex parts.
a. Hints for series:
i. Create temporary variables to store the current sum and initialize to 0.
ii. Iteratively add to these for each impedance value.
b. Hints for parallel
i. For parallel create a set of temporary variables for the result of the multiplication and
division.
ii. Create temporary variables to store the current impedance value also, and initialize to
the first components impedance values.
iii. Iterate through the component values starting from the second component and for each
multiply the current impedance value by the component and store the result
appropriately, then add the current impedance value by the component and store the
result appropriately. Then use the results of these to perform the division and store the
result of the division in the current impedance value.
3. Store the impedance values in the appropriate places in the result table array.
FUNCTION - PRINT_RESULTS
1. Print the result table as shown below:a. The first column describes the rows with the text
"COMPONENTS","Z ( Ohms)","I ( Amps)","V (Volts)".
b. The first row prints the components as 'R' for resistor, 'C' for capacitor and 'I' for inductor.
i. (Hint.) There are 14 spaces before the letter and 13 spaces after.
c. The second row prints the impedance, the third the current and the fourth the voltage
i. The real part is printed in brackets in exponential format with sign and 3 decimal places.
Then the '+' character. Then the complex part is printed in brackets in exponential
format with sign and 3 decimal places. Then the 'j' character.
CONSTRAINTS
Pi needs to be to 11 decimal places in your program.
Formatting needs to be exactly as specified. (Your program will be tested against automated test cases)
Text to user needs to be easily understandable. You can change the text but the same inputs must be
used.
The program must print your student number, name and the assignment number as specified.
Types should be used appropriately.
You must use comments to explain significant lines of code.
BONUS +5% CAPPED AT 100%
Print the sign outside of the brackets as shown below. You will also need to change the number of spaces
in the first row. (4%)
Validate the users input in the main function in step 6. (1%)
HINTS
The code should be around 150 - 200 lines of code without the bonus. If your code is significantly larger
you may want to reconsider your approach.
SUBMISSION
Part 1. Solve the problem by implementing a program using C code.
Part 2. Produce a report of 500 +/- 150 words explaining your code and why you chose your particular
implementation. Key words that should be included are: 'array', 'pointer', 'pointer arithmetic',
'function', 'malloc'. The report needs to be submitted as a pdf. Must include your name and
student number.EXAMPLES (WITHOUT BONUS)
RUBRIC
Part 1 – 74 points
Is the student's number,
student name and
assignment number
printed?
ACADEMIC INTEGRITY
If the names are different
0
Incorrectly formatted
1
Correctly formattedDoes the program
compile?
0
Does not compile.
4
Compiles with warnings.
8
Compiles appropriately.
Is the logic of the code
appropriate?
0
The code logic does not
match the problem.
2
Some small errors in
interpreting the problem.
4
The logic is suitable for
the problem.
Are the inputs and
outputs understandable?
0
Does not accept inputs
correctly or does not
output data
appropriately.
2
Some errors in outputs.
4
Inputs and outputs are
handled appropriately.
Suitable comments are
used to explain particular
blocks or lines of code.
0
No comments
4
Reasonable comments
used throughout.
8
Excellent descriptions and
commenting.
Is the formatting of the
table correct?
0
The tables do not use any
formatting.
4
The tables are formatted
but incorrectly.
6
Correct format specifiers,
spacing and new lines are
used
Is the impedance
calculated correctly
0
The impedance is not
calculated correctly
4
The impedance for each
component is calculated
correctly, but not the
total for both series and
parallel.
9
Total impedance and
impedance for each
component is calculated
correctly for both series
and parrallel.
Is the current calculated
correctly
0
The current is not
calculated correctly.
4
Total current or current
of each component is
calculated correctly for
both series and parallel.
6
Total current and current
of each component is
calculated correctly for
both series and parrallel.
Is the voltage calculated
correctly
0
The current is not
calculated correctly.
4
Voltage of each
component is calculated
correctly for both series
and parallel.
6
Total current and current
of each component is
calculated correctly for
both series and parrallel.
Is malloc used
appropriately
0
Malloc is not used
2
Malloc is used incorrectly
4
Malloc is used
appropriately
Is pointer arithmetic
used appropriately
0
Pointer arithmetic is not
used
2
Pointer arithmetic is used
incorrectly
4
Pointer arithmetic is used
appropriately
Are pointers used
appropriately
0
Pointers are not used
2
Pointers are used
incorrectly
4
Pointers are used
appropriately
Are functions used
appropriately
0
Functions are not used
2
Functions are used
incorrectly
4
Functions are used
appropriately
Is the code correctly
indented?
0
The code is not indented.
1
Some errors in indenting.
2
Code is indented
appropriately.
Are variables named
appropriately?
0
Variables are named
poorly.
1
Variables are named
suitably for the code.
2
Variables are named
suitably for the code and
the domain.Are appropriate variable
types used where
appropriate?
0
All variables are declared
as the largest types
1
Some types are not
suitable.
2
All variable types are
suitable.
Part 2 – 26 points
Is the student's number
and student name on the
report?
ACADEMIC INTEGRITY
If the names are different
0
Included
Is the code explained? 0
The explanation does not
match the code.
8
Explains the code well.
12
Excellent explanation of
code.
Is their justification for
the method used?
0
No justification for the
method used.
6
Justification is incorrect
or lacking.
10
Justification is
appropriate.
Does the explanation use
appropriate grammar
and spelling?
0
Poor grammar or many
spelling mistakes
2
Some errors in grammar
or spelling.
4
Reasonable grammar and
spelling.