---
title: "Simpson 3/8 pseudo-code"
subject: "Numerical Method"
chapter: 5
key: "semester_4/numerical_method:simpson-38-pseudocode"
source: https://www.bctengineer.com/q/semester_4/numerical_method/simpson-38-pseudocode
site: BCT Engineering
---

# Simpson 3/8 pseudo-code

Numerical Method · Chapter 5: Numerical Differentiation and Integration

- Asked in: 2068 Bhadra, 6 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/numerical_method/simpson-38-pseudocode
- Whole chapter: https://www.bctengineer.com/md/semester_4/numerical_method/chapter-5

## Question

Write a pseudo-code to integrate a given function within given limits using Simpson's 3/8 rule.

## Short answer

Simpson's 3/8 rule requires n a multiple of 3, h=(b-a)/n. Sum f(a)+f(b), add 3*f for interior points not multiples of 3 and 2*f for multiples of 3, then multiply by 3h/8.

## Answer

Simpson's 3/8 rule needs the number of sub-intervals n to be a multiple of 3. With h = (b − a)/n:

I ≈ (3h/8)[y₀ + yₙ + 3(y₁ + y₂ + y₄ + y₅ + …) + 2(y₃ + y₆ + y₉ + …)]

So every interior point whose index is a multiple of 3 gets weight 2; all other interior points get weight 3.

#### Pseudo-code
```text
START
  DEFINE function f(x)
  READ a, b, n
  IF n mod 3 ≠ 0 THEN
      PRINT "n must be a multiple of 3"
      STOP
  ENDIF
  h   ← (b − a) / n
  sum ← f(a) + f(b)
  FOR i ← 1 TO n − 1
      x ← a + i·h
      IF i mod 3 = 0 THEN
          sum ← sum + 2·f(x)
      ELSE
          sum ← sum + 3·f(x)
      ENDIF
  ENDFOR
  I ← 3·h·sum / 8
  PRINT I
STOP
```

#### C implementation
```c
#include <stdio.h>
#include <math.h>

double f(double x)
{
    return 1.0 / (1.0 + x * x);          /* function to integrate */
}

int main(void)
{
    double a, b, h, sum, x;
    int n, i;

    printf("Enter a, b and n (n multiple of 3): ");
    scanf("%lf %lf %d", &a, &b, &n);
    if (n % 3 != 0)
    {
        printf("n must be a multiple of 3\n");
        return 1;
    }
    h = (b - a) / n;
    sum = f(a) + f(b);
    for (i = 1; i < n; i++)
    {
        x = a + i * h;
        if (i % 3 == 0) sum += 2 * f(x);
        else            sum += 3 * f(x);
    }
    printf("Integral = %.4f\n", 3 * h / 8 * sum);
    return 0;
}
```
Sample run: for f = 1/(1+x²), a = 0, b = 1, n = 6 the program prints `Integral = 0.7854` (exact π/4 = 0.7854).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What check does the pseudo-code make?
   A. n mod 2 = 0
   B. n mod 4 = 0
   C. n > 3
   D. n mod 3 = 0
   Answer: D. The 3/8 rule needs a multiple of 3. (Key point: n condition)

2. Which interior points get weight 2?
   A. Indices that are multiples of 3
   B. Even indices
   C. Odd indices
   D. The first interior point
   Answer: A. Others get weight 3. (Key point: Weights)

3. What is the final computation?
   A. I = h·sum/3
   B. I = 3·h·sum/8
   C. I = h·sum/8
   D. I = 3·sum/h
   Answer: B. Simpson's 3/8 factor is 3h/8. (Key point: Final formula)

4. What is sum initialised to?
   A. 0
   B. f(a)
   C. f(a) + f(b)
   D. 3f(a)+3f(b)
   Answer: C. End points have weight 1. (Key point: Initial sum)

## Related in Numerical Method

- [Derive Simpson's 1/3 rule](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Aderive-simpson-13) — Numerical Method, chapter 5
- [Gauss-Jordan pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Agauss-jordan-pseudocode) — Numerical Method, chapter 3, asked 4×
- [Bisection method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Abisection-pseudocode) — Numerical Method, chapter 2, asked 3×
- [False position pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Afalse-position-pseudocode) — Numerical Method, chapter 2, asked 2×
- [Power method pseudo-code](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Apower-method-algorithm) — Numerical Method, chapter 3, asked 2×
- [Composite Simpson 1/3 algorithm](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Asimpson-13-algorithm) — Numerical Method, chapter 5

## Related in other subjects

- [Algorithm/pseudo-code for software PID](https://www.bctengineer.com/question.md?key=semester_6%2Fembedded_system%3Apid-software-algorithm) — Embedded System, chapter 7, asked 3×
- [Algorithm versus pseudo-code](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aalgorithm-vs-pseudocode) — Computer Programming, chapter 2
- [Map sequence diagram to pseudo code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aorder-model-mapping-pseudocode) — Object Oriented Analysis and Design, chapter 4
- [Offsets area: trapezoidal, Simpson, coordinate](https://www.bctengineer.com/question.md?key=semester_3%2Fsurveying_i%3Aarea-offsets-curved-boundary-trapezoidal-simpson-coordinate) — Surveying I, chapter 9
