2017-04-07 88 views
-2

我有兩個陣列:A = [1 2 3 4]B = [5 6 7 8]。我如何將A & B合併到一個數組C中,然後按升序對C進行排序?我需要這樣做FORTRAN 77中在Fortran 77中合併陣列

+1

大家好,歡迎計算器!這不是一個代碼寫入服務;請張貼您嘗試過的例子以及您遇到的具體問題。請參閱http://stackoverflow.com/help/how-to-ask獲取指導。 – betseyb

+0

也清楚地說明你爲什麼不能使用現代fortran。 – agentp

回答

-1

這裏是一個天真的實現級聯/排序算法:

program sort 
    integer size1, size2, sizeout 
    parameter (size1 = 4, size2 = 4) 
    parameter (sizeout = size1 + size2) 
    integer in1(size1), in2(size1) 
    data in1/1,2,4,4/, in2/5,8,7,5/ 
    integer out(sizeout) 

c concatenate arrays 
    do j=1,size1 
     out(j)=in1(j) 
    enddo 
    do j=1,size2 
     out(j+size1)=in2(j) 
    enddo 

c sort the elements of the output array 
4 do j=2,sizeout 
     if(out(j).lt.out(j-1)) then 
      temp =out(j-1) 
      out(j-1)=out(j ) 
      out(j )=temp 
      goto 4 
     endif 
    enddo 

    end 
+1

這是Fortran 77和現代Fortran的一些方法。根據任何標準它都不合法。 – francescalus

+0

@francescalus謝謝你注意到這一點。正如我所提到的,這是在我的機器上用gfortran編譯的一個天真的實現。它連接兩個數組並按升序對它們進行排序。請隨時編輯我的答案,使其符合F77。 – Sergio

+1

要麼嘗試使用SO作爲代碼寫入服務,要麼嘗試使用*或*填充它們。不是都。 –