-3

I'm writing a complex program that calculates change in potential energy( ΔPE= mgΔh ).

Console.WriteLine("Mass in kg");
string cmMa = Console.ReadLine();
double cMass;
if(!Double.TryParse(cmMa, out cMass) )
{
    Console.WriteLine("Only numbers!");
    Console.ReadLine();
}
Console.WriteLine("First height in m");
string cfH = Console.ReadLine();
double fH;
if(!Double.TryParse(cfH, out fH))
{
    Console.WriteLine("Only numbers!");
    Console.ReadLine();
}
Console.WriteLine("Second height in m");
string csH = Console.ReadLine();
double sH;
if(!Double.TryParse(csH, out sH))
{
    Console.WriteLine("Only numbers!");
    Console.ReadLine();
}
double ch = fH - sH;
Console.WriteLine("Intermediate result: Change in height(Δh)= "+ch+" m" );
Console.ReadLine();
double ng = 9.81;  //   m/s^2
Console.WriteLine("CHANGE IN POTENTIAL ENERGY: "+ch*cMass*ng+" J");

At the end, I want to add an if statement that if sH>fH, then their values are swapped. What concept should I apply?

Darren
  • 68,902
  • 24
  • 138
  • 144

7 Answers7

3

To swap values of sH and sF:

sH = Interlocked.Exchange(ref sF, sH);

Class Interlocked is declared in System.Threading namespace.

It allows to swap variables in one line. The swap is atomic, thread-safe.

In terms of performance it is a little bit "slower" than swap using temp variable. It is still lighting-fast. One should not worry unless doing extreme optimization at nano-scale level.

VeganHunter
  • 5,584
  • 2
  • 26
  • 26
1

Store one of the variables in a temp an variable and then swap them:

if (sH > fH) {
   var temp = sH;
   sh = fH;
   fH = temp; 
}
Darren
  • 68,902
  • 24
  • 138
  • 144
1
if (sH > fH) {
  double temp = sH;
  sH = fH;
  fH = temp;
}

The swapping can be accomplished by using a temporary third variable. Store sH in a temporary variable and assign sH to fH. This way the value of sH is cached and not lost when reassigning. Lastly, assign fH to temp. The values are now swapped.

Hope this helps

Paul Renton
  • 2,652
  • 6
  • 25
  • 38
1

Assuming your reason for doing this is so that ch isn’t negative here:

double ch = fH - sH;

Just get the absolute value:

double ch = Math.Abs(fH - sH);
Ry-
  • 218,210
  • 55
  • 464
  • 476
1

Use:

static void Main(string[] args)
{
    Console.WriteLine("please imput number 1 ");
    int a = Convert.ToInt32(Console.ReadLine());
    Console.WriteLine("please imput number 2 ");
    int b = Convert.ToInt32(Console.ReadLine());

    if (a > b)
    {
        int temp1 = a;
        int temp2 = b;
        b = temp1;
        a = temp2;
    }
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
louis
  • 11
  • 1
0

Er... Copy A into a tmp variable, copy B into A, copy tmp into B? Since you are dealing with strong parsing, I can't see that anything crazy like an xor-swap (via unsafe integer treatment) would be warranted.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
-2

Try this, but this is not as fast as using a temporary variable:

if (sH > fH)
{
    sH -= fH;
    fH += sH;
    sH = fH - sH;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131