### Greedy implementation not working as intended

Problem:
For a given maximum weight and a given number of objects,each with their own value and weight,display the object number and percentage inserted in the bag,for the combination with the highest value
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869`` ``````#include #include #pragma warning (disable : 4996) using namespace std; class Greedy { int o[100],N,M; float val[100],weight[100],x[100],Gr; public: void imput() { int i; cout<<"Imput maximum weight and number of objects"<>M>>N; for (i=0;i>val[i]>>weight[i]; } } void order() { int i,aux,check; do {check=0; for(i=0;iweight[o[i]]) x[o[i]]=weight[o[i]]/(Gr-weight[o[i]]); Gr+=weight[o[i]]; if(Gr==M) x[o[i]]=0; } } void output() { int i; for(i=0;i

When i run the code and insert the values,the output is very strange,and im not sure what to make of it.Ex:
For max weight 10 and 4 objects,defined with

values 3 4 4 3
weight 6 6 7 9

I get the following output:
 ```1:23.0769% 2:58.3333% 3:100% 4:23.6842%```

What I want to get:
 ```1:0% 2:100% 3:57.14% 4:0%```

There are some values for which i get the correct output but most of them are wrong.
It would be a lot easier to spot if you learn to indent your code.
In fact, you probably shouldn't bother, simple tell your IDE to do it.

lines 43 and 46 are executed always.
¿may you explain the conditions in line 41 and 44?
Changed a few things in the code and added a few comments:
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576`` ``````#include #include #pragma warning (disable : 4996) using namespace std; class Greedy { int o[100],N,M; float val[100],weight[100],x[100],Gr; public: void imput() { int i; cout<<"Imput maximum weight and number of objects"<>M>>N; for (i=0;i>val[i]>>weight[i]; } } void order() { int i,aux,check; do {check=0; for(i=0;iM)// If the weight I have in the bag(GR)+the weight of the next object[weight[o[i]] is higher than the max weight(M) { //the else gives x the value 1 and increases the weight in the bag(Gr) if(Gr

The output im getting now is closer to what i want but still not there:
This is for the same imput I listed in the first post.
 ``` 1:-1.07374e+010% 2:100% 3:57.1429% 4:-1.07374e+010%```

1: and 4: should be 0
Also for imput max weight 10 and 2 objects ,both objects defined with value 5 and weight 5 i get
 `1:100%`

I should get:
 ``` 1:100% 2:100%```
Last edited on
 indentation indentation is optional whitespace. see also: !vowel vowel vwls r bt s sfl s whtspc, spclly n vrbl nms. s ls: !ndnttn

> Also for imput max weight 10 and 2 objects ,both objects defined with value 5 and weight 5 i get
execute step by step through a debugger.
observe line 55.
Topic archived. No new replies allowed.