I have a problem in c, i want to achieve this kind of thing using the code i written below. The input and output should be like this:

``````      Input Freq                                  Output Freq
1     0,1,4,2,5,3 add two first (0+1)             0,1,4,2,5,3,1
2     4,2,5,3,1   add min and last (2+1)          0,1,4,2,5,3,1,3
3     4,5,3,3     add min and last (3+3)          0,1,4,2,5,3,1,3,6
4     4,5,6      **here we add(4+5)**(minimum two)0,1,4,2,5,3,1,3,6,9
5     9,6         minimum two                     0,1,4,2,5,3,1,3,6,9,15
6     15
``````

But condition is that there must be no swaping of elements, no sorting, **But we can deal with the index of element for comparison and once if we found the correct element at any index we add them and put at last of the array.

I am trying some basic idea It is working correctly for the first if condition but what to write in the other two if conditions that i want to know.Please help me there.
suppose data[i].freq={0,1,2,3,4,5} and data[i].next points to the next element like in the example above in first step 0 points to 1 and this 1 now points to the element obtained by these two(so this 1 points to the 1 at alst and this 1 at last index will point to the next of the "1" (which we used in addition)so next of that "1" is 4 , so last element "1" points to 4 and the same way we maintain the index pointing). Please do not hesitate me to ask if you have not understood what i mean to say.
code i guess should be very close to this :

``````    data[data_size].freq=data[0].freq+data[1].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[0].flag=1;
data[1].flag=1;  //these two have been added.
int count=5;
do
{
for(i=0;data[i].next!=-1;i=data[i].next)
{
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.(step1 and step2 are coming in this loop)
{
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)
{
//some code for step4 where 6>5 (in this case we added 5+4)
data_size++;
}
if(data[data[i].next].freq==data[data_size].freq && data[data[i].next].flag==0)
{
//Some code for step3 when element are equal
data_size++;
}
}
count--;
} while(count>0)
``````

There will be different conditions like (last elements= element in right found eg. 3+3=6 in step2) and elements found is saller then last element like 5+4=9 (see step 4)

Any idea what to right in other two if conditions? My array input must be `{0,1,4,2,5,3}`(i mean data[i].freq) and output array must be {0,1,4,2,5,3,1,3,6,9,15} (data[data_size].freq), with out any sorting and without any swapping, just using index movement , only using arrays.
Please help me in writting another two if conditions.

I explained my question very properly and in explained manner. But still no replies :P ... Very strange

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.