Follow us on Twitter!
Things are more like they are now than they have ever been before. - Dwight D. Eisenhower
Thursday, April 24, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 31
Guests Online: 26
Members Online: 5

Registered Members: 82903
Newest Member: Piriformis
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 26-10-08 22:19
*Don't think you will have any problems helping me if you can programming even if you don't know fortran*

Well I have a question that I'm having a bit of trouble. It works for some situations but doesn't for others. What the program does is takes two lists of numbers then calls a subroutine that sorts them in ascending order (lowest to highest) then calls another subroutine which merges them and puts them in ascending order. One condition is that you can't call the sorting subroutine in your merging subroutine to sort them. So you have to merge and sort at the same time is how I understand it.

The program has to work for these sets of lists:
(a) List 1: 5, 3, 7, 1, 9
List 2: 2, 4, 6, 8, 10
(b) List 1: 10, 9, 6, 5, 4, 1
List 2: 3, 7, 2, 8
(c) List 1: 1, 2, 3, 7, 6, 4, 5
List 2: 9, 8, 10
(d) List 1: 10
List 2: 1, 9, 2, 8, 3, 7, 4, 6, 5

The problem seems to be with the merging of the arrays and not with the sorting because the sorting gives the correct output.

The merging works for (a) and (d) but doesn't work for (b) and (c).

When (b)'s lists are used here is the output:
Code
How many items are in list one? 6
Enter list: 10 9 6 5 4 1

How many items are in list two? 4
Enter list: 3 7 2 8

Sorted list one:
  1,  4,  5,  6,  9, 10
Sorted list two:
  2,  3,  7,  8
Merged List:
  1,  2,  3,  4,  5,  6,  7,  8,  9,***





When (c)'s lists are used here is the output:
Code
How many items are in list one? 7
Enter list: 1 2 3 7 6 4 5

How many items are in list two? 3
Enter list: 9 8 10

Sorted list one:
  1,  2,  3,  4,  5,  6,  7
Sorted list two:
  8,  9, 10
Merged List:
  1,  2,  3,  4,  5,  6,  7,  8,  0,  9





Here is my code for my subroutine

Code
SUBROUTINE merge(list1, list2, num1, num2, merged_list)
!--------------------------------------------------------------------
! This subroutine merges two sorted lists together in ascending order
!--------------------------------------------------------------------
    INTEGER,INTENT(IN)::list1(:), list2(:), num1, num2
    INTEGER,INTENT(OUT)::merged_list(40)
    INTEGER:: i, j, k, l
    i = 1
    j = 1
    k = 1
    DO l=1, num1+num2
   IF (list1(i) < list2(j)) THEN
       IF (i>num1) THEN
      merged_list(k) = list2(j)
      j = j+1
      k = k+1
       ELSE IF (j>num2) THEN
      merged_list(k) = list1(i)
      i = i+1
      k = k+1
       ELSE IF (i>num1 .AND. j>num2) THEN
      EXIT
       END IF
       merged_list(k) = list1(i)
       i = i+1
       k = k+1
   ELSE IF (list1(i) > list2(j)) THEN
       IF (i>num1) THEN
      merged_list(k) = list2(j)
      j = j+1
      k = k+1
       ELSE IF (j>num2) THEN
      merged_list(k) = list1(i)
      i = i+1
      k = k+1
       ELSE IF (i>num1 .AND. j>num2) THEN
      EXIT
       END IF
       merged_list(k) = list2(j)
       j = j+1
       k = k+1
   END IF
    END DO
END SUBROUTINE merge





Can someone help me get this working correctly please.

edit: also sorry about the mess it looks without any indentation




Edited by on 26-10-08 22:21
Author

RE: Question on merging arrays in fortran 90

K3174N 420
Member



Posts: 296
Location: In a grow room, growing cannabis.
Joined: 14.09.08
Rank:
Hacker Level 1
Warn Level: 69
Posted on 26-10-08 22:33
1. order
2. merge
3. repeat

i don't know the language, but ill try and lay out some basic logic on the problem...


we have 2 lists of random random single digit numbers, we 1st need to change this to two lists of
ordered numbers, followed by merging them to become 1 ordered string?

ORDER
1. make a loop check and count the 1st string...
2. get the loop to read through the numbers, each time returning the lowest value...
3. write these returned values to a new string...
4. repeat for 2nd string

MERGE
5. make a loop to check each digit of both strings and return which has the '0', and if it has a '1' after and so on until a number is missed...
6. write these numbers to the string
7. check the next string for he missing number...
8. if number is there, repeat,
9. else skip the number...

REPEAT
10. repeat whole process 4 times


