4.2: The Program
- Page ID
- 25698
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
First, notice the use of #define
for pi and function prototypes. The functions, though small, make the main section much more readable.
#include <stdio.h> #include <math.h> #define M_PI 3.141592653 void give_directions( void ); double find_fc( double res, double cap ); double find_xc( double freq, double cap ); double find_dB( double gain ); int main( void ) { double r, c, xc, gain, dB, steps; double fc, f, fstart, fstop, ffactor; give_directions(); printf("Enter the resistance in ohms:"); scanf("%lf", &r); printf("Enter the capacitance in farads:"); scanf("%lf", &c); fc = find_fc( r, c ); printf("\nThe critical frequency is %lf hertz.\n\n", fc); printf("Enter the start frequency in hertz:"); scanf("%lf", &fstart); printf("Enter the stop frequency in hertz:"); scanf("%lf", &fstop); printf("Enter the number of steps per decade to display:"); scanf("%lf", &steps); printf("Frequency (Hz)\t\t\tGain (dB)\n"); /* \t is a tab */ ffactor = pow( 10.0, 1.0/steps ); f = fstart; while( f <= fstop ) { xc = find_xc( f, c ); gain = xc/sqrt(r*r + xc*xc); /* could use pow() for square here, but mult by self executes faster */ dB = find_dB( gain ); printf("%10.1lf\t\t%10.1lf\n", f, dB ); /* %10.1lf is 10 spaces with 1 digit after decimal */ f *= ffactor; /* shortcut for f=f*ffactor; */ } } void give_directions( void ) { printf("Bode Table Generator\n\n"); printf("This program will display dB gains for a simple RC circuit\n"); } double find_fc( double res, double cap ) { return( 1.0/(2.0*M_PI*res*cap) ); } double find_xc( double freq, double cap ) { return( 1.0/(2.0*M_PI*freq*cap) ); } double find_dB( double gain ) { return( 20.0 * log10( gain ) ); }
Enter this program and test it using R = 1 k\(\Omega\), C = 100 nF, start frequency = 100 Hz, stop frequency = 20 kHz, points per decade=8. Consider what might go wrong with this program and how you might circumvent those problems. For example, consider what might happen if the user entered 0
for the resistor value, or a stop frequency that was less than the start frequency.