Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b9e442b
Refactor linear search to use vector instead of array
twilight-debugger Feb 25, 2026
4290065
Merge pull request #1 from twilight-debugger/twilight-debugger-patch-1
twilight-debugger Feb 25, 2026
56e6dc8
Refactor rock-paper-scissors game logic
twilight-debugger Feb 25, 2026
e458186
Merge pull request #2 from twilight-debugger/twilight-debugger-patch-2
twilight-debugger Feb 25, 2026
18d3001
Refactor linear search to use vectors and improve output
twilight-debugger Feb 25, 2026
625b98a
Merge pull request #3 from twilight-debugger/twilight-debugger-patch-3
twilight-debugger Feb 25, 2026
e4f13f0
Refactor simple calculator to use calculate function
twilight-debugger Feb 25, 2026
09032ed
Merge pull request #4 from twilight-debugger/twilight-debugger-patch-4
twilight-debugger Feb 25, 2026
1b3648f
Refactor linear search implementation and fix output
twilight-debugger Feb 25, 2026
2be5c73
Refactor rock-paper-scissors game implementation
twilight-debugger Feb 25, 2026
df12731
Merge pull request #5 from twilight-debugger/twilight-debugger-patch-5
twilight-debugger Feb 25, 2026
ce9a6f1
Refactor spiral matrix generation for clarity
twilight-debugger Mar 5, 2026
fb1c51d
Merge pull request #6 from twilight-debugger/twilight-debugger-patch-6
twilight-debugger Mar 5, 2026
617b4db
Refactor largest element finder in C++
twilight-debugger Mar 5, 2026
446c0ea
Merge pull request #7 from twilight-debugger/twilight-debugger-patch-7
twilight-debugger Mar 5, 2026
a9a4dcb
Refactor countX function and update driver code
twilight-debugger Mar 6, 2026
0ebc4d2
Merge pull request #8 from twilight-debugger/twilight-debugger-patch-8
twilight-debugger Mar 6, 2026
458b31b
Enhance code documentation with comments
twilight-debugger Mar 6, 2026
adb3e58
Merge pull request #9 from twilight-debugger/twilight-debugger-patch-9
twilight-debugger Mar 6, 2026
4b71725
Enhance DelannoyGenerator function with comments
twilight-debugger Mar 15, 2026
0236268
Merge pull request #10 from twilight-debugger/twilight-debugger-patch-10
twilight-debugger Mar 15, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions code/data_structures/src/2d_array/SpiralMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,81 @@
#include <vector>
using namespace std;

// Function to generate an n x n spiral matrix
vector<vector<int>> generateSpiralMatrix(int n) {

// Create an empty n x n matrix filled with 0
vector<vector<int>> matrix(n, vector<int>(n));

int left = 0;
int right = n - 1;
int top = 0;
int bottom = n - 1;
// These variables represent the current boundaries
int left = 0; // left column boundary
int right = n - 1; // right column boundary
int top = 0; // top row boundary
int bottom = n - 1; // bottom row boundary

// Direction indicator
// 0 -> left to right
// 1 -> top to bottom
// 2 -> right to left
// 3 -> bottom to top
int direction = 0;

// Value that will be filled inside the matrix
int value = 1;

// Continue while the boundaries are valid
while (left <= right && top <= bottom) {

// Move from LEFT to RIGHT across the top row
if (direction == 0) {
for (int i = left; i <= right; i++) {
matrix[top][i] = value++;
matrix[top][i] = value++; // fill value then increment
}
top++;
} else if (direction == 1) {
top++; // move the top boundary downward
}

// Move from TOP to BOTTOM down the right column
else if (direction == 1) {
for (int j = top; j <= bottom; j++) {
matrix[j][right] = value++;
}
right--;
} else if (direction == 2) {
right--; // move the right boundary left
}

// Move from RIGHT to LEFT across the bottom row
else if (direction == 2) {
for (int i = right; i >= left; i--) {
matrix[bottom][i] = value++;
}
bottom--;
} else {
bottom--; // move the bottom boundary upward
}

// Move from BOTTOM to TOP up the left column
else {
for (int j = bottom; j >= top; j--) {
matrix[j][left] = value++;
}
left++;
left++; // move the left boundary right
}

// Change direction (cycle through 0 → 1 → 2 → 3 → 0)
direction = (direction + 1) % 4;
}

// Return the completed spiral matrix
return matrix;
}

