Advertisement
Guest User

C incoming data

a guest
Apr 5th, 2025
86
0
112 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.45 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define MAX_DATA 100  // Maximum number of stored values (can be dynamic if needed)
  6.  
  7. // Structure to store a value and its timestamp
  8. typedef struct {
  9.     int value;
  10.     time_t timestamp;
  11. } Data;
  12.  
  13. // Global buffer to store data and the index of the next position in the buffer
  14. Data data_buffer[MAX_DATA];
  15. int buffer_index = 0;
  16. int data_count = 0;
  17.  
  18. // Function to calculate sum of values in the last 10 seconds
  19. int calculate_sum() {
  20.     time_t current_time = time(NULL);
  21.     int sum = 0;
  22.    
  23.     // Iterate over the data buffer and sum values within the last 10 seconds
  24.     for (int i = 0; i < data_count; i++) {
  25.         if (difftime(current_time, data_buffer[i].timestamp) <= 10) {
  26.             sum += data_buffer[i].value;
  27.         }
  28.     }
  29.    
  30.     return sum;
  31. }
  32.  
  33. // Function to add new data to the buffer
  34. void add_data(int value) {
  35.     time_t current_time = time(NULL);
  36.    
  37.     // Add new data to the buffer
  38.     data_buffer[buffer_index].value = value;
  39.     data_buffer[buffer_index].timestamp = current_time;
  40.    
  41.     // Update buffer index and count
  42.     buffer_index = (buffer_index + 1) % MAX_DATA;
  43.     if (data_count < MAX_DATA) {
  44.         data_count++;
  45.     }
  46. }
  47.  
  48. // Function to discard old data and update the buffer
  49. void update_data() {
  50.     time_t current_time = time(NULL);
  51.    
  52.     // Remove data older than 10 seconds
  53.     int i = 0;
  54.     while (i < data_count) {
  55.         if (difftime(current_time, data_buffer[i].timestamp) > 10) {
  56.             // Shift elements to remove the old data
  57.             for (int j = i; j < data_count - 1; j++) {
  58.                 data_buffer[j] = data_buffer[j + 1];
  59.             }
  60.             data_count--;
  61.         } else {
  62.             i++;
  63.         }
  64.     }
  65. }
  66.  
  67. int main() {
  68.     // Seed the random number generator
  69.     srand(time(NULL));
  70.    
  71.     // Simulate incoming data with random intervals and values
  72.     while (1) {
  73.         // Simulate random wait time between 1 to 3 seconds
  74.         int wait_time = rand() % 3 + 1;
  75.         sleep(wait_time);
  76.        
  77.         // Simulate random data value between 1 and 10
  78.         int value = rand() % 10 + 1;
  79.         printf("Received value: %d\n", value);
  80.        
  81.         // Update data and calculate sum
  82.         add_data(value);
  83.         update_data();
  84.        
  85.         int sum = calculate_sum();
  86.         printf("Sum of values in the last 10 seconds: %d\n", sum);
  87.     }
  88.    
  89.     return 0;
  90. }
Tags: C++
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement