How to Merge and Sort Two Fortran Arrays?

In summary: Again, you need to show us (in code) what you have tried, not explain to us what you have tried. As already noted, please start a new thread in the Homework & Coursework section.
  • #1
hobmarcus
10
0
I have 2 set of data (from different files) and I allocate each set of data to array in my main program. But now I want to merge those 2 array and sort them in ascending orders in my sub program. What should I do? I know how to sort array in ascending orders, but i don't know how to merge those 2 array together... Any suggestion or help?
 
Technology news on Phys.org
  • #2
hobmarcus said:
I have 2 set of data (from different files) and I allocate each set of data to array in my main program. But now I want to merge those 2 array and sort them in ascending orders in my sub program. What should I do? I know how to sort array in ascending orders, but i don't know how to merge those 2 array together... Any suggestion or help?
pseudo code:
create new array big enough to hold all the data
create a loop that puts all of the first array's elements into the new array
create a loop that puts all of the second array's elements into the new array (starting at the index left at the end of the first loop)
 
  • #3
phinds said:
pseudo code:
create new array big enough to hold all the data
create a loop that puts all of the first array's elements into the new array
create a loop that puts all of the second array's elements into the new array (starting at the index left at the end of the first loop)
thanks you for answering my question. It helps a lot. However, Another subroutine is required to find the intersection of them and combined it into 1 array. Any suggestion or help please
 
Last edited:
  • #4
hobmarcus said:
thanks you for answering my question. It helps a lot. However, Another subroutine is required to find the intersection of them and combined it into 1 array. Any suggestion or help please
You need to show us what you have tried. We are not going to write this code for you.

Your question should be posted in the Engineering & Comp. Sci. subsection under Homework & Coursework. I am closing this thread.
 
Last edited:
  • #5
Mark44 said:
You need to show us what you have tried. We are not going to write this code for you.
First, I tried to use do loop to compare each value in both array and the write out each intersect value. But I do not know how to put them together into an array since the size of this intersect array varies if the data change.
 
  • #6
hobmarcus said:
First, I tried to use do loop to compare each value in both array and the write out each intersect value. But I do not know how to put them together into an array since the size of this intersect array varies if the data change.

Again, you need to show us (in code) what you have tried, not explain to us what you have tried. As already noted, please start a new thread in the Homework & Coursework section.
 

1. How can I merge two arrays in Fortran?

In Fortran, you can use the MERGE function to combine two arrays into one. The syntax for this function is MERGE(array1, array2, mask), where array1 and array2 are the arrays you want to merge and mask is an array of logical values that determines which elements from array1 and array2 are included in the merged array.

2. Can I merge arrays of different sizes in Fortran?

Yes, you can merge arrays of different sizes in Fortran as long as they have the same shape. If the arrays have different shapes, you can use the RESHAPE function to reshape them to the same size before merging.

3. How do I merge arrays in Fortran without creating a new array?

To merge arrays in Fortran without creating a new array, you can use the ASSIGN statement. This statement allows you to assign one array to another, overwriting the existing values in the target array. However, keep in mind that this method can be inefficient and may lead to unexpected results if the target array is not large enough to hold all the elements from the merged arrays.

4. Can I merge arrays with different data types in Fortran?

No, Fortran does not allow merging arrays with different data types. The arrays you want to merge must have the same data type, such as INTEGER, REAL, or CHARACTER. If you need to merge arrays with different data types, you can convert them to the same data type before merging using the TRANSFER function.

5. Is there a limit to the size of arrays that can be merged in Fortran?

There is no specific limit to the size of arrays that can be merged in Fortran. However, the size of the merged array will depend on the available memory on your system. If the merged array is too large, it may exceed the memory limit and cause a runtime error. It is recommended to allocate enough memory for the merged array before merging to avoid any issues.

Similar threads

  • Programming and Computer Science
Replies
6
Views
817
  • Programming and Computer Science
Replies
1
Views
554
  • Programming and Computer Science
Replies
21
Views
289
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top