-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d90768
commit e81d75a
Showing
3 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include <iostream> | ||
#include <memory> | ||
#include <vector> | ||
using namespace std; | ||
|
||
// Abstract Base Class (Polymorphism in action) | ||
class Animal { | ||
public: | ||
virtual void makeSound() const = 0; // Pure virtual function | ||
virtual ~Animal() {} // Virtual destructor for cleanup | ||
}; | ||
|
||
// Derived class: Dog | ||
class Dog : public Animal { | ||
public: | ||
void makeSound() const override { | ||
cout << "🐶 Dog says: Woof! Woof!\n"; | ||
} | ||
}; | ||
|
||
// Derived class: Cat | ||
class Cat : public Animal { | ||
public: | ||
void makeSound() const override { | ||
cout << "🐱 Cat says: Meow! Meow!\n"; | ||
} | ||
}; | ||
|
||
// Derived class: Bird | ||
class Bird : public Animal { | ||
public: | ||
void makeSound() const override { | ||
cout << "🐦 Bird says: Tweet! Tweet!\n"; | ||
} | ||
}; | ||
|
||
// Template function to add animals dynamically | ||
template <typename T> | ||
void addAnimal(vector<unique_ptr<Animal>>& zoo) { | ||
zoo.push_back(make_unique<T>()); | ||
} | ||
|
||
int main() { | ||
vector<unique_ptr<Animal>> zoo; | ||
|
||
cout << "Welcome to the Animal Zoo Simulator 🦁🐘!\n"; | ||
|
||
// Using templates to add different animals | ||
addAnimal<Dog>(zoo); | ||
addAnimal<Cat>(zoo); | ||
addAnimal<Bird>(zoo); | ||
|
||
cout << "\nThe animals in the zoo are making sounds...\n"; | ||
for (const auto& animal : zoo) { | ||
animal->makeSound(); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
#include <fstream> | ||
#include <complex> | ||
#include <vector> | ||
#include <thread> | ||
|
||
const int WIDTH = 800; | ||
const int HEIGHT = 800; | ||
const int MAX_ITER = 1000; | ||
|
||
// Function to compute Mandelbrot set for a given pixel | ||
int mandelbrot(std::complex<double> c) { | ||
std::complex<double> z = 0; | ||
int iterations = 0; | ||
while (std::abs(z) <= 2.0 && iterations < MAX_ITER) { | ||
z = z * z + c; | ||
++iterations; | ||
} | ||
return iterations; | ||
} | ||
|
||
// Function to render a portion of the Mandelbrot set (executed by each thread) | ||
void render_section(int start_row, int end_row, std::vector<int>& image) { | ||
for (int y = start_row; y < end_row; ++y) { | ||
for (int x = 0; x < WIDTH; ++x) { | ||
std::complex<double> c( | ||
(x - WIDTH / 2.0) * 4.0 / WIDTH, | ||
(y - HEIGHT / 2.0) * 4.0 / HEIGHT | ||
); | ||
int color = mandelbrot(c); | ||
image[y * WIDTH + x] = color; | ||
} | ||
} | ||
} | ||
|
||
int main() { | ||
std::vector<int> image(WIDTH * HEIGHT); | ||
int num_threads = std::thread::hardware_concurrency(); // Use all available cores | ||
std::vector<std::thread> threads; | ||
|
||
std::cout << "Generating Mandelbrot set using " << num_threads << " threads...\n"; | ||
|
||
// Divide the work among threads | ||
int rows_per_thread = HEIGHT / num_threads; | ||
for (int i = 0; i < num_threads; ++i) { | ||
int start_row = i * rows_per_thread; | ||
int end_row = (i == num_threads - 1) ? HEIGHT : start_row + rows_per_thread; | ||
threads.emplace_back(render_section, start_row, end_row, std::ref(image)); | ||
} | ||
|
||
// Join all threads | ||
for (auto& thread : threads) { | ||
thread.join(); | ||
} | ||
|
||
// Write the image to a PPM file | ||
std::ofstream ofs("mandelbrot.ppm"); | ||
ofs << "P3\n" << WIDTH << " " << HEIGHT << "\n255\n"; | ||
for (int i = 0; i < WIDTH * HEIGHT; ++i) { | ||
int color = (255 * image[i]) / MAX_ITER; | ||
ofs << color << " " << 0 << " " << 255 - color << "\n"; | ||
} | ||
ofs.close(); | ||
|
||
std::cout << "Mandelbrot set generated! Check 'mandelbrot.ppm' for the result.\n"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
void clear_screen() { | ||
#ifdef _WIN32 | ||
system("cls"); | ||
#else | ||
system("clear"); | ||
#endif | ||
} | ||
|
||
int main() { | ||
int secret_number, guess, attempts = 0; | ||
char player_name[50]; | ||
char play_again; | ||
|
||
// Seed random number generator | ||
srand(time(NULL)); | ||
|
||
printf("Welcome to the MYSTERIOUS Guessing Game!\n"); | ||
printf("Enter your name, brave soul: "); | ||
scanf("%s", player_name); | ||
|
||
do { | ||
clear_screen(); | ||
printf("Alright %s, I have chosen a number between 1 and 100.\n", player_name); | ||
printf("Can you guess it? Let's see!\n"); | ||
|
||
secret_number = (rand() % 100) + 1; | ||
attempts = 0; | ||
|
||
while (1) { | ||
printf("\nEnter your guess: "); | ||
scanf("%d", &guess); | ||
attempts++; | ||
|
||
if (guess < secret_number) { | ||
printf("Too low! Try again."); | ||
} else if (guess > secret_number) { | ||
printf("Too high! Try again."); | ||
} else { | ||
printf("Congratulations, %s! You've guessed it in %d attempts!\n", player_name, attempts); | ||
break; | ||
} | ||
} | ||
|
||
printf("\nDo you want to play again? (y/n): "); | ||
scanf(" %c", &play_again); | ||
} while (play_again == 'y' || play_again == 'Y'); | ||
|
||
printf("\nThanks for playing, %s! See you next time.\n", player_name); | ||
|
||
return 0; | ||
} |