fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. // Define the structure for IndexValue
  5. typedef struct {
  6. int index;
  7. float value;
  8. } IndexValue;
  9.  
  10. // Define the structure for M_PC0 (assuming it's a 3D point cloud)
  11. typedef struct {
  12. float* X;
  13. float* Y;
  14. float* Z;
  15. } PointCloud;
  16.  
  17. // Comparison function for qsort
  18. int compare(const void* a, const void* b) {
  19. IndexValue* idx_a = (IndexValue*) a;
  20. IndexValue* idx_b = (IndexValue*) b;
  21. return (idx_a->value > idx_b->value) ? 1 : -1;
  22. }
  23.  
  24. int main() {
  25. int n = 10; // Number of points in the point cloud
  26.  
  27. // Initialize M_PC0
  28. PointCloud* M_PC0 = (PointCloud*) malloc(sizeof(PointCloud));
  29. M_PC0->X = (float*) malloc(n * sizeof(float));
  30. M_PC0->Y = (float*) malloc(n * sizeof(float));
  31. M_PC0->Z = (float*) malloc(n * sizeof(float));
  32.  
  33. // Initialize some sample data for M_PC0
  34. for (int i = 0; i < n; i++) {
  35. M_PC0->X[i] = (9-i) * 1.0;
  36. M_PC0->Y[i] = (9-i) * 2.0;
  37. M_PC0->Z[i] = (9-i) * 3.0;
  38. }
  39.  
  40. // Perform the code snippet you provided
  41. {
  42. IndexValue* indices = (IndexValue*)malloc(n * sizeof(IndexValue));
  43.  
  44. for (int i = 0; i < n; i++)
  45. {
  46. indices[i].index = i;
  47. indices[i].value = M_PC0->X[i];
  48. }
  49.  
  50. // Sort the indices array based on the values
  51. qsort(indices, n, sizeof(IndexValue), compare);
  52.  
  53. {
  54. float* sortedY = (float*)malloc(n * sizeof(float));
  55. float* sortedZ = (float*)malloc(n * sizeof(float));
  56. float* sortedX = (float*)malloc(n * sizeof(float));
  57.  
  58. for (int i = 0; i < n; i++) {
  59. sortedY[i] = M_PC0->Y[indices[i].index];
  60. sortedZ[i] = M_PC0->Z[indices[i].index];
  61. sortedX[i] = M_PC0->X[indices[i].index];
  62. }
  63.  
  64. // Copy the sorted y and z buffers back to the original arrays
  65. for (int i = 0; i < n; i++) {
  66. M_PC0->Y[i] = sortedY[i];
  67. M_PC0->Z[i] = sortedZ[i];
  68. M_PC0->X[i] = sortedX[i];
  69. }
  70.  
  71. free(sortedY);
  72. free(sortedZ);
  73. free(sortedX);
  74. }
  75.  
  76. free(indices);
  77. }
  78.  
  79. // Print the sorted point cloud
  80. printf("Sorted Point Cloud:\n");
  81. for (int i = 0; i < n; i++) {
  82. printf("X: %f, Y: %f, Z: %f\n", M_PC0->X[i], M_PC0->Y[i], M_PC0->Z[i]);
  83. }
  84.  
  85. // Free memory
  86. free(M_PC0->X);
  87. free(M_PC0->Y);
  88. free(M_PC0->Z);
  89. free(M_PC0);
  90.  
  91. return 0;
  92. }
  93.  
Success #stdin #stdout 0s 5288KB
stdin
10
aba
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
geeksforgeeks
stdout
Sorted Point Cloud:
X: 0.000000, Y: 0.000000, Z: 0.000000
X: 1.000000, Y: 2.000000, Z: 3.000000
X: 2.000000, Y: 4.000000, Z: 6.000000
X: 3.000000, Y: 6.000000, Z: 9.000000
X: 4.000000, Y: 8.000000, Z: 12.000000
X: 5.000000, Y: 10.000000, Z: 15.000000
X: 6.000000, Y: 12.000000, Z: 18.000000
X: 7.000000, Y: 14.000000, Z: 21.000000
X: 8.000000, Y: 16.000000, Z: 24.000000
X: 9.000000, Y: 18.000000, Z: 27.000000