-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
292 lines (248 loc) · 7.94 KB
/
main.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// Created by Andrei Rusu on 06/05/2019.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//Function Prototypes
float getCapVal(void);
float getIndVal(void);
int printArrInt(int *arr, int size);
float* getFreqArr(void);
float* getCapArr(float *userCap);
float* getIndArr(float *userInd);
float* getMatrix(float *freqArr, float *capArr, float *indArr, float *attArr, float *attdBArr);
float printArr(float *arr, int size);
float* getAttenuationArr(float *userInd, float *userCap);
float* getAttenuationDBArr(float *attenuationArr);
int main(void) {
//Declaration of variables
float capVal, indVal;
float *capArray;
float *indArray;
float *attenuationArray;
float *attenuationDBArray;
float *freqArray;
//Calling functions in order to pass down data to create the matrix
capVal = getCapVal();
indVal = getIndVal();
freqArray = getFreqArr();
capArray = getCapArr(&capVal);
indArray = getIndArr(&indVal);
attenuationArray = getAttenuationArr(&indVal, &capVal);
attenuationDBArray = getAttenuationDBArr(attenuationArray);
getMatrix(freqArray, capArray, indArray, attenuationArray, attenuationDBArray);
return 0;
}
//collect inductance value from user input
float getIndVal()
{
float indVal, *ptrI;
printf("Enter your inductance value in mH: \n");
scanf("%f", &indVal);
ptrI = &indVal;
*ptrI= *ptrI/ 1000;
return *ptrI;
}
//Collect capacitance value from user input
float getCapVal()
{
float capVal, *ptrC;
printf("Enter your capacitance value in mF: \n");
scanf("%f", &capVal);
ptrC = &capVal;
*ptrC = *ptrC/1000000;
return *ptrC;
}
//Create frequency array.
float* getFreqArr(void)
{
static float freqArr[18];
int i, j;
float *ptrArr;
ptrArr = &freqArr[0];
for (i=0; i <= 9; i++)
{
freqArr[i] = ((i+1)*100);
// printf("\nValue is: %d\n", *(ptrArr+ i));
}
for (j=10; j <= 18; j++)
{
freqArr[j] = ((j-8)*1000);
// printf("\nValue is: %d\n", *(ptrArr+ j));
}
return freqArr;
}
//Create capacitance array.
float* getCapArr(float *userCap)
{
static float capArr[17];
float *ptrArr;
ptrArr = capArr;
int i,j;
for (i = 0; i <= 9; i++)
{
capArr[i] = (1 / (2 * M_PI * (*userCap) * ((i+1) * 100)));
//printf("\nValue is: %.3f\n", *(ptrArr+ i));
}
for (j = 10; j<=17; j++)
{
capArr[j] =(1 / (2 * M_PI * (*userCap) * ((j-8)* 1000)));
//printf("\nValue is: %.3f\n", *(ptrArr+ j));
}
return &capArr[0];
}
float* getIndArr(float *userInd)
{
static float indArr[17];
float *ptrArr;
ptrArr = indArr;
int i, j;
for (i = 0; i <= 9; i++)
{
indArr[i] = (2 * M_PI * (*userInd) * ((i+1) * 100));
//printf("\nValue is: %.3f\n", *(ptrArr+ i));
}
for (j = 10; j<=17; j++)
{
indArr[j] =(2 * M_PI * (*userInd) * ((j-8)* 1000));
//printf("\nValue is: %.3f\n", *(ptrArr+ j));
}
//return memeory address of 1st element in array.
return &indArr[0];
}
float* getAttenuationArr(float *userInd, float *userCap )
{
static float attenuationArr[17];
float *ptrArr, omega;
ptrArr = attenuationArr;
int i, j;
//Iterates over values in order to populate attenuationArr with user input values.
for (i = 0; i <= 9; i++) {
omega = (2 * M_PI * ((i+1) * 100) );
attenuationArr[i] = fabsf(1/(1 - (omega*omega) * (*userInd) * (*userCap)));
//printf("\nValue is: %.3f\n", *(ptrArr+ i));
}
for (j = 10; j <= 17; j++) {
omega = (2 * M_PI * ((j-8) * 1000) );
attenuationArr[j] = fabsf(1/(1 - (omega*omega) * (*userInd) * (*userCap)));
//printf("\nValue is: %.3f\n", *(ptrArr+ j));
}
//return memeory address of 1st element in array.
return &attenuationArr[0];
}
float* getAttenuationDBArr(float *attenuationArr )
{
static float attenuationDBArr[17], a;
float *ptrArr;
int i;
ptrArr = attenuationDBArr;
//Iterates over values in order to populate getAttenuationDBArr array with values from attenuationArr.
for (i=0; i<=17; i++)
{
a = *(attenuationArr+i);
//TO TEST VALUE OF a UNCOMMENT:
//printf("Value of a is: %f", *(attenuationArr+ i));
attenuationDBArr[i] = fabs(-20 * log10(a));
//TO TEST VALUES OF attenuationDBArr:
//printf("\nValue is: %.3f\n", *(ptrArr+ i));
}
//return memeory address of 1st element in array.
return &attenuationDBArr[0];
}
//ASSIGNS AND PRINTS MATRIX
float* getMatrix(float *freqArr, float *capArr, float *indArr, float *attArr, float *attdBArr)
{
static float matrix[5][18] = {{0}};
float *ptrFreqArr, *ptrCapArr, *ptrIndArr, *ptrAttArr, *ptrArrDBArr;
float (*p)[18] = matrix;
int i;
int row, col;
ptrFreqArr = freqArr;
ptrCapArr = capArr;
ptrIndArr = indArr;
ptrAttArr = attArr;
ptrArrDBArr = attdBArr;
//ASSIGNING VALUES TO 2-D ARRAY:matrix
for (i= 0; i<18; i++)
{
*(*(p)+ i ) = *(ptrFreqArr + i);
}
for (i = 0; i<18; i++)
{
*(*(p+1) +i) = *(ptrCapArr + i);
// To test:
// printf("printing induc: %.3f |", *(*(p+1)+ i));
}
for (i = 0; i<18; i++)
{
*(*(p+2)+i) = *(ptrIndArr + i);
// To test:
// printf("printing capa: %.3f |", *(*(p+2) + i) );
// printf("\n");
}
for (i = 0; i<18; i++)
{
*(*(p+3)+i) = *(ptrAttArr + i);
// To test:
// printf("printing ATTENUATION: %.3f |", *(*(p+2) + i) );
// printf("\n");
}
for (i = 0; i<18; i++)
{
*(*(p+4)+i) = *(ptrArrDBArr + i);
// To test:
// printf("printing ATTENUATION2: %.3f |", *(*(p+2) + i) );
// printf("\n");
}
//PRINTING MATRIX
printf("\t\t\t\t\t\t\t ****University of Brighton****\n");
printf("\t\t\t\t\t\t\t Division of Mathematics and Computing\n");
printf("\n");
printf("Student Name: Andrei Rusu\n");
printf("\n");
printf("\tFrequency (Hz)\t\tCapacitor Reactance(Xc)\t\tInductive Reactance(XL)\t\tAttenuation(Vout/Vin)\t\tAttenuation(dB)\n");
printf("------------------\t\t-----------------------\t\t----------------------\t\t--------------------\t\t------------------\n");
for (row = 0; row < 18; row++) {
for (col = 0; col < 5; col++)
{
printf("\t\t %.3f \t\t\t", matrix[col][row]);
}
printf("\n\n");
}
//Control FLow to Save the File
FILE *ptrFile = NULL;
char filename[30];
char userAns;
printf("Would you like to save the file?\n Type y for yes:\n");
scanf("%c", &userAns);
if(userAns == 'y')
{
if ( ptrFile != NULL )
{
printf("Please specify the name of the file: ");
fscanf(ptrFile,"%c", filename);
ptrFile = fopen(filename, "w");
fprintf(ptrFile,"\t\t\t\t\t\t\t ****University of Brighton****\n");
fprintf(ptrFile,"\t\t\t\t\t\t\t Division of Mathematics and Computing\n");
fprintf(ptrFile,"\n");
fprintf(ptrFile,"Student Name: Andrei Rusu\n");
fprintf(ptrFile,"\n");
fprintf(ptrFile,"\tFrequency (Hz)\t\tCapacitor Reactance(Xc)\t\tInductive Reactance(XL)\t\tAttenuation(Vout/Vin)\tAttenuation(dB)\n");
fprintf(ptrFile,"------------------\t\t------------------\t\t------------------\t\t------------------\t\t------------------\n");
for (row = 0; row < 18; row++)
{
for (col = 0; col < 5; col++)
{
fprintf(ptrFile,"\t\t %.3f \t\t", matrix[col][row]);
}
fprintf(ptrFile,"\n");
}
fputs(filename, ptrFile);
fclose(ptrFile);
printf("File write was successful\n");
}else
{
printf("File already saved!");
}
}
return 0;
}