Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. /**
  2. * @file q11210332221.c
  3. * @brief サンプルプログラム
  4. * @author Keitetsu
  5. * @date 2019/07/12
  6. * @copyright Copyright (c) 2019 Keitetsu
  7. * @par License
  8. * This software is released under the MIT License.
  9. */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <math.h>
  14.  
  15.  
  16. #define N_DATA (720) /**< Number of data */
  17.  
  18.  
  19. size_t set_wave_data(void *array, size_t array_size);
  20.  
  21.  
  22. int main(int argc, char *argv[])
  23. {
  24. double *array;
  25. size_t array_size;
  26. size_t ret_val;
  27. FILE *fp;
  28. size_t t;
  29.  
  30. array_size = sizeof(double) * N_DATA;
  31. array = (double *)malloc(array_size);
  32. if (array == NULL) {
  33. fprintf(stderr, "[ERROR] Failed to allocate memory.\n");
  34. return 1;
  35. }
  36.  
  37. ret_val = set_wave_data(array, array_size);
  38.  
  39. fp = fopen("wave_data.csv", "w");
  40. if (fp == NULL) {
  41. fprintf(stderr, "[ERROR] Failed to open the file.\n");
  42. free(array);
  43. return 2;
  44. }
  45.  
  46. for (t = 0; t < ret_val; t++) {
  47. fprintf(fp, "%zu, %lf\n", t, array[t]);
  48. }
  49.  
  50. fclose(fp);
  51.  
  52. free(array);
  53.  
  54. return 0;
  55. }
  56.  
  57.  
  58. size_t set_wave_data(void *array, size_t array_size)
  59. {
  60. size_t n_data;
  61. size_t t;
  62.  
  63. n_data = array_size / sizeof(double);
  64.  
  65. for (t = 0; t < n_data; t++) {
  66. ((double *)array)[t] = sin(t * M_PI / 180.0);
  67. }
  68.  
  69. return t;
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement