Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← C++ Worked Programs

Learn / Worked program

Linear Thermal Expansion Table

A threshold marker is an explicit policy

BeginnerDesktop C++17 · runnable source

What you will learn

The complete source

main.cpp

main.cpp
#include <cmath>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

// Read one whole line so a failed conversion cannot trap the input stream.
bool readNumber(const std::string& prompt, double& value, double low, double high) {
    std::string line;
    while (std::cout << prompt && std::getline(std::cin, line)) {
        std::istringstream input(line);
        char extra = '\0';
        if (input >> value && !(input >> extra) && std::isfinite(value)
            && value >= low && value <= high) {
            return true;
        }
        std::cout << "Enter a finite number in [" << low << ", " << high << "].\n";
    }
    std::cerr << "Input ended before a valid number was supplied.\n";
    return false;
}

double lengthChange(double alphaPerKelvin, double lengthM, double temperatureChangeC) {
    return alphaPerKelvin * lengthM * temperatureChangeC;
}

int main() {
    double lengthM = 0.0, initialTempC = 0.0, alphaPerKelvin = 0.0, allowanceM = 0.0;
    if (!readNumber("Initial length (m): ", lengthM, 1e-6, 1e6)
        || !readNumber("Initial temperature (C): ", initialTempC, -200.0, 200.0)
        || !readNumber("Linear coefficient (1/K): ", alphaPerKelvin, 0.0, 1e-3)
        || !readNumber("Illustrative movement allowance (m): ", allowanceM, 0.0, 1e3)) return 1;
    std::cout << "Teaching model: marker compares absolute movement with an illustrative allowance.\n"
              << "final_C final_m change_m marker\n" << std::fixed << std::setprecision(6);
    for (int finalTempC = 0; finalTempC <= 40; ++finalTempC) {
        const double changeM = lengthChange(alphaPerKelvin, lengthM, finalTempC - initialTempC);
        const char marker = std::abs(changeM) > allowanceM ? '*' : '-';
        std::cout << finalTempC << ' ' << lengthM + changeM << ' ' << changeM << ' ' << marker << '\n';
    }
}
Source integrity

SHA-256 32a2a0fbe2d386da761598b4a0c2dbb679392fbfc8c7d565b8c871929d890ffd. Display, copy and approved download use the same source.

What it does and why it was selected

The historical bridge table combines an engineering equation with a threshold decision. It is valuable precisely because those are different claims: a formula can correctly predict free expansion while an uncited threshold cannot establish that a bridge is dangerous. The canonical version names the threshold an illustrative movement allowance.

Actual execution

main reads initial length, reference temperature, a linear expansion coefficient and the illustrative allowance. The integer loop selects each final temperature from 0 through 40 C. lengthChange receives the temperature DIFFERENCE from the initial temperature, not the absolute final reading. The returned change is added to the original length. Its magnitude is compared with the allowance to choose a star or dash marker. In the captured historical-parameter example, a 175-m length at 3 C with alpha=11.7e-6 expands by 0.0757575 m at 40 C, displayed as 0.075758 m.

Mathematical or state model

deltaL=alphaL0deltaT is a linear free-expansion approximation with constant alpha. Alpha has inverse-kelvin units. Celsius and Kelvin temperature DIFFERENCES are numerically equal, so finalTempC-initialTempC is valid here. A negative difference produces contraction; the coefficient is not an instruction to add positive movement at every temperature. The final length is L0+deltaL. No stress, constraint, support interaction or thermal gradient is calculated.

Design and C++ mechanics

The function isolates the physical calculation from display policy. main compares abs(changeM), because the illustrative allowance now applies to movement magnitude in either direction. The original lab compared only lengthChange>0.063, marking sufficiently large expansion while ignoring contraction. That behavioral change is intentional and documented. It does not silently convert the old one-sided threshold into an actual bridge-design requirement. Taking the allowance as input makes the user-defined comparison visible rather than burying a supposedly authoritative constant.

Historical differences and limits

The accepted temperature and coefficient ranges are teaching bounds, not validated material properties across all temperatures. Alpha can vary with temperature; a constant coefficient over a broad interval is an approximation. The marker uses a strict greater-than comparison, so exactly equal movement is not marked. Rounded printed movement can appear equal to an allowance even if the underlying comparison differs by a small amount. The source table remains an arithmetic illustration, not a structural safety assessment.

Useful extensions

Add a supplied material record with a sourced coefficient and reference temperature range. If modeling constraints, derive thermal strain alpha*deltaT and then a stress model with explicit elastic and boundary assumptions. Preserve the distinction between free expansion, mechanical constraint and a chosen comparison policy; each requires different inputs and evidence.

Source-linked explanations

Follow each line reference to its marked source region. The explanations below are bound to the reviewed source hash and preserve the supplied teaching layers.

