fork(1) download
  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <err.h>
  6. #include <string.h>
  7.  
  8.  
  9. int remove_C(int C_num, int list_size, int *list, double *c_old, double *c_new) {
  10. int i;
  11.  
  12. // Copy the elements before the node i (removed element)
  13. switch(list[0]){
  14. case 0:
  15. memcpy(c_new, c_old + 3, list[1] * sizeof(double) * 3);
  16. break;
  17. default:
  18. memcpy(c_new, c_old, list[0] * sizeof(double) * 3);
  19. break;
  20. }
  21. // Copy the elements before the removed element i
  22. for(i = 1; i < list_size; i++)
  23. memcpy(c_new + (list[i-1]-i+1) * 3, c_old + (list[i] - 1) * 3, (list[i] - list[i-1] - 1) * sizeof(double) * 3);
  24.  
  25. // Copy the elements before (after) the removed element i
  26. if(list[list_size] == list_size + C_num)
  27. memcpy(c_new + (list[list_size - 1] - list_size) * 3, c_old + list[list_size - 1] * 3, (list[list_size - 1] - list[list_size - 2] -1) * sizeof(double) * 3);
  28. else
  29. memcpy(c_new + (list[list_size - 1] - list_size + 1) * 3, c_old + (list[list_size - 1] + 1) * 3, (C_num + list_size - list[list_size - 1] - 1) * sizeof(double) * 3);
  30.  
  31.  
  32. return c_new[0];
  33. }
  34.  
  35. int main(void) {
  36. int C_num = 0;
  37. int list_size = 0;
  38. int i, j;
  39. int *list;
  40. double *c_old, *c_new;
  41. c_old = malloc(sizeof(double) * C_num);
  42. c_new = malloc(sizeof(double) * C_num);
  43. list = malloc(sizeof(int) * list_size);
  44.  
  45. list_size++;
  46. list = realloc(list, sizeof(int) * list_size);
  47. list[list_size -1] = 1;
  48.  
  49. list_size++;
  50. list = realloc(list, sizeof(int) * list_size);
  51. list[list_size -1] = 4;
  52.  
  53. list_size++;
  54. list = realloc(list, sizeof(int) * list_size);
  55. list[list_size -1] = 6;
  56.  
  57. list_size++;
  58. list = realloc(list, sizeof(int) * list_size);
  59. list[list_size - 1] = 8;
  60.  
  61. C_num += 10;
  62. c_old = realloc(c_old, sizeof(double) * C_num * 3);
  63.  
  64. for(i = 0; i < C_num; i++)
  65. for(j = 0; j < 3; j++)
  66. c_old[i * 3 + j] = i * 3 + j;
  67.  
  68.  
  69. for(i = 0; i < C_num; i++){
  70. for(j = 0; j < 3; j++)
  71. printf("%f ", c_old[i * 3 + j]);
  72. printf("\n");
  73. }
  74. printf("\n");
  75.  
  76. C_num -= list_size;
  77. c_new = realloc(c_new, sizeof(double) * C_num * 3);
  78.  
  79. c_new[0] = remove_C(C_num, list_size, list, c_old, c_new);
  80.  
  81. for(i = 0; i < C_num; i++){
  82. for(j = 0; j < 3; j++)
  83. printf("%f ", c_new[i * 3 + j]);
  84. printf("\n");
  85. }
  86. printf("\n");
  87. free(c_old);
  88. free(c_new);
  89. }
  90.  
Success #stdin #stdout 0s 9416KB
stdin
Standard input is empty
stdout
0.000000 1.000000 2.000000 
3.000000 4.000000 5.000000 
6.000000 7.000000 8.000000 
9.000000 10.000000 11.000000 
12.000000 13.000000 14.000000 
15.000000 16.000000 17.000000 
18.000000 19.000000 20.000000 
21.000000 22.000000 23.000000 
24.000000 25.000000 26.000000 
27.000000 28.000000 29.000000 

0.000000 1.000000 2.000000 
9.000000 10.000000 11.000000 
12.000000 13.000000 14.000000 
15.000000 16.000000 17.000000 
21.000000 22.000000 23.000000 
27.000000 28.000000 29.000000