array1 and array2 is my ascended ordered arrays
ı want to put this arrays into the array3 and then ı want to write ouput.txt
you can understand better my codes.

``````for( int i=0; i<21 ;++i)

{
if (array1[d].ave> array2[e].ave)

array3[i].ave=array2[e].ave;
e = e+1;

if(array2[e].ave>array1[1].ave)

{
array3[i].ave = array1[d].ave;
d=d+1;
}

}
for ( int m=0;m<20;++m)
{

fprintf(b,"%s%f\n",array3[m].name,array3[m].ave );
}``````
3
Contributors
15
Replies
18
Views
7 Years
Discussion Span
Last Post by Ancient Dragon

I think what you want is this: assumes number elements in array1 is the same as in array2

``````int k = 0;
int n = NumberElementsInArray1;
for(int i = 0; i < n; i++, k++)
{
if( array1[d].ave > array2[e].ave )
{
array3[k] = array1[d];
d++;
}
else if( array2[e].ave > array1[d].ave )
{
array3[k] = array2[e];
e++;
}
else // both are the same value
{
array3[k] = array2[e];
d++;
e++;
}
}
// finish up array2
while( e < NumberElementsInArray1)
array3[k++] = array2[e++];``````

Edited by Ancient Dragon: n/a

yes they have same number

but this formula does not put in order these two arrays

Try to think for yourself a little. I suspect AD was assuming that your arrays were already sorted.

Do you need to sort the individual arrays before you merge them? If so, you should be able to use any standard in-place sorting algorithm. Then, once they are sorted individually, use the merge algorithm to combine them.

I tryingt find but my brain stopped.
my arrays has already sorted yes.
there is little mistake but my brain don't work now

``````int k = 0,d=0,e=0;
int n = 10;
for(int i = 0; (e<n+1 ) ; ++i, ++k)
{
if( array1[d].ave > array2[e].ave )
{
array3[k] = array2[d].ave;
e=e+1;
}
else if( array2[e].ave > array1[d].ave )
{

array3[k] = array1[d].ave;
d=d+1;
}
else // both are the same value
{
array3[k] = array2[e].ave;
d++;
e++;
}
}
// finish up array2
for(int i=0; d<=10 ; ++i,++k,++d)
array3[k+1]=array2[d+1].ave;

for(int i=0 ; i<20 ;++i)
cout<<"\n" <<array3[i]<<"    ";``````

>>array3[k] = array2[e].ave;
Wrong. See the code I posted for the correct line. I assume you have two arrays of structures, so when a copy is needed you have to copy the entire structure, not just the value of one of its members.

how will we do this

how will you do what? Just setting them with = will copy the structure.

okey it worked. but ı could not understand way of your.
For example , for(int i = 0; i < n; i++, k++)
why is it not ++i , ++k
if you have a some time . can you give me a short explanation.

This code do not put two structure arrays into third structure array in the way of ordered

``````int k = 0,d=0,e=0;
int n = 10;
for(int i = 0; i < n; i++, k++)
{
if( array1[d].ave > array2[e].ave )
{
array3[k] = array1[d];
d++;
}
else if( array2[e].ave > array1[d].ave )
{
array3[k] = array2[e];
e++;
}
else // both are the same value
{
array3[k] = array2[e];
d++;
e++;
}
}
// finish up array2
while( e < 10)
array3[k++] = array2[e++];``````

this code puts array1 and array2 into to array3 but not in order.

Yes, there are a couple bugs in that code. For example, lines 23 and 24. You need to do that for both arrays. The other bug is on line 17 -- you have to copy the element from both arrays into the destination array. When array1 and array2 have duplicate values you will get two entries in array3 with the same value.

You might want to check for < on lines 5 and 10 instead of >.

If this is a school assignment I'm not going to post the final version because I'm not supposed to do your homework for you.

Edited by Ancient Dragon: n/a

:)
NO of course ı want to develop myself . if you say where is the mistake , ı can solve him . ı couldd not find and ı could not understand your reason only.

ı want to say something . ı asked a question why you did such. ı want to disscuss and examine with you . you saw my code. if you can talk and tell me about reason . ı can solve easily.

The code you originally posted was pretty close to what you need. I just took what you had posted and expanded on it a little bit.

About that else statement where the value in array1 is the same in array2. If you want duplicates in array3 then copy the value from both array1 and array2 into array3. But if you don't want duplicates then copy from just one of those two arrays. If array3 does not contain duplicates, then the number of elements in array3 will not be the same as the sum of the number of elements in array1+array2, and in that case your program will have to keep track of the number of valid entries in array3.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.