for and if condition giving unexpected result

i have a problem in c++ like this:
I have a problem in the code below. The problem is this that i doing some addition part after both `if conditions` break.And that addiion is repeatative while loop until `count>0` The problem is if i put braces in for loop then it repeats the the part inside the braces until it's condition is not false. Buti have to do addition which is like this:
We have an array data[i].freq={0,1,2,3,4,5} and data[i].next represents the next member to be added.Suppose i add 0 and 1 first i got"1" as a result and i put the result in last index of my array , like this ({0 1 2 3 4 5 1})now 0 and 1 cann not be added because they are already added(i don't have to repeat the addition on same elements), so next time the addition will be between the last index element and smallest element before the last element (but not those elements who are already added). so here the addition will be between the "1" in last index+ smallest element in right to it which is "2" **Note here we have not taken in acount 0 and 1 because they are already added, the same way this 2 will not be added next time because it is being added this time** and their addition wil be `{0 1 2 3 4 5 1 3}` we have to repeat the same until there left & element at last.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
    data[data_size].freq=data[i].freq+data[p].freq; // here i add the first 2 elements "0" and "1" in the example i given below.
    data[data_size].flag=0;  //I am using flag variable to show which elements are added or which are not added even once. If flag ="1" then that element is added if it "0" then not added even once.
    data[i].flag=1;
    data[p].flag=1;  //these two have been added.
    int count=5;
    do
     {
      for(i=0;data[i].next!=-1;i=data[i].next)//the problem is here if i put bracesit dont't do the ask which i expect it to do.
      { 
        if(data[data[i].next].freq>data[data_size].freq && data[data[i].next].flag==0)//Here i am setting flag=0 for those elements who not have been added yet. Because we don't have to take in account for addition those elements who are already added once.
          data[data_size+1].freq= data[data_size].freq+ data[data[i].next].freq; 
         data[data_size].flag=1;//those elements which we are adding we set their flag to 1
         data[data[i].next].flag=1;
         data[data_size+1].flag=0;//this is the element onbtained on result will be sent to last index.With 0 flag because it is not added yet.
         data[data_size].next=data[i].next;
         data[i].next=data_size;
         data_size++;
       if(data[data[i].next].freq<data[data_size].freq && data[data[i].next].flag==0)
         data[data_size+1].freq= data[data_size].freq + data[i].freq
         data[data_size].next=data[i].next;
         data[i].next=data_size;
         data_size++;
        
      } 
      count--;
    } while(count>0)

**could any one please help me in desiging the code for what i want to achieve.**
Last edited on
Let me try to think:
1
2
3
4
5
6
7
8
9
10
11
12
Input freq
0,1,4,2,5,3
add two first (0+1)
4,2,5,3,1
add min and last (2+1)
4,5,3,3
add min and last (3+3)
4,5,6
add min and last (4+6)
5,10
add min and last (5+10)
15
Output freq


0,1,4,2,5,3,1

0,1,4,2,5,3,1,3

0,1,4,2,5,3,1,3,6

0,1,4,2,5,3,1,3,6,10

0,1,4,2,5,3,1,3,6,10,15

Was that what should happen?
Why is the first addition different from the rest?
@keskiverto

You written idea is Ok except that when we you added 3+3 =6 just after that you can see that there are 2 elements smaller then 6, which are 4and 5 in this case we will get 4+5 =9 and then 9 +6=15 (we should get the result exactly as we get in sorting but without doing sorting. we have to go to the index of the elements and then do the addition there by comparing if smaller or bigger).Actually it has very less complexity.The "next" of my code denotes the next element of choice at "i" index in my code.

It's not necessary that min and last will be added ..they could be any two minimum for addition and this has to be achieved through a queue(arrays only). please do not hesitate to ask more if you don't understand yet my question?
and you can sort Input freq 0,1,4,2,5,3 once at starting so that we can get.
0,1,2,3,4,5. (you will not need to do like 0,1,4,2,5,3)
But no sorting after that. My code is trying to implement that but my code works fine when there is only 1 if condition but it is not able to work for 3 if conditions here(because i have to break each time find the index of the desired small elements and then add).
There will be different conditions:

(1) If last element is equal to the element found in right .
you second addition holds that where 3+3 is 6.
(2) It is smaller then element in right
where see your very first addition holds this condition. "1" at last is smaller then "2" found in right to it.

Last edited on
It's not necessary that min and last will be added ..they could be any two minimum for addition

Why do you talk about this or that index, when you really want to take the two smallest values?
1
2
3
4
5
6
7
8
min_priority_queue q;
for( n in data )
  q.push(n);

while( some_condition )
  int a = q.pop();
  int b = q.pop();
  q.push( a+b );
You've got an array, at each step you take the 2 lesser values, remove them and add its sum.
¿is that what you want to do?
Last edited on
Topic archived. No new replies allowed.