Skip to main content
Engineering LibreTexts

2.2: The Program

  • Page ID
    25677
  • \( \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}}\)

    Here is the (first try) specification for the program:

    The program will prompt the user for a DC voltage source value, a nominal resistor value and a resistor tolerance. It will then print out the values for current and power dissipation based on the nominal, minimum and maximum acceptable values of the resistor.

    Not bad, but we need to refine it. First, command line programs usually need some form of start-up message or print out of directions. Remember these are not GUI-driven programs with Help menus. Second, always prompt for input values indicating expected units. If the program expects ohms but the user types in kilo ohms, there’s going to be trouble. Unless there is a compelling reason not to, always use base units (ohms versus kilo ohms for example).

    Here’s our refined specification:

    The program will first give appropriate directions/explanations of use to the user. The program will prompt the user for a DC voltage source value in volts, a nominal resistor value in ohms and a resistor tolerance in percent. It will then print out the values for current in amps and power dissipation in watts based on the nominal, minimum and maximum acceptable values of the resistor.

    Note that we have specified tolerance as a percentage rather than as a factor. This is because the typical user would be prepared to enter 10 for 10%, not 0.1. You can use this specification to create a pseudo code or flow chart. Here is a possible pseudo code:

    1. Print out directions for user.

    2. Prompt user for voltage (in volts) and obtain value.

    3. Prompt user for resistance (in ohms) and obtain value.

    4. Prompt user for tolerance (in percent) and obtain value.

    5. Determine maximum and minimum resistance values.

    6. Calculate currents based on the three resistances.

    7. Calculate powers based on the three resistances.

    8. Print a heading for the values.

    9. Print out the values.

    You could of course choose an alternate algorithm or method of solution. For example, you might prefer to print the heading before the calculations and then print values following each calculation. You might prefer to change the format so that you get rows for each resistor rather than for the current and power. You might even choose an entirely different approach using loops and/or arrays. There will be upsides and downsides to each approach. Often, the question is not “Can I solve this problem?” but rather “What is the most effective way of solving this problem?” Extend a little forethought before you begin coding.

    Based on the above pseudo code, the following program should fit the bill. We will refine it later. Note the use of double as we will most likely have fractional values with which to deal.

    #include <stdio.h>
    
    int main( void )
    {
          double v, tol;
          double rnom, rlow, rhigh;
          double inom, ilow, ihigh;
          double pnom, plow, phigh;
    
          printf(“This program determines current and power.\n”);
    
          printf(“Please enter the voltage source in volts.\n”);
          scanf(“%lf”, &v);
          printf(“Please enter the nominal resistance in ohms.\n”);
          scanf(“%lf”, &rnom);   
          printf(“Please enter the resistor tolerance in percent.\n”);
          scanf(“%lf”, &tol);    
    
          tol = tol/100.0;  /* turn tolerance into a factor */
          rlow = rnom – rnom*tol;
          rhigh = rnom + rnom*tol;
          inom = v/rnom;
          ihigh = v/rlow;
          ilow = v/rhigh;
    
          pnom = v * inom;
          plow = v * ilow;
          phigh = v * ihigh;
    
          printf(“Resistance (ohms)  Current (amps)  Power (watts)\n”);
          printf(“%lf     %lf      %lf\n”, rnom, inom, pnom );
          printf(“%lf     %lf      %lf\n”, rhigh, ilow, plow );
          printf(“%lf     %lf      %lf\n”, rlow, ihigh, phigh );
    }
    

    A word of caution here: Note that the variable ihigh is the highest current, not the current associated with the highest resistor. This can make the print out code seem incorrect. This is a good place for some comments! Also, the initial “directions” are skimpy at best. In any case, enter and build the code above and verify that it works.

    You may have noticed that there is a bit of repetition in this code in the form of calculations and printouts. It may be more convenient if we created functions to handle these. For example, we could create a function to calculate the current:

    double find_current( double voltage, double resistance )
    {
          double current;
    
          current = voltage/resistance;
          return( current );
    }
    

    You could also do this in one step:

    double find_current( double voltage, double resistance )
    {
          return( voltage/resistance );
    }
    

    Updating the program produces the following:

    #include <stdio.h>
    
    double find_current( double voltage, double resistance )
    {
          return( voltage/resistance );
    }
    
    int main( void )
    {
          double v, tol;
          double rnom, rlow, rhigh;
          double inom, ilow, ihigh;
          double pnom, plow, phigh;
    
          printf(“This program determines current and power.\n”);
    
          printf(“Please enter the voltage source in volts.\n”);
          scanf(“%lf”, &v);
          printf(“Please enter the nominal resistance in ohms.\n”);
          scanf(“%lf”, &rnom);   
          printf(“Please enter the resistor tolerance in percent.\n”);
          scanf(“%lf”, &tol);    
    
          tol = tol/100.0;  /* turn tolerance into a factor */
          rlow = rnom – rnom*tol;
          rhigh = rnom + rnom*tol;
    
          inom = find_current( v, rnom );
          ihigh = find_current( v, rlow );
          ilow = find_current( v, rhigh );
    
          pnom = v * inom;
          plow = v * ilow;
          phigh = v * ihigh;
    
          printf(“Resistance (ohms)  Current (amps)  Power (watts)\n”);
          printf(“%lf     %lf      %lf\n”, rnom, inom, pnom );
          printf(“%lf     %lf      %lf\n”, rhigh, ilow, plow );
          printf(“%lf     %lf      %lf\n”, rlow, ihigh, phigh );
    }
    

    This doesn’t seem to be much of an improvement. In fact, it just seems longer! This is true, but extend the idea a moment. What if the calculation for current involved a dozen lines of code instead of just one? This new format would save considerable code space. Note that this is not just a matter of saving some typing, but rather in saving memory used by the executable. This is particularly important when using constrained embedded systems with only a small amount of available memory.

    Note that the new function was added before main(). This is not required. We could also have added it after main(), but in that case we’d have to add a function prototype so that the compiler would know what to expect when it saw the function call in main(). It would look something like this:

    #include <stdio.h>
    
    /* this is the prototype so the compiler can do type checking */
    
    double find_current( double voltage, double resistance );
    
    int main( void )
    {
          ....
    }
    
    double find_current( double voltage, double resistance )
    {
          return( voltage/resistance );
    }
    

    Alter the program to use this new current calculation function and test it. Once this is complete, alter the program one more time to use a function to calculate the power and another to print out the three values. Use the current calculation function as a guide. Test this with the following values: 12 volt source with a 100 ohm, 5% resistor. Finally, consider what might go wrong with the program. What would happen if we the user entered 0 for the resistor value? How could you get around that problem?


    This page titled 2.2: The Program is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by James M. Fiore via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.