Friday, 12 December 2025

Implementing a Custom Mathematical Function in the C++ Environment

Topic: Mathematical Functions
Subject: Numerical Methods
Tool: C++ and JetBrains CLion 2025.2.3

By: Gani Comia, December 2025

Illustrative Example

The number of bacteria, \(B\), in a culture that’s subject to refrigeration can be approximated by the formula:

$$\large B = 300000 \; e^{-0.032 \; t}$$

Where:
\(B\) – number of bacteria
\(t\) – time the culture has been refrigerated, \(hours\)

Using the given formula, calculates the number of bacteria in the culture, and display the results after 12, 18, 24, . . ., 72 hours.

Solution

C++ Code 

// Calculating the number of bacterial Growth in specified Time
// Ref. C++ for Engineers and Scientist, page 170
// Gani, 2025-12-12
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;

// function prototype
int calcNumBacteria(int time);

int main() {
   
int const minTime = 12;
   
int const maxTime = 72;
   
int const stepTime = 6;
   
cout << "\n";
   
// Table title
   
cout << setw(14) << left << "Time (hours)"
           
<< setw(18) << left << "Number of Bacteria" << endl;
   
// Calculation
   
for (int time = minTime; time <= maxTime; time += stepTime) {
       
int const numBacteria = calcNumBacteria(time);
       
cout << setw(8) << right << time
           
<< setw(18) << right << numBacteria << endl;
    }
   
return 0;
}

// Given function to calculate the number of bacteria
int calcNumBacteria(int const time) {
   
const double A = 300000;                    // constant
   
const double n = 0.032;                     // constant
   
double const result = A * exp(-n * time);
   
// Explicitly round the double to the nearest whole number before
    // returning the int
   
return static_cast<int>(round(result));
}

Console Output

Time (hours)  Number of Bacteria

           12                     204339

           18                     168643

           24                     139182

           30                     114868

           36                       94801

           42                       78240

           48                       64572

           54                       53292

           60                       43982

           66                       36299

           72                       29958

Feel free to comment for inquiry, clarification, correction or suggestion for improvement. Drop your email to make a request to the author.

Disclaimer: The formulas and calculations presented are for technical reference only. Users must verify accuracy and ensure compliance with applicable engineering standards, codes, and safety requirements before practical application.

References

  1. Gary R. Bronson and G.J. Borse. C++ for Engineers & Scientist. 3rd Ed. Course Technology, Cengage Learning, Boston, Massachusetts. 2010.

Implementing a Custom Mathematical Function in the C++ Environment

Topic: Mathematical Functions Subject: Numerical Methods Tool: C++ and JetBrains CLion 2025.2.3 By: Gani Comia , December 2025...