-
Notifications
You must be signed in to change notification settings - Fork 0
/
EmbeddedSystemDesign_HW_week5.1.c
74 lines (67 loc) · 2.08 KB
/
EmbeddedSystemDesign_HW_week5.1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Problem: Make a function that calculates series capacitor circuit or parallel capacitor circuit. For the function capacitor values
(unit F) C1,C2,C2,Cn and circuit type "serial" or "parallel" and capacitor count are passed as a parameters. Unit for result is µF.
For example:
Test | Result
------------------------------------------------------------------------
float C1=10E-6; | 2.000
float C2=10E-6; |
float C3=10E-6; |
float C4=10E-6; |
float C5=10E-6; |
|
float capacitor_values[]={C1,C2,C3,C4,C5}; |
int len = sizeof(capacitor_values)/sizeof(capacitor_values[0]); |
int circuit_type=2; //1=parallel circuit, 2=serial circuit |
printf("%.3f",circuit(capacitor_values,circuit_type,len)); |
*/
#include <stdio.h>
#include <math.h>
#include <string.h>
//function prototype(s)
float circuit(float capacitor_values[], int circuit_type, int len);
/*---------------------------------------------------------------------------------------------------------------------------------*/
//main function
int main(void)
{
//declair vars
float C1=10E-6;
float C2=10E-6;
float C3=10E-6;
float C4=10E-6;
float C5=10E-6;
//functions
float capacitor_values[]={C1,C2,C3,C4,C5};
int len = sizeof(capacitor_values)/sizeof(capacitor_values[0]);
int circuit_type=2; //1=parallel circuit, 2=serial circuit
printf("%.3f\n", circuit(capacitor_values, circuit_type, len));
return 0;
}//end main
/*---------------------------------------------------------------------------------------------------------------------------------*/
//fuction(s) declaration
float circuit(float capacitor_values[], int circuit_type, int len)
{
//declair vars
float result = 0;
float Csum = 0;
//functions
if(circuit_type == 1)
{
for(int i=0; i<len; i++)
{
Csum = Csum + capacitor_values[i];
}
result = Csum*pow(10, 6);
}
else if(circuit_type == 2)
{
for(int i=0; i<len; i++)
{
Csum = Csum + (1/capacitor_values[i]);
}
result = (1/Csum)*pow(10, 6);
}
else
printf("Wrong circuit type!\nPlease enter again... [1 = parallel | 2 = serial]");
return result;
}//end circuit