Input contract and end-of-file

main.cpp, lines 8–21 →

Only validated finite numbers are allowed to reach the Linear Thermal Expansion Table calculation.

Technical explanation
getline consumes a complete line, then a temporary istringstream attempts the numeric conversion. After conversion, trying to extract extra detects a trailing non-whitespace character. Failure of that second extraction means no unwanted data remains. The local stream can fail without putting cin into the historical repeated-failbit state. A fresh local stream is created for every attempt. Low/high comparisons establish the caller’s domain, and exhausting cin ends the while condition and returns false.
Syntax
The && operators evaluate left to right and short-circuit: range tests run only after numeric extraction succeeds. !(input >> extra) intentionally treats no remaining non-whitespace character as success. A double& or int& parameter writes the accepted value into the caller’s variable.
Effect on the program
The caller checks the returned bool and returns exit status 1 on EOF before calculation. Rejected lines are followed by another prompt; valid lines reach the model exactly once.
Common mistake
Clearing a permanently exhausted input stream and retrying cannot create new input. Accepting a numeric prefix such as 550x would silently change the input contract.
Deeper connection
Reliable input separates parsing from physical or algorithmic validity: a valid number still must lie in this program’s supported domain.

input validationconsole ioreferences

Use a difference, not an absolute temperature

main.cpp, lines 23–25 →

alphaPerKelvin*lengthM*temperatureChangeC multiplies inverse-temperature, length and temperature units to obtain length.

Technical explanation
alphaPerKelvin*lengthM*temperatureChangeC multiplies inverse-temperature, length and temperature units to obtain length. Temperature differences in C and K have the same numerical size. The function does not infer a reference temperature; the caller supplies a difference explicitly.
Syntax
The function does not infer a reference temperature; the caller supplies a difference explicitly.
Effect on the program
The function does not infer a reference temperature; the caller supplies a difference explicitly.
Common mistake
Substituting finalTempC directly treats zero Celsius as the physical reference and changes the model.

functionsunits

The marker is a comparison policy

main.cpp, lines 35–40 →

The loop covers all 41 integer temperatures from 0 to 40.

Technical explanation
The loop covers all 41 integer temperatures from 0 to 40. Each change uses finalTempC-initialTempC. std::abs turns contraction and expansion into movement magnitude for the selected allowance comparison. The star represents this arithmetic criterion, not an independently validated structural danger assessment.
Syntax
The star represents this arithmetic criterion, not an independently validated structural danger assessment.
Effect on the program
The star represents this arithmetic criterion, not an independently validated structural danger assessment.
Common mistake
The historical >0.063 condition flagged only expansion. Calling either threshold a bridge safety standard goes beyond the supplied evidence.

conditionalsloopsfloating point

Recorded input and output

Captured from the compiled canonical program in the supplied verification package. These are recorded terminal sessions, not an interactive browser runner.

Sample run

Arguments
None
Recorded exit status
0

Program input

175
3
0.0000117
0.063

Program output

Initial length (m): Initial temperature (C): Linear coefficient (1/K): Illustrative movement allowance (m): Teaching model: marker compares absolute movement with an illustrative allowance.
final_C final_m change_m marker
0 174.993857 -0.006142 -
1 174.995905 -0.004095 -
2 174.997952 -0.002047 -
3 175.000000 0.000000 -
4 175.002048 0.002047 -
5 175.004095 0.004095 -
6 175.006143 0.006142 -
7 175.008190 0.008190 -
8 175.010237 0.010238 -
9 175.012285 0.012285 -
10 175.014332 0.014332 -
11 175.016380 0.016380 -
12 175.018428 0.018427 -
13 175.020475 0.020475 -
14 175.022523 0.022522 -
15 175.024570 0.024570 -
16 175.026617 0.026617 -
17 175.028665 0.028665 -
18 175.030712 0.030712 -
19 175.032760 0.032760 -
20 175.034807 0.034807 -
21 175.036855 0.036855 -
22 175.038903 0.038902 -
23 175.040950 0.040950 -
24 175.042998 0.042997 -
25 175.045045 0.045045 -
26 175.047092 0.047092 -
27 175.049140 0.049140 -
28 175.051187 0.051187 -
29 175.053235 0.053235 -
30 175.055283 0.055282 -
31 175.057330 0.057330 -
32 175.059378 0.059377 -
33 175.061425 0.061425 -
34 175.063472 0.063473 *
35 175.065520 0.065520 *
36 175.067567 0.067567 *
37 175.069615 0.069615 *
38 175.071663 0.071662 *
39 175.073710 0.073710 *
40 175.075758 0.075757 *
3 more recorded behavior and error cases
contraction marker
Arguments
None
Recorded exit status
0

Program input

175
40
0.0000117
0.01

Program output

