it's late night, dark, give me some cool stuff, relatively quick, fun, math/physics…
- User
it's late night, dark, give me some cool stuff, relatively quick, fun, math/physics based maybe, in cpp (no graphics)
- Assistant
it's late night, dark, give me some cool stuff, relatively quick, fun, math/physics based maybe, in cpp (no graphics)
Sure, late-night coding sessions can be both fun and rewarding! Here are a few math and physics-based C++ project ideas that are relatively quick to implement and don't require graphics:
A classic cellular automaton simulation. You can represent the grid using characters in the console.
Key Concepts:
Basic Implementation Steps:
*) and dead (.) cells.Snippet Example:
#include <iostream>
#include <vector>
#include <unistd.h> // For usleep
const int ROWS = 20;
const int COLS = 40;
using Grid = std::vector<std::vector<char>>;
// Initialize grid with some pattern
Grid initializeGrid() {
Grid grid(ROWS, std::vector<char>(COLS, '.'));
// Example: Glider
grid[1][2] = grid[2][3] = grid[3][1] = grid[3][2] = grid[3][3] = '*';
return grid;
}
// Count alive neighbors
int aliveNeighbors(const Grid& grid, int x, int y) {
int count = 0;
for(int i=-1;i<=1;i++) {
for(int j=-1;j<=1;j++) {
if(i == 0 && j == 0) continue;
int ni = x + i, nj = y + j;
if(ni >=0 && ni < ROWS && nj >=0 && nj < COLS && grid[ni][nj] == '*')
count++;
}
}
return count;
}
// Update grid
Grid updateGrid(const Grid& grid) {
Grid newGrid = grid;
for(int i=0;i<ROWS;i++) {
for(int j=0;j<COLS;j++) {
int neighbors = aliveNeighbors(grid, i, j);
if(grid[i][j] == '*' && (neighbors < 2 || neighbors > 3))
newGrid[i][j] = '.';
else if(grid[i][j] == '.' && neighbors == 3)
newGrid[i][j] = '*';
}
}
return newGrid;
}
// Display grid
void display(const Grid& grid) {
system("clear"); // For Unix/Linux. Use "cls" for Windows.
for(auto &row : grid) {
for(auto cell : row)
std::cout << cell;
std::cout << '\n';
}
}
int main() {
Grid grid = initializeGrid();
while(true) {
display(grid);
grid = updateGrid(grid);
usleep(200000); // Sleep for 200ms
}
}
Simulate simple physical systems like projectile motion or the harmonic oscillator using numerical methods (Euler, Runge-Kutta).
Key Concepts:
Example Project: Simple Harmonic Oscillator
Implementation Steps:
x) and velocity (v).Snippet Example:
#include <iostream>
#include <vector>
#include <iomanip>
struct State {
double x; // Position
double v; // Velocity
};
int main() {
double k = 1.0; // Spring constant
double dt = 0.01; // Time step
int steps = 1000;
State state = {1.0, 0.0}; // Initial condition: x=1, v=0
std::cout << std::fixed << std::setprecision(4);
std::cout << "Time\tPosition\tVelocity\n";
for(int i=0;i<=steps;i++) {
double t = i * dt;
std::cout << t << "\t" << state.x << "\t\t" << state.v << "\n";
// Euler's method
double a = -k * state.x;
state.x += state.v * dt;
state.v += a * dt;
}
}
Efficiently generate and list prime numbers up to a specified limit.
Key Concepts:
Implementation Steps:
isPrime initialized to true.Snippet Example:
#include <iostream>
#include <vector>
#include <cmath>
std::vector<int> sieve(int n) {
std::vector<bool> isPrime(n+1, true);
isPrime[0] = isPrime[1] = false;
int sqrt_n = std::sqrt(n);
for(int p=2; p<=sqrt_n; p++) {
if(isPrime[p]) {
for(int multiple=p*p; multiple<=n; multiple+=p)
isPrime[multiple] = false;
}
}
std::vector<int> primes;
for(int p=2; p<=n; p++) if(isPrime[p]) primes.push_back(p);
return primes;
}
int main() {
int limit;
std::cout << "Find primes up to: ";
std::cin >> limit;
std::vector<int> primes = sieve(limit);
std::cout << "Primes up to " << limit << ":\n";
for(auto p : primes) std::cout << p << " ";
std::cout << "\nTotal: " << primes.size() << "\n";
}
Generate and display the Mandelbrot set using ASCII characters in the console.
Key Concepts:
Implementation Steps:
Snippet Example:
#include <iostream>
#include <complex>
int main() {
const int width = 80, height = 40;
const int maxIter = 1000;
const double x_min = -2.0, x_max = 1.0;
const double y_min = -1.0, y_max = 1.0;
const std::string shades = " .:-=+*#%@";
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
double a = x_min + (x / (double)width) * (x_max - x_min);
double b = y_min + (y / (double)height) * (y_max - y_min);
std::complex<double> c(a, b);
std::complex<double> z = 0;
int iter = 0;
while(abs(z) < 2.0 && iter < maxIter) {
z = z * z + c;
iter++;
}
if(iter == maxIter)
std::cout << "@";
else
std::cout << shades[iter * shades.size() / maxIter];
}
std::cout << "\n";
}
}
Simulate the motion of celestial bodies under mutual gravitational attraction.
Key Concepts:
Implementation Steps:
Body structure with mass, position, and velocity.Snippet Example:
#include <iostream>
#include <vector>
#include <cmath>
struct Vector {
double x, y;
};
struct Body {
double mass;
Vector position;
Vector velocity;
};
int main() {
const double G = 6.67430e-11;
double dt = 1e5; // Time step in seconds
int steps = 1000;
// Initialize bodies (e.g., Earth and Moon)
std::vector<Body> bodies = {
{5.972e24, {0, 0}, {0, 0}}, // Earth
{7.348e22, {384400000, 0}, {0, 1022}} // Moon
};
for(int step=0; step<steps; step++) {
// Calculate forces
std::vector<Vector> forces(bodies.size(), Vector{0, 0});
for(int i=0; i<bodies.size(); i++) {
for(int j=i+1; j<bodies.size(); j++) {
double dx = bodies[j].position.x - bodies[i].position.x;
double dy = bodies[j].position.y - bodies[i].position.y;
double dist = std::sqrt(dx*dx + dy*dy);
double force = (G * bodies[i].mass * bodies[j].mass) / (dist * dist + 1e3); // Added softening
double fx = force * dx / dist;
double fy = force * dy / dist;
forces[i].x += fx;
forces[i].y += fy;
forces[j].x -= fx;
forces[j].y -= fy;
}
}
// Update velocities and positions
for(int i=0; i<bodies.size(); i++) {
bodies[i].velocity.x += (forces[i].x / bodies[i].mass) * dt;
bodies[i].velocity.y += (forces[i].y / bodies[i].mass) * dt;
bodies[i].position.x += bodies[i].velocity.x * dt;
bodies[i].position.y += bodies[i].velocity.y * dt;
}
// Simple output (positions)
std::cout << "Step " << step << ":\n";
for(int i=0; i<bodies.size(); i++) {
std::cout << "Body " << i << ": (" << bodies[i].position.x << ", " << bodies[i].position.y << ")\n";
}
std::cout << "--------------------------\n";
}
}
Note: This is a very basic simulation. For more accuracy, consider using more advanced integration methods like Verlet or Runge-Kutta.
Implement a simple matrix library to perform operations like addition, multiplication, and finding determinants.
Key Concepts:
Implementation Steps:
Matrix class or use std::vector<std::vector<double>>.Snippet Example:
#include <iostream>
#include <vector>
using Matrix = std::vector<std::vector<double>>;
// Function to perform matrix multiplication
Matrix multiply(const Matrix& A, const Matrix& B) {
int rows = A.size();
int cols = B[0].size();
int inner = A[0].size();
Matrix C(rows, std::vector<double>(cols, 0));
for(int i=0;i<rows;i++) {
for(int j=0;j<cols;j++) {
for(int k=0;k<inner;k++) {
C[i][j] += A[i][k] * B[k][j];
}
}
}
return C;
}
// Function to display matrix
void display(const Matrix& A) {
for(auto &row : A) {
for(auto val : row) std::cout << val << "\t";
std::cout << "\n";
}
}
int main() {
// Example matrices
Matrix A = {{1, 2, 3},
{4, 5, 6}};
Matrix B = {{7, 8},
{9, 10},
{11, 12}};
std::cout << "Matrix A:\n";
display(A);
std::cout << "Matrix B:\n";
display(B);
Matrix C = multiply(A, B);
std::cout << "A * B:\n";
display(C);
}
These projects should keep your late-night coding session both engaging and educational. Feel free to expand upon them or combine different ideas to create something unique. Happy coding!
Not yet included in a published release.