How to swap number in the fortran memory?

In summary, the conversation discusses how to swap numbers in the Fortran memory. The suggested solution is to use a temporary variable to store one of the numbers and then swap them using a do loop. The conversation also mentions using a flag to stop the loop if no numbers are swapped. Additionally, there is a mention of arranging numbers in ascending order using a similar method.
  • #1
dadidudada
6
0
how to swap number in the fortran memory??

hello..
im new to fortran so how i swap number in do loop
first the tracing is like this:

x(1)=3
x(2)=8

then, how to i can swap the number in x(1) and x(2)

can anyone help me
please...
 
Technology news on Phys.org
  • #2


Why would you not use;

a=x(1)
x(1)=x(2)
x(2)=a

?
 
  • #3


ok I am completely blank..
if i have to arrange these number(10,4,1)into ascending order..
what should i do..
 
Last edited:
  • #4


Run through each number (to n-1) in the list (of n numbers) and swap over any where x(a) > x(a+1). Repeat. Set up a flag so you can stop if none are swapped during a repeat.
 
  • #5


cmb said:
Run through each number (to n-1) in the list (of n numbers) and swap over any where x(a) > x(a+1). Repeat. Set up a flag so you can stop if none are swapped during a repeat.

thanks man!its solved..
credit to cmb...
 

1. How do I swap numbers in the Fortran memory?

In order to swap numbers in the Fortran memory, you can use the Fortran intrinsic subroutine "swap". This subroutine takes two arguments, the first being the variable you want to swap and the second being the variable to swap it with. For example, if you want to swap the values of variables A and B, you can use "call swap(A, B)".

2. Can I swap more than two numbers at a time in Fortran?

Yes, you can use the "swap" subroutine to swap multiple numbers at a time. Simply pass in the variables you want to swap in the order you want them to be swapped. For example, if you want to swap variables A, B, and C, you can use "call swap(A, B, C)".

3. How does the "swap" subroutine work in Fortran?

The "swap" subroutine in Fortran works by temporarily storing the value of the first variable in a temporary location, then replacing the first variable's value with the value of the second variable. Finally, it stores the value of the second variable in the first variable's original location. This effectively swaps the values of the two variables.

4. Can I swap numbers in an array in Fortran?

Yes, you can swap numbers in an array in Fortran by using the "swap" subroutine on the specific array elements you want to swap. For example, if you have an array A and you want to swap the values of elements 2 and 5, you can use "call swap(A(2), A(5))".

5. Are there any other ways to swap numbers in Fortran?

Yes, there are other ways to swap numbers in Fortran, such as using temporary variables or mathematical operations. However, the "swap" subroutine is the most efficient and recommended way to swap numbers in Fortran as it is specifically designed for this purpose.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
19
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
941
Replies
63
Views
3K
  • Programming and Computer Science
Replies
2
Views
156
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
Back
Top