fork download
  1. #include <stdio.h>
  2.  
  3. void set_smallest_to_zero(double a[], size_t n) {
  4.  
  5. if ( !a || !n ) {
  6. return;
  7. }
  8.  
  9. double smallest = a[0];
  10. size_t min_index = 0,
  11. max_index = 0,
  12. i;
  13.  
  14. // find the smallest values
  15. for ( i = 1; i < n; ++i ) {
  16. if ( a[i] > smallest ) {
  17. continue;
  18. } else if ( a[i] < smallest ) {
  19. smallest = a[i];
  20. min_index = i;
  21. max_index = i;
  22. } else { // a[i] == smallest
  23. max_index = i;
  24. }
  25. }
  26.  
  27. // set to zero all the the minimum values
  28. for ( i = min_index; i <= max_index; ++i ) {
  29. if ( a[i] == smallest ) {
  30. a[i] = 0.0;
  31. }
  32. }
  33.  
  34. }
  35.  
  36. int main(void) {
  37. double v[] = {2.0, 3.0, 1.2, 4.0, 0.7, 3.4, 0.7, 5.6, 1.2 };
  38. size_t l = (sizeof (v ))/ (sizeof (double));
  39. size_t i;
  40. set_smallest_to_zero(v, l);
  41. for ( i = 0; i < l; ++i ) {
  42. printf ("%lf\n", v[i]);
  43. }
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
2.000000
3.000000
1.200000
4.000000
0.000000
3.400000
0.000000
5.600000
1.200000