Skip to content
Physics and Astronomy
Home Our Teaching Resources C programming Example: the circuit program
Back to top
On this page
Contents

Example: the circit program

Passive electrical circuits

In the lecture we saw how to find the impedance of an electrical circuit consisting of series and parallel components. But of course we also need to be read in the data and to setup the circuit structures.

Reading in a single component

The following code will read in a single component:

 

Allowing for multiple components

We can allow for multiple coponents ina circuit by adding an extra option to the switch() statement that allocates an array of circuit pointers and recursively calls newcomponent() to fill it.

This example uses the flexible array trick of having an empty array at the end of a structure and allocating anough space for the size of the array we want.

 

The whole program

 
(For an example of a cicuit this can't handle directly consider a pyramid with the corners as nodes and the vertices being the "wires" each with a component, as in this PDF courtesy of Charles Williams.)
Log in
                                                                                                                                                                                                                                                                       

Validate   Link-check © Copyright & disclaimer Privacy & cookies Share
Back to top