Help us measure site usage?

With your permission, we use self-hosted analytics to understand page usage. It is optional and does not affect the Service. Learn more.

C++beginnercliAI generated

Matrix Calculator CLI

Build a matrix calculator for the terminal in C++. You enter two matrices (typed in, or loaded from a text file) and choose an operation: addition, multiplication, transpose or multiplying by a number. The result is printed in neat, aligned columns. Operations that aren't allowed, such as adding a 2×3 matrix to a 3×2 one, are refused with a clear explanation. At the heart of it is your own Matrix class, which lets you write A + B and A * B just like with numbers. You'll learn classes and constructors, operator overloading, std::vector, exceptions, reading files, and writing simple tests.

Estimate
~9h
Steps
6
Completed by
0
Proposed by
codeseed.app

Standard Library (STL)

Project roadmap

  1. 01

    Create the Matrix class

    ~1.5h

    Create matrix.h and matrix.cpp with a Matrix class storing its number of rows and columns and its values in a std::vector<double> (row after row). Add a constructor taking the size (filled with zeros), rows() and cols(), and an at(row, col) method that checks the indexes and throws std::out_of_range when they're invalid. Done when: a small program creates a 2×3 matrix, sets values and reads them back.

  2. 02

    Read matrices

    ~1.5h

    Write a function that reads a matrix from any input stream: first the number of rows and columns, then the values. Use it both for typing a matrix and for loading one from a file with std::ifstream. Reject bad input (letters, too few values, a size of 0) with a clear message. Done when: matrices can be typed in and loaded from matrices/a.txt.

  3. 03

    Add operators

    ~2h

    Overload operator+ and operator- (the sizes must be equal), operator* for two matrices (A's columns must equal B's rows; each result cell is a row of A times a column of B, summed) and operator* for a matrix and a number. When sizes don't fit, throw std::invalid_argument with a message showing both sizes. Done when: A + B and A * B give the results you calculated by hand.

  4. 04

    Add transpose and printing

    ~1h

    Add a transpose() method returning a new matrix with rows and columns swapped. Overload operator<< to print a matrix with every column aligned, using std::setw and a fixed number of decimals, for example 2. Done when: a 2×3 matrix transposes to 3×2 and prints in neat columns.

  5. 05

    Build the menu

    ~1.5h

    Write main.cpp with a loop menu: load or type matrix A, load or type matrix B, show both, A + B, A - B, A × B, transpose A, multiply A by a number, and quit. Catch exceptions around each operation and show their message without closing the program. Done when: every menu option works, and invalid operations show an error and return to the menu.

  6. 06

    Write tests

    ~1.5h

    Write a tests.cpp program using assert (or a small framework like doctest) that checks: addition and multiplication with known results, multiplying by the identity matrix returns the same matrix, transposing twice returns the original, and mismatched sizes throw. Build everything with a Makefile or CMake. Done when: the tests pass, and both programs build with one command.

Ready to build this?

Get a GitHub repo and start building. Your AI reviewer checks each step as you go.

~9h · 6 steps

Tech stack

Standard Library (STL)