i don't know if this helps, but when i write programs, i jot down the logic steps like this 1st to try and keep track.

as for that language your using, i don't know it.


Thanks Yours31f!
img114.imageshack.us/img114/1497/keiran420cy2.jpg
Make poverty history... Cheaper drugs now! - Frank gallagher
[small][center]Einstein climbs to the top of Mt. Sinai to get close enough to talk to God.
Looking up, he asks the Lord...
"God, what does a million years mean to you?"
The Lord replies, "A minute."
"Einstein asks, "And what does a million pounds mean to you?"
The Lord replies, "A penny."
Einste
http://keiran420.ueuo.com/
Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 26-10-08 22:40
K3174N 420 wrote:
1. order
2. merge
3. repeat

i don't know the language, but ill try and lay out some basic logic on the problem...


we have 2 lists of random random single digit numbers, we 1st need to change this to two lists of
ordered numbers, followed by merging them to become 1 ordered string?

ORDER
1. make a loop check and count the 1st string...
2. get the loop to read through the numbers, each time returning the lowest value...
3. write these returned values to a new string...
4. repeat for 2nd string

MERGE
5. make a loop to check each digit of both strings and return which has the '0', and if it has a '1' after and so on until a number is missed...
6. write these numbers to the string
7. check the next string for he missing number...
8. if number is there, repeat,
9. else skip the number...

REPEAT
10. repeat whole process 4 times


i don't know if this helps, but when i write programs, i jot down the logic steps like this 1st to try and keep track.

as for that language your using, i don't know it.


Thanks for the suggestion but I should point out some things I guess.

1) The 4 different examples aren't done at once. The program is run a separate time for each a, b, c and d.
2) I have the sorting finished and it gives me the two lists sorted as you can see when I provided the two examples of b and c. The problem occurs when I try to merge the two sorted lists and have them in ascending order.
3) There doesn't necessarily have to be every number from 1-10 so checking which array has a 1, then 2 then 3 wont be sufficient.


Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 26-10-08 23:26
Just keep track of the current indexes for each array and, while they're not all equal to the number of items in each - 1, increment a variable... check it against each array item, and increment the indexes as you pull items from it.

Basically:

Four index variables
One counter variable

while (index1 < count(list1) - 1 and index2 < count(list2) - 1, etc.)
{
if list1[index1] == counter, then add it and increment index1
if list2[index2] == counter, then add it and increment index2
etc.
increment counter
}

It's long and winded, but I don't have the time to cook up a quicker or more efficient solution.


Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 26-10-08 23:50
Zephyr_Pure wrote:
Just keep track of the current indexes for each array and, while they're not all equal to the number of items in each - 1, increment a variable... check it against each array item, and increment the indexes as you pull items from it.

Basically:

Four index variables
One counter variable

while (index1 < count(list1) - 1 and index2 < count(list2) - 1, etc.)
{
if list1[index1] == counter, then add it and increment index1
if list2[index2] == counter, then add it and increment index2
etc.
increment counter
}

It's long and winded, but I don't have the time to cook up a quicker or more efficient solution.


Again this seems to assume that the two lists together will contain 1-10 without missing either one and without there being doubles of the same which could happen.


Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-10-08 00:36
ShapeShifters wrote:
Again this seems to assume that the two lists together will contain 1-10 without missing either one and without there being doubles of the same which could happen.

No, it doesn't... if it was expecting that, it would be shorter and not doing the repetition on each of the lists as it is. It accounts for doubles and accounts for as many numbers as it takes to go through all 4 lists. Don't skim.


Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-10-08 01:01
Not skimming, must just be misunderstanding your code.

Wouldn't

if list1[index1] == counter, then add it and increment index1
if list2[index2] == counter, then add it and increment index2
etc.
increment counter

make the list[index] be something that is equal to the counter which since it's a counter, goes up by one each time?

I don't mean to insult you or anything, that's just what I gathered from looking at it. I'm probably looking at it wrong though it seems.


Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-10-08 01:08
ShapeShifters wrote:
Not skimming, must just be misunderstanding your code.

Wouldn't

if list1[index1] == counter, then add it and increment index1
if list2[index2] == counter, then add it and increment index2
etc.
increment counter

make the list[index] be something that is equal to the counter which since it's a counter, goes up by one each time?

It's fine; I wasn't insulted. Just seemed like a hasty appraisal. Here it is, in a nutshell:

1. Each time you loop through, the counter is incremented... no matter what.
2. The index for each list is only incremented when the indexed value equals the counter... because that value will be added to the master list and you need to move onto the next value.

Hope that's clearer.


Author

RE: Question on merging arrays in fortran 90


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 27-10-08 01:19
Ok, thanks that does make it clearer Smile