C++
2D scatter plot
data fitting
linear regression
programming tutorial

How to fit the 2D scatter data with a line with C

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

Fitting 2D scatter data with a line usually means ordinary least squares linear regression. Given points x, y, you want a line y = m x + b that minimizes the sum of squared vertical residuals. For that problem, you do not need a large numerical library; a small C program can compute the slope and intercept directly from sums over the data.

The Least-Squares Formula

For n points, the standard formulas are:

  • 'm = (n * sum(xy) - sum(x) * sum(y)) / (n * sum(x^2) - sum(x)^2)'
  • 'b = (sum(y) - m * sum(x)) / n'

These formulas assume:

  • 'x is the independent variable'
  • the goal is to minimize vertical error in y
  • the best-fit line is not vertical

That last assumption matters. If all x values are identical, the denominator becomes zero and the slope-intercept form breaks down.

A Runnable C Implementation

c
1#include <stdio.h>
2#include <stdlib.h>
3
4int main(void) {
5    double xs[] = {1, 2, 3, 4, 5};
6    double ys[] = {2, 4, 5, 4, 5};
7    int n = 5;
8
9    double sum_x = 0.0;
10    double sum_y = 0.0;
11    double sum_xy = 0.0;
12    double sum_x2 = 0.0;
13
14    for (int i = 0; i < n; i++) {
15        sum_x += xs[i];
16        sum_y += ys[i];
17        sum_xy += xs[i] * ys[i];
18        sum_x2 += xs[i] * xs[i];
19    }
20
21    double denominator = n * sum_x2 - sum_x * sum_x;
22    if (denominator == 0.0) {
23        fprintf(stderr, "Cannot fit y = m x + b when all x values are identical\n");
24        return 1;
25    }
26
27    double m = (n * sum_xy - sum_x * sum_y) / denominator;
28    double b = (sum_y - m * sum_x) / n;
29
30    printf("slope = %.6f\n", m);
31    printf("intercept = %.6f\n", b);
32
33    return 0;
34}

Compile and run it:

bash
gcc fit_line.c -o fit_line
./fit_line

This is enough for many practical cases where you just need a basic straight-line fit.

What the Numbers Mean

The slope m tells you how much y changes when x increases by one unit. The intercept b is the predicted value of y when x is zero.

If the fitted line is y = 0.6 x + 2.2, then every additional unit of x raises the prediction by about 0.6.

That interpretation is easy, but do not mistake interpretability for correctness. A line can be easy to describe and still be the wrong model for the data.

Check the Residuals

A line fit is only useful if a line is a sensible model. If the points curve, cluster into multiple groups, or are dominated by outliers, a straight line may be misleading.

A simple residual calculation helps:

c
1for (int i = 0; i < n; i++) {
2    double predicted = m * xs[i] + b;
3    double residual = ys[i] - predicted;
4    printf("point %d residual = %.6f\n", i, residual);
5}

Residuals close to zero suggest the line is reasonable. A pattern in the residuals usually means the model is missing structure.

Know the Limitation of Ordinary Least Squares

This approach minimizes vertical distances, not geometric distance to the line. That is correct for many regression problems, but not for every line-fitting problem.

If you want the best geometric line through scattered points, especially for nearly vertical lines, you may need total least squares or another orthogonal-distance method.

So the right question is not just "how do I fit a line?" It is "what kind of line-fitting problem do I actually have?"

Common Pitfalls

The biggest mistake is applying the least-squares line formula to data that is obviously nonlinear.

Another mistake is ignoring the zero-denominator case when all x values are identical or nearly identical.

A third issue is trusting the line parameters without inspecting residuals or plotting the points.

Summary

  • A straight-line fit in C can be implemented with ordinary least squares using simple sums
  • The formulas give slope and intercept for y = m x + b
  • This approach assumes vertical residual minimization, not arbitrary geometric distance
  • Always check for the vertical-line failure case and inspect residuals
  • A small C program is enough for basic regression when the model assumptions are appropriate

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.