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;
}
} ...
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 = 0
136
137
138
139
140
141
142
143
128.type = 0
128.n = 0
144
145
146
147
148
149
150
151
128.nodes[0] =
128.nodes[1] =
160
161
162
163
164
165
166
167
160.value = 0
168
169
170
171
172
173
174
175
160.type = 0
160.n = 0
184
185
186
187
188
189
190
191
184.value = 0
192
193
194
195
196
197
198
199
184.type = 0
184.n = 0
200
201
202
203
204
205
206
207
184.nodes[0] =
184.nodes[1] =
216
217
218
219
220
221
222
223
216.value = 0
224
225
226
227
228
229
230
231
216.type = 0
216.n = 0
240
241
242
243
244
245
246
247
240.value = 0
248
249
250
251
252
253
254
255
240.type = 0
240.n = 0
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 = 156225976
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 = 156225976
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 = 156225920
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] =
newcomponent-2()
500
501
502
503
n = 156225920
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(): for()
608
609
610
611
i = 0
impedance()
608
609
610
611
612
613
614
615
freq = 50000
624
625
626
627
628
629
630
631
denom = 4.89510587e-270
632
633
634
635
636
637
638
639
z(r) = 0
640
641
642
643
644
645
646
647
z(i) = 0
664
665
666
667
668
669
670
671
?
?
?
?
c = -4136901752
newcomponent()
612
613
614
615
n = -1838444
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) = 2.78888e-157
728
729
730
731
732
733
734
735
z(i) = 1.76844e+192
736
737
738
739
740
741
742
743
?
?
?
?
c = NULL
NB: the actual memory address of each variable is
the address shown plus 4293127672 (0xFFE3EDF8).
The actual address of allocated memory is
the address shown plus 156225792 (0x94FD100).
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 0 and phase 0