Problem extracting minimum values from an array

hey everyone, i am a novice programmer and i'm trying to create huffman codes. But I'm facing problem with acquiring the two minimum frequencies from an array. I'v converted the part of the code that i'm stuck at into a separate program:



#include<iostream.h>
class bnode{public:
int freq;
};

void main()
{
bnode node[9];
const int chars=5;
int i,k,temp1,temp2,min1,min2;
for(i=0;i<5;i++)
{
cout<<"Enter element no."<<i+1<<endl;
cin>>node[i].freq;
}
for(i=0;i<chars;i++)
{
temp1=node[i].freq;
temp2=node[i].freq;
for(k=i;k<chars+i;k++)
{
{
if(temp1>node[k].freq)
{
temp1=node[k].freq;
min1=k;
}
}
{
if((temp2>node[k].freq)&&(temp2>temp1))
{
temp2=node[k].freq;
min2=k;
}
}
}
node[chars+i].freq=temp1+temp2;
cout<<"these set consists of: ";
for(k=i;k<chars+i;k++)
{
cout<<node[k].freq<<" ";
}
cout<<"minimum element for this is:"<<temp1<<" and its position is: "<<min1+1<<endl;
cout<<"last but minimum element for this set is:"<<temp2<<" and its position is: "<<min2+1<<endl;
}
}





As you know you require a b-tree with 2n-1 nodes for n leaf nodes of the tree. I'v taken n=5 here. I'm combining the frequency of the two minimum values into the next place in the array after the last element that the for loop checks in one run of the loop. I want the loop to move to the next set of 5 elements to acquire the value of the 2 minimum numbers. Please help me! Thank you!
It would be much easier to help you if you would edit your post and use code tags. Moreover, you should beautiful the code in your IDE and copy and paste it so that the indenting sticks after the paste. It is very difficult to read embedded for loops and if..else constructs when the code is not placed within tags and not indented properly.
Topic archived. No new replies allowed.