fork download
  1. Copy code
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5.  
  6. // Function to clear the screen
  7. void clearScreen() {
  8. printf("\033[H\033[J");
  9. }
  10.  
  11. int main() {
  12. int i, j, k;
  13. int n = 100; // number of frames
  14. int delay = 100000; // delay between frames in microseconds
  15. char stars[20][41]; // array to store the positions of stars
  16.  
  17. // Initialize stars array
  18. for (i = 0; i < 20; i++) {
  19. for (j = 0; j < 40; j++) {
  20. stars[i][j] = ' ';
  21. }
  22. }
  23.  
  24. // Generate random positions for stars
  25. for (i = 0; i < 50; i++) {
  26. int x = rand() % 20;
  27. int y = rand() % 40;
  28. stars[x][y] = '*';
  29. }
  30.  
  31. // Animation loop
  32. for (k = 0; k < n; k++) {
  33. clearScreen(); // Clear screen
  34.  
  35. // Print stars
  36. for (i = 0; i < 20; i++) {
  37. for (j = 0; j < 40; j++) {
  38. printf("%c ", stars[i][j]);
  39. }
  40. printf("\n");
  41. }
  42.  
  43. usleep(delay); // Wait before next frame
  44. }
  45.  
  46. return 0;
  47.  
Success #stdin #stdout 0.02s 25992KB
stdin
Standard input is empty
stdout
Copy code
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

// Function to clear the screen
void clearScreen() {
    printf("\033[H\033[J");
}

int main() {
    int i, j, k;
    int n = 100; // number of frames
    int delay = 100000; // delay between frames in microseconds
    char stars[20][41]; // array to store the positions of stars
    
    // Initialize stars array
    for (i = 0; i < 20; i++) {
        for (j = 0; j < 40; j++) {
            stars[i][j] = ' ';
        }
    }
    
    // Generate random positions for stars
    for (i = 0; i < 50; i++) {
        int x = rand() % 20;
        int y = rand() % 40;
        stars[x][y] = '*';
    }
    
    // Animation loop
    for (k = 0; k < n; k++) {
        clearScreen(); // Clear screen
        
        // Print stars
        for (i = 0; i < 20; i++) {
            for (j = 0; j < 40; j++) {
                printf("%c ", stars[i][j]);
            }
            printf("\n");
        }
        
        usleep(delay); // Wait before next frame
    }
    
    return 0;