C++ Algorithm and Data Structure Code Examples
Eratosthenes
Prime Number Checker
This code checks if a given number is prime using the Sieve of Eratosthenes algorithm.
#include
#include
#include
using namespace std;
const int Max = 1000;
int main() {
vector es_primer(1000001, true);
es_primer[0] = es_primer[1] = false;
for (int i = 2; i < Max; ++i) {
if (es_primer[i]) {
for (int m = 2 * i; m <= Max * Max; m += i) {
es_primer[m] = false;
}
}
}
int x;
while (cin >> x) {
cout << x;
if (es_primer[x])
cout << " es primer" <
Java Code for Queue and Stack Data Structures
Queue Class
First, we import the ArrayList
class:
import java.util.ArrayList;
This is the Cola
(Queue) class, which extends ArrayList
:
public class Cola extends ArrayList {
public void encolar(Object dato) {
if (dato != null) {
if (!esLlena())
this.add(dato);
else
System.out.println("La cola esta llena.");
} else {
System.out.println("Introduzca un dato no nulo");
}
}
public void encolar(int pos,
Read More
Pneumatic, Hydraulic, Linear, and Piezoelectric Actuators
Pneumatic Actuators
A pneumatic actuator is a mechanical device that converts compressed air pressure into mechanical motion or force. It’s commonly used in various industrial applications to control valves, dampers, gates, and other mechanical components. Pneumatic actuators are preferred for their simplicity, reliability, and suitability for high-force applications in environments where electrical actuators may not be practical or safe.
How Pneumatic Actuators Work
- Pneumatic Power Source: Pneumatic
Understanding Signals: Phase, Frequency, and Modulation
Signal: A sequence of voltage changes characterized by phase, amplitude, and frequency.
Phase: A measure of the relative position in time within a single period of a signal.
Amplitude: The instantaneous value of the signal at a given time.
Frequency: The number of repetitions per unit of time, measured in Hertz (Hz). It is the inverse of the period.
Hertz (Hz): Equivalent to one cycle per second.
Signal Spectrum: The range of signal values from minimum to maximum frequency.
Digital Signals: Discrete signals
Read MoreNetwork Cables and Components: Types and Uses
Twisted Pair Wiring
Twisted pair cables consist of pairs of insulated copper wires twisted together. This twisting helps reduce electromagnetic interference (EMI) and crosstalk.
Types
There are two main types: Unshielded Twisted Pair (UTP) and Shielded Twisted Pair (STP). UTP is common for Ethernet, while STP has additional shielding for better EMI protection.
Categories
Twisted pair cables are categorized into different performance levels, such as Cat 5e, Cat 6, Cat 6a, and Cat 7, with higher categories
Read MoreJava Code Snippets: BSTNode and Graph Algorithms
private BSTNode removeLeaves (BSTNode curr)
{
if (curr == null)
return null;
if (curr.left == null && curr.right == null){
treeSize–;
return null;
}
curr.left = removeLeaves (curr.left);
curr.right = removeLeaves(curr.right);
return curr;
}
public String getEdges (boolean asc)
{
TreeMap ,>> m = null;
if (asc == true)
m = new TreeMap ,>> (new Less <><>());
else
m = new TreeMap ,>> (new Greater ());
for (Entry e1: adjacencyMap.entrySet())
Read More