RCsim.c
· 983 B · C
Raw
#include <stdio.h>
#define Vin 0.5
#define timeFinal 5.0
void run_simulation(double R, double C, double stepsize, int testNum);
int main() {
run_simulation(10000, 0.0002, 0.1, 1);
run_simulation(5000, 0.0001, 0.1, 2);
run_simulation(10000, 0.0002, 0.5, 3);
run_simulation(5000, 0.0001, 0.5, 4);
return 0;
}
/*
Function to simulate an RC circuit with voltage across C as output voltage
Pass in resistance, capacitance, time step size, and test number
*/
void run_simulation(double R, double C, double stepsize, int testNum) {
double Vc = 0.0;
double time = 0.0;
double tau = R * C;
printf("Test number %d: R=%.0f, C=%.0f, Step=.2f, Tau=%.2f \n", testNum, R, C, stepsize, tau);
printf("Time (s) \t Voltage (V)\n");
printf("%.2f \t\t %.5f\n", time, Vc);
// loop until time = timeFinal
while (time < timeFinal) {
double newVc = Vc + (stepsize / tau) * (Vin - Vc);
Vc = newVc;
time += stepsize;
printf("%.2f \t\t %.5f\n", time, Vc);
}
printf("\n");
}
| 1 | #include <stdio.h> |
| 2 | |
| 3 | #define Vin 0.5 |
| 4 | #define timeFinal 5.0 |
| 5 | |
| 6 | |
| 7 | void run_simulation(double R, double C, double stepsize, int testNum); |
| 8 | |
| 9 | int main() { |
| 10 | run_simulation(10000, 0.0002, 0.1, 1); |
| 11 | |
| 12 | run_simulation(5000, 0.0001, 0.1, 2); |
| 13 | |
| 14 | run_simulation(10000, 0.0002, 0.5, 3); |
| 15 | |
| 16 | run_simulation(5000, 0.0001, 0.5, 4); |
| 17 | |
| 18 | return 0; |
| 19 | } |
| 20 | |
| 21 | /* |
| 22 | Function to simulate an RC circuit with voltage across C as output voltage |
| 23 | Pass in resistance, capacitance, time step size, and test number |
| 24 | */ |
| 25 | void run_simulation(double R, double C, double stepsize, int testNum) { |
| 26 | double Vc = 0.0; |
| 27 | double time = 0.0; |
| 28 | |
| 29 | double tau = R * C; |
| 30 | |
| 31 | printf("Test number %d: R=%.0f, C=%.0f, Step=.2f, Tau=%.2f \n", testNum, R, C, stepsize, tau); |
| 32 | |
| 33 | printf("Time (s) \t Voltage (V)\n"); |
| 34 | |
| 35 | printf("%.2f \t\t %.5f\n", time, Vc); |
| 36 | |
| 37 | // loop until time = timeFinal |
| 38 | while (time < timeFinal) { |
| 39 | double newVc = Vc + (stepsize / tau) * (Vin - Vc); |
| 40 | Vc = newVc; |
| 41 | time += stepsize; |
| 42 | |
| 43 | printf("%.2f \t\t %.5f\n", time, Vc); |
| 44 | } |
| 45 | printf("\n"); |
| 46 | } |
bit_merge.cpp
· 432 B · C++
Raw
#include <stdio.h>
int bit_merge(unsigned short lower, unsigned short upper);
int main() {
unsigned short hn1 = 0x1234;
unsigned short hn2 = 0xabcd;
int result;
result = bit_merge(hn1, hn2);
printf("\nmerging 0x%x and 0x%x results in 0x%x\n", hn1, hn2, result);
return 0;
}
int bit_merge(unsigned short lower, unsigned short upper) {
int merged = ((int)upper << 16) | lower;
return merged;
}
| 1 | #include <stdio.h> |
| 2 | |
| 3 | int bit_merge(unsigned short lower, unsigned short upper); |
| 4 | |
| 5 | int main() { |
| 6 | unsigned short hn1 = 0x1234; |
| 7 | unsigned short hn2 = 0xabcd; |
| 8 | int result; |
| 9 | |
| 10 | result = bit_merge(hn1, hn2); |
| 11 | |
| 12 | printf("\nmerging 0x%x and 0x%x results in 0x%x\n", hn1, hn2, result); |
| 13 | |
| 14 | return 0; |
| 15 | } |
| 16 | |
| 17 | |
| 18 | int bit_merge(unsigned short lower, unsigned short upper) { |
| 19 | |
| 20 | int merged = ((int)upper << 16) | lower; |
| 21 | |
| 22 | return merged; |
| 23 | } |