Skip to content
EMPS intranet

Back to top
Hi!   Hi!           Start main()

Try stepping through the code


  Value and type of last evaluated expression: Address of array element: Address of array elementx:  (none)

Your browser does not support the canvas element which will make some the features unavailable.

If you are using Internet Explorer within the University of Exeter try going to the Settings menu (probably the gear shape at the top right of this page), selecting "Compatibility View settings", unchecking "Display intranet sites in Compatibility View" and reloading this page.

Code

 Header...
// // Find the impedance of an electrical circuit // #define INFINITY 1e100 #define PI 3.14159265358979 typedef enum type{Resistor,Capacitor,Inductor,Series,Parallel}Type; typedef struct component { double value; Type type; unsigned int n;// If parallel or serial struct component *nodes[];// If parallel or serial }Component;
Component* newcomponent(void){
  ...
» char *names[]={"Resistor","Capacitor","Inductor","Series","Parallel",NULL}; » Component *c=NULL; » Type type; » int n; » while (1) { » printf("What type of component?\n"); » for ( int i=0;namesi!=NULL; ++i) printf("%d\t%s\n", i, namesi); » scanf("%d", &type); » switch (type) { » case Resistor: » case Capacitor: » case Inductor: » c=xmalloc(sizeof *c); » c->type=type; » while (1) { » printf("Please enter the value of the %s\n", names(c->type)); » scanf("%lg", &c->value); » if (c->value>0) return c; » printf("Please enter a positive value\n"); } » case Series: » case Parallel: » while (1) { » printf("How many nodes are there?\n"); » scanf("%ud", &n); » if (n>0) break; » printf("Please enter a positive value\n"); } » c=xmalloc(sizeof *c+n*sizeof *c->nodes); » c->n=n; » for (n=0; n<c->n; ++n) c->nodesn=newcomponent(); » return c; » default : » printf("Sorry \"%d\" is not a valid option, please try again\n", c->type); } }
} double complex impedance(Component *c,double freq){
  ...
» double complex z=0; » double denom; » switch (c->type) { » case Resistor: » return c->value; » case Inductor: » return 2*PI*I*c->value*freq; » case Capacitor: // Handle DC and v low frequencies » denom=2*PI*c->value*freq; » if (denom<1.0/INFINITY) denom=1.0/INFINITY; » return 1.0/(I*denom); » case Series: » for ( int n=0;n<c->n; ++n) z+=impedance(c->nodes[n], freq); » return z; » case Parallel: » for ( int n=0;n<c->n; ++n) z+=1.0/impedance(c->nodes[n], freq); » return 1.0/z; }
} int main(){
  ...
» Component *c=NULL; » double complex z; » c=newcomponent(); » z=impedance(c, 50000); » printf("The impedance is:\n" "\tmodulus %g and phase %g\n", cabs(z), carg(z)); » return 0;
}

Where two numbers are shown the first is the real part, the second the imaginary.

Memory

Allocated memory

128
129
130
131
132
133
134
135
128.value = -2.03556219e+183
136
137
138
139
140
141
142
143
128.type = 2.40689296e+09
128.n = 636383269
144
145
146
147
148
149
150
151
128.nodes[0] = 2394533737
128.nodes[1] =

160
161
162
163
164
165
166
167
160.value = -3.72691038e-268
168
169
170
171
172
173
174
175
160.type = 784504663
160.n = 9634572

184
185
186
187
188
189
190
191
184.value = 6.57120369e-291
192
193
194
195
196
197
198
199
184.type = 3.63061838e+09
184.n = 992312495
200
201
202
203
204
205
206
207
184.nodes[0] = 2432701406
184.nodes[1] =

216
217
218
219
220
221
222
223
216.value = -9.19916014e+254
224
225
226
227
228
229
230
231
216.type = 2.47793556e+09
216.n = 6137851

240
241
242
243
244
245
246
247
240.value = -6.90059275e+261
248
249
250
251
252
253
254
255
240.type = 3.32632211e+09
240.n = 9085245

newcomponent-3(): for()

384
385
386
387
i = 0

newcomponent-3(): for()

384
385
386
387
i = 0

newcomponent-3()

388
389
390
391
n = 161309472
392
393
394
395
396
397
398
399
type = 0
c = NULL
400
401
402
403
404
405
406
407
names[0] =
names[1] =
408
409
410
411
412
413
414
415
names[2] =
names[3] =
416
417
418
419
420
421
422
423
names[4] =
names[5] =

newcomponent-3()

388
389
390
391
n = 161309472
392
393
394
395
396
397
398
399
type = 1
c = NULL
400
401
402
403
404
405
406
407
names[0] =
names[1] =
408
409
410
411
412
413
414
415
names[2] =
names[3] =
416
417
418
419
420
421
422
423
names[4] =
names[5] =

newcomponent-2(): for()

496
497
498
499
i = 0

newcomponent-2(): for()

496
497
498
499
i = 0

newcomponent-2()

500
501
502
503
n = 161309416
504
505
506
507
508
509
510
511
type = 0
c = NULL
512
513
514
515
516
517
518
519
names[0] =
names[1] =
520
521
522
523
524
525
526
527
names[2] =
names[3] =
528
529
530
531
532
533
534
535
names[4] =
names[5] =

newcomponent-2()

500
501
502
503
n = 161309416
504
505
506
507
508
509
510
511
type = 1
c = NULL
512
513
514
515
516
517
518
519
names[0] =
names[1] =
520
521
522
523
524
525
526
527
names[2] =
names[3] =
528
529
530
531
532
533
534
535
names[4] =
names[5] =

impedance()

592
593
594
595
596
597
598
599
freq = 50000
608
609
610
611
612
613
614
615
denom = -nan
616
617
618
619
620
621
622
623
z(r) = 0
624
625
626
627
628
629
630
631
z(i) = 0
664
665
666
667
668
669
670
671
?
?
?
?
c = -4133108640

newcomponent(): for()

608
609
610
611
i = 0

newcomponent()

612
613
614
615
n = -548060
616
617
618
619
620
621
622
623
type = 1
c = NULL
624
625
626
627
628
629
630
631
names[0] =
names[1] =
632
633
634
635
636
637
638
639
names[2] =
names[3] =
640
641
642
643
644
645
646
647
names[4] =
names[5] =

main()

720
721
722
723
724
725
726
727
z(r) = -7.34082e+266
728
729
730
731
732
733
734
735
z(i) = 4.98426e-270
736
737
738
739
740
741
742
743
?
?
?
?
c = NULL

NB: the actual memory address of each variable is the
address shown plus 4294418056 (0xFFF79E88).

The actual address of allocated memory is the
address shown plus 161309288 (0x99D6268).

Show Advanced options

.

Input was:

4 2 
0 2000 
3 2
0 1000 
2 0.01

Show output (Before looking at the output, work out what you think it put should be and see if you are right.)

Output

What type of component?
0	Resistor
1	Capacitor
2	Inductor
3	Series
4	Parallel
How many nodes are there?
What type of component?
0	Resistor
1	Capacitor
2	Inductor
3	Series
4	Parallel
Please enter the value of the Resistor
What type of component?
0	Resistor
1	Capacitor
2	Inductor
3	Series
4	Parallel
How many nodes are there?
What type of component?
0	Resistor
1	Capacitor
2	Inductor
3	Series
4	Parallel
Please enter the value of the Resistor
What type of component?
0	Resistor
1	Capacitor
2	Inductor
3	Series
4	Parallel
Please enter the value of the Inductor
The impedance is:
	modulus 5.68015e+266 and phase 3.14159
                                                                                                                                                                                                                                                                       

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