int main() {
int n;

// Take matrix size as input
cin >> n;

// Generate the spiral matrix
vector<vector<int>> spiralMatrix = generateSpiralMatrix(n);

// Print the matrix
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << spiralMatrix[i][j] << " ";
Expand Down
56 changes: 28 additions & 28 deletions code/languages/c/linear_search/linear_search.c
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
#include <stdio.h>
#include <stdlib.h>
/*
Input : integer array indexed from 0, key to be searched
Ouput : Position of the key in the array if found, else -1
*/
int linearSearch(int a[], int n, int key) {
int pos = -1;
counter=0;
for (int i = 0; i < n; ++i) {
if (a[i] == key) {
pos = i;
break;
#include <stdio.h>

/*
Input : integer array indexed from 0, key to be searched
Output : Position of the key in the array if found, else -1
*/

int linearSearch(int arr[], int size, int key)
{
for (int i = 0; i < size; ++i)
{
if (arr[i] == key)
{
return i; // key found
}
}
return pos;
}
return -1; // key not found
}

int main()
{
int arr[] = {8, 12, 3, 4, 7, 2, 13};
int size = sizeof(arr) / sizeof(arr[0]);

int main() {
int a[] = {8, 12, 3, 4, 7, 2, 13};
int n = sizeof(a) / sizeof(a[0]);
int pos = linearSearch(a, n, 13);
if (pos != 1)
printf("Key found at position : %d \n", pos);
int pos = linearSearch(arr, size, 12);

if (pos != -1)
printf("Key found at position : %d\n", pos);
else
printf("Key not found \n");
return 0;
}
printf("Key not found\n");

/*
Output :
Key found at position : 2
*/
return 0;
}
126 changes: 56 additions & 70 deletions code/languages/c/rock_paper_scissor/rock_game.c
Original file line number Diff line number Diff line change
@@ -1,101 +1,87 @@
// Part of Cosmos by OpenGenus
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


int generaterandomfunc(int n)
// Generate random choice: 0, 1, or 2
int generateRandom(int n)
{
srand(time(NULL));
return rand() % n;
}
int greater(char char1, char char2)
{
// return 1 if c1>c2 and 0 otherwise . if c1==c2 it will return -1

if (char1 == char2)
{
// Returns:
// 1 -> first wins
// 0 -> second wins
// -1 -> draw
int compare(char p1, char p2)
{
if (p1 == p2)
return -1;
}
else if ((char1 == 'r') && (char2 == 's'))
{
return 1;
}
else if ((char2 == 'r' && char1 == 's'))
{
return 0;
}

else if ((char1 == 'p') && (char2 == 'r'))
{
if ((p1 == 'r' && p2 == 's') ||
(p1 == 'p' && p2 == 'r') ||
(p1 == 's' && p2 == 'p'))
return 1;
}
else if ((char2 == 'p') && (char1 == 'r'))
{
return 0;
}

else if ((char1 == 's') && (char2 == 'p'))
{
return 1;
}
else if ((char2 == 's') && (char1 == 'p'))
{
return 0;
}
return 0;
}

int main()
{
int playerscore = 0, compscore = 0, temp;
char playerchar, compchar;
char dict[45] = {'r', 'p', 's'};
printf("welcome to the rock paper scissor game\n");
int playerScore = 0, cpuScore = 0;
int choice;
char playerChar, cpuChar;
char options[] = {'r', 'p', 's'};

for (int i = 0; i < 3; i++)
srand(time(NULL)); // seed ONCE

printf("Welcome to Rock Paper Scissors Game 🎮\n");

for (int i = 0; i < 3; i++)
{
printf("choose 1 for rock choose 2 for paper choose 3 for scissor \n");
printf("player no.1 turn \n");
scanf("%d", &temp);
playerchar = dict[temp - 1];
printf("you choose %c\n", playerchar);

printf("choose 1 for rock choose 2 for paper choose 3 for scissor \n");
printf("player no.1 turn \n");
temp = generaterandomfunc(3) + 1;
compchar = dict[temp - 1];
printf("cpu choose %c\n", compchar);
if (greater(compchar, playerchar) == 1) //compchar is greater than player char
printf("\nRound %d\n", i + 1);
printf("Choose:\n1. Rock\n2. Paper\n3. Scissors\n");
scanf("%d", &choice);

if (choice < 1 || choice > 3)
{
compscore += 1;
printf("cpu got it hurray\n");
printf("Invalid choice! Try again.\n");
i--;
continue;
}
else if (greater(compchar, playerchar) == -1)

playerChar = options[choice - 1];
cpuChar = options[generateRandom(3)];

printf("You chose: %c\n", playerChar);
printf("CPU chose: %c\n", cpuChar);

int result = compare(playerChar, cpuChar);

if (result == 1)
{
compscore += 1;
playerscore += 1;
printf("its a draw \n");
printf("You win this round! 🎉\n");
playerScore++;
}
else if (result == 0)
{
printf("CPU wins this round 🤖\n");
cpuScore++;
}
else
{
playerscore += 1;
printf("you got it hurray\n");
printf("It's a draw 🤝\n");
}

printf("you : %d \ncpu : %d \n", playerscore, compscore);
}
if (playerscore > compscore)
{
printf("you win \n");
}
else if (compscore > playerscore)
{
printf("cpu win \n");
printf("Score → You: %d | CPU: %d\n", playerScore, cpuScore);
}

printf("\nFinal Result:\n");
if (playerScore > cpuScore)
printf("You won the game 🏆\n");
else if (cpuScore > playerScore)
printf("CPU won the game 🤖\n");
else
{
printf("match is draw");
}
printf("Game is a draw 🤝\n");

return 0;
}
74 changes: 43 additions & 31 deletions code/languages/cpp/calculator/simpleCalculator.cpp
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
# include <iostream>
#include <iostream>
using namespace std;

int main() {

char op;
float num1, num2;

cout << "Enter operator: +, -, *, /: ";
cin >> op;

cout << "Enter two operands: ";
cin >> num1 >> num2;

switch(op) {
// User-defined function
float calculate(float num1, float num2, char op)
{
switch (op)
{
case '+':
return num1 + num2;

case '-':
return num1 - num2;

case '*':
return num1 * num2;

case '/':
if (num2 == 0)
{
cout << "Error! Division by zero.\n";
return 0;
}
return num1 / num2;

default:
cout << "Error! Operator is not correct.\n";
return 0;
}
}

case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
int main()
{
char op;
float num1, num2, result;

case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
cout << "Enter operator (+, -, *, /): ";
cin >> op;

case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
cout << "Enter two operands: ";
cin >> num1 >> num2;

case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
result = calculate(num1, num2, op);

default:
// If the operator is other than +, -, * or /, error message is shown
cout << "Error! operator is not correct";
break;
}
// Print result only if operator is valid
if (op == '+' || op == '-' || op == '*' || op == '/')
{
cout << num1 << " " << op << " " << num2 << " = " << result;
}

return 0;
return 0;
}
Loading