fork download
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. # include<stdio.h>
  76.  
  77. void knapsack(int n, float weight[], float profit[], float capacity) {
  78. float x[20], tp = 0;
  79. int i, j, u;
  80. u = capacity;
  81.  
  82. for (i = 0; i < n; i++)
  83. x[i] = 0.0;
  84.  
  85. for (i = 0; i < n; i++) {
  86. if (weight[i] > u)
  87. break;
  88. else {
  89. x[i] = 1.0;
  90. tp = tp + profit[i];
  91. u = u - weight[i];
  92. }
  93. }
  94.  
  95. if (i < n)
  96. x[i] = u / weight[i];
  97.  
  98. tp = tp + (x[i] * profit[i]);
  99.  
  100. printf("\nThe result vector is:- ");
  101. for (i = 0; i < n; i++)
  102. printf("%f\t", x[i]);
  103.  
  104. printf("\nMaximum profit is:- %f", tp);
  105.  
  106. }
  107.  
  108. int main() {
  109. float weight[20], profit[20], capacity;
  110. int num, i, j;
  111. float ratio[20], temp;
  112.  
  113. printf("\nEnter the no. of objects:- ");
  114. scanf("%d", &num);
  115.  
  116. printf("\nEnter the wts and profits of each object:- ");
  117. for (i = 0; i < num; i++) {
  118. scanf("%f %f", &weight[i], &profit[i]);
  119. }
  120.  
  121. printf("\nEnter the capacityacity of knapsack:- ");
  122. scanf("%f", &capacity);
  123.  
  124. for (i = 0; i < num; i++) {
  125. ratio[i] = profit[i] / weight[i];
  126. }
  127.  
  128. for (i = 0; i < num; i++) {
  129. for (j = i + 1; j < num; j++) {
  130. if (ratio[i] < ratio[j]) {
  131. temp = ratio[j];
  132. ratio[j] = ratio[i];
  133. ratio[i] = temp;
  134.  
  135. temp = weight[j];
  136. weight[j] = weight[i];
  137. weight[i] = temp;
  138.  
  139. temp = profit[j];
  140. profit[j] = profit[i];
  141. profit[i] = temp;
  142. }
  143. }
  144. }
  145.  
  146. knapsack(num, weight, profit, capacity);
  147. return(0);
  148. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:1: error: expected identifier or '(' before numeric constant
 1
 ^
In file included from /usr/include/stdio.h:74:0,
                 from prog.c:75:
/usr/include/libio.h:306:3: error: unknown type name 'size_t'
   size_t __pad5;
   ^
/usr/include/libio.h:310:67: error: 'size_t' undeclared here (not in a function)
   char _unused2[15 * sizeof (int) - 4 * sizeof (void *) - sizeof (size_t)];
                                                                   ^
/usr/include/libio.h:338:62: error: expected declaration specifiers or '...' before 'size_t'
 typedef __ssize_t __io_read_fn (void *__cookie, char *__buf, size_t __nbytes);
                                                              ^
/usr/include/libio.h:347:6: error: expected declaration specifiers or '...' before 'size_t'
      size_t __n);
      ^
/usr/include/libio.h:469:19: error: expected '=', ',', ';', 'asm' or '__attribute__' before '_IO_sgetn'
 extern _IO_size_t _IO_sgetn (_IO_FILE *, void *, _IO_size_t);
                   ^
In file included from prog.c:75:0:
/usr/include/stdio.h:337:20: error: expected declaration specifiers or '...' before 'size_t'
       int __modes, size_t __n) __THROW;
                    ^
/usr/include/stdio.h:386:44: error: expected declaration specifiers or '...' before 'size_t'
 extern int snprintf (char *__restrict __s, size_t __maxlen,
                                            ^
/usr/include/stdio.h:390:45: error: expected declaration specifiers or '...' before 'size_t'
 extern int vsnprintf (char *__restrict __s, size_t __maxlen,
                                             ^
/usr/include/stdio.h:709:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'fread'
 extern size_t fread (void *__restrict __ptr, size_t __size,
               ^
/usr/include/stdio.h:715:15: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'fwrite'
 extern size_t fwrite (const void *__restrict __ptr, size_t __size,
               ^
prog.c: In function 'knapsack':
prog.c:79:11: warning: unused variable 'j' [-Wunused-variable]
    int i, j, u;
           ^
stdout
Standard output is empty