Initial length (m): Initial temperature (C): Linear coefficient (1/K): Illustrative movement allowance (m): Teaching model: marker compares absolute movement with an illustrative allowance.
final_C final_m change_m marker
0 174.918100 -0.081900 *
1 174.920148 -0.079852 *
2 174.922195 -0.077805 *
3 174.924242 -0.075757 *
4 174.926290 -0.073710 *
5 174.928337 -0.071662 *
6 174.930385 -0.069615 *
7 174.932433 -0.067567 *
8 174.934480 -0.065520 *
9 174.936528 -0.063473 *
10 174.938575 -0.061425 *
11 174.940622 -0.059377 *
12 174.942670 -0.057330 *
13 174.944717 -0.055282 *
14 174.946765 -0.053235 *
15 174.948813 -0.051187 *
16 174.950860 -0.049140 *
17 174.952908 -0.047092 *
18 174.954955 -0.045045 *
19 174.957002 -0.042997 *
20 174.959050 -0.040950 *
21 174.961097 -0.038902 *
22 174.963145 -0.036855 *
23 174.965193 -0.034807 *
24 174.967240 -0.032760 *
25 174.969288 -0.030712 *
26 174.971335 -0.028665 *
27 174.973383 -0.026617 *
28 174.975430 -0.024570 *
29 174.977477 -0.022522 *
30 174.979525 -0.020475 *
31 174.981572 -0.018427 *
32 174.983620 -0.016380 *
33 174.985668 -0.014332 *
34 174.987715 -0.012285 *
35 174.989763 -0.010238 *
36 174.991810 -0.008190 -
37 174.993857 -0.006142 -
38 174.995905 -0.004095 -
39 174.997952 -0.002047 -
40 175.000000 0.000000 -
zero alpha
Arguments
None
Recorded exit status
0

Program input

175
3
0
0.063

Program output

Initial length (m): Initial temperature (C): Linear coefficient (1/K): Illustrative movement allowance (m): Teaching model: marker compares absolute movement with an illustrative allowance.
final_C final_m change_m marker
0 175.000000 -0.000000 -
1 175.000000 -0.000000 -
2 175.000000 -0.000000 -
3 175.000000 0.000000 -
4 175.000000 0.000000 -
5 175.000000 0.000000 -
6 175.000000 0.000000 -
7 175.000000 0.000000 -
8 175.000000 0.000000 -
9 175.000000 0.000000 -
10 175.000000 0.000000 -
11 175.000000 0.000000 -
12 175.000000 0.000000 -
13 175.000000 0.000000 -
14 175.000000 0.000000 -
15 175.000000 0.000000 -
16 175.000000 0.000000 -
17 175.000000 0.000000 -
18 175.000000 0.000000 -
19 175.000000 0.000000 -
20 175.000000 0.000000 -
21 175.000000 0.000000 -
22 175.000000 0.000000 -
23 175.000000 0.000000 -
24 175.000000 0.000000 -
25 175.000000 0.000000 -
26 175.000000 0.000000 -
27 175.000000 0.000000 -
28 175.000000 0.000000 -
29 175.000000 0.000000 -
30 175.000000 0.000000 -
31 175.000000 0.000000 -
32 175.000000 0.000000 -
33 175.000000 0.000000 -
34 175.000000 0.000000 -
35 175.000000 0.000000 -
36 175.000000 0.000000 -
37 175.000000 0.000000 -
38 175.000000 0.000000 -
39 175.000000 0.000000 -
40 175.000000 0.000000 -
eof
Arguments
None
Recorded exit status
1

Program input

No standard input.

Program output

Initial length (m): 

Error output

Input ended before a valid number was supplied.

Build and run

From this folder, with a configured C++17 compiler:

sh
g++ -std=c++17 -Wall -Wextra -Wpedantic main.cpp -o program
./program

On Windows GCC, build with -o program.exe and use .\program.exe. These are desktop examples; VS Code is an editor, the compiler builds an executable, and the terminal runs it.

Windows run command from the supplied metadata: .\program.exe.

Desktop compiler setup and platform limits. How headers and translation units fit together.

Verification and provenance

  • Source inspected
  • Compile verified
  • Warning reviewed
  • Behavior verified
  • Sanitizer checked

Supplied evidence: 4 recorded behavior/error cases and 4 address/undefined-behavior sanitizer cases for this exact source. These are tested cases, not an exhaustive correctness claim.

Recorded compiler: g++ (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0.

LeakSanitizer: disabled because unavailable under runtime tracing. Windows/MSVC and macOS execution are not certified by this evidence.

Source origin and publication treatment

Modernized user supplied source

User-supplied origin; no independent authorship or license determination. Instructor code is not included.

New or rewritten source, limited sanitized historical excerpts when useful

No new license is asserted by this publication.