Advertisement
KingAesthetic

C Example 1

Aug 10th, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int main() {
  6.     // function that prints a random number from 1 - 50.
  7.     void printRandomNumber() {
  8.     srand(time(NULL));
  9.     int randomNum = rand() % 50 + 1;
  10.     printf("Random number: %d\n", randomNum);
  11. }
  12.  
  13. // function that calculates the area of a rectangle.
  14. float calculateRectangleArea(float length, float width) {
  15.     return length * width;
  16. }
  17.  
  18. // function that applies a speed boost.
  19. void applySpeedBoost(float *speed) {
  20.     // Increase speed by 10%
  21.     *speed *= 1.1;
  22. }
  23.  
  24. // function that simulates health regeneration.
  25. void regenerateHealth(int *health, int maxHealth) {
  26.     // Add 5 health points (up to maxHealth)
  27.     *health = (*health + 5 <= maxHealth) ? *health + 5 : maxHealth;
  28. }
  29.  
  30. // function that calculates experience points.
  31. int calculateExperience(int level) {
  32.     // Simple formula (you can customize this)
  33.     return level * 100;
  34. }
  35.  
  36. // function that adds an item to a players inventory.
  37. #define MAX_INVENTORY_SIZE 10
  38.  
  39. typedef struct {
  40.     char name[50];
  41.     int quantity;
  42. } Item;
  43.  
  44. void addItemToInventory(Item inventory[], const char *itemName, int quantity) {
  45.     // Check if inventory is full.
  46. }
  47.  
  48. // function that tracks quests completed by players.
  49. #define MAX_INVENTORY_SIZE 10
  50.  
  51. typedef struct {
  52.     char name[50];
  53.     int quantity;
  54. } Item;
  55.  
  56. void addItemToInventory(Item inventory[], const char *itemName, int quantity) {
  57.     // Check if inventory is full
  58. }
  59.  
  60. // function that simulates loot drops.
  61. typedef struct {
  62.     char name[50];
  63.     float dropChance; // chance of (0.0 to 1.0)
  64. } LootItem;
  65.  
  66. LootItem lootTable[] = {
  67.     {"Health Potion", 0.3},
  68.     {"Gold Coin", 0.6},
  69.     {"Rare Sword", 0.1}
  70. };
  71.  
  72. void simulateLootDrop() {
  73.     srand(time(NULL));
  74.     float randomNum = (float)rand() / RAND_MAX; // between 0 and 1
  75.  
  76.     for (int i = 0; i < sizeof(lootTable) / sizeof(lootTable[0]); ++i) {
  77.         if (randomNum < lootTable[i].dropChance) {
  78.             printf("Loot dropped: %s\n", lootTable[i].name);
  79.             break;
  80.         }
  81.     }
  82. }
  83.  
  84. return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement