diff --git a/C++/Animals.cxx b/C++/Animals.cxx new file mode 100644 index 0000000..55aa969 --- /dev/null +++ b/C++/Animals.cxx @@ -0,0 +1,59 @@ +#include +#include +#include +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 +void addAnimal(vector>& zoo) { + zoo.push_back(make_unique()); +} + +int main() { + vector> zoo; + + cout << "Welcome to the Animal Zoo Simulator 🦁🐘!\n"; + + // Using templates to add different animals + addAnimal(zoo); + addAnimal(zoo); + addAnimal(zoo); + + cout << "\nThe animals in the zoo are making sounds...\n"; + for (const auto& animal : zoo) { + animal->makeSound(); + } + + return 0; +} diff --git a/C++/Mandelbrot.cxx b/C++/Mandelbrot.cxx new file mode 100644 index 0000000..7e584c5 --- /dev/null +++ b/C++/Mandelbrot.cxx @@ -0,0 +1,67 @@ +#include +#include +#include +#include +#include + +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 c) { + std::complex 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& image) { + for (int y = start_row; y < end_row; ++y) { + for (int x = 0; x < WIDTH; ++x) { + std::complex 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 image(WIDTH * HEIGHT); + int num_threads = std::thread::hardware_concurrency(); // Use all available cores + std::vector 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; +} diff --git a/C/GuessTheNumber.c b/C/GuessTheNumber.c new file mode 100644 index 0000000..844210c --- /dev/null +++ b/C/GuessTheNumber.c @@ -0,0 +1,55 @@ +#include +#include +#include + +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; +}