Jump to content


- - - - -

Inter change / Swipe values


16 replies to this topic

#1 Guest_Rajesh V.C_*

Guest_Rajesh V.C_*
  • Guests

Posted 17 October 2002 - 02:32 AM

Hello Everyone,

Its been a long time i've been here. I have a situation.
I have 2 temp numeric variables say #A and #B.
Each having a value (#A = 5 and #B = 7).

Now Without using a anyother variable i want to inter change/swipe the values. Something like in the end #A should have 7 and #B should have 5. Is it possible to do so.

I'll be greatfull for your help.

Rajesh

#2 Dennis John Laceda

Dennis John Laceda

    Member

  • Members
  • PipPip
  • 36 posts
  • Gender:Male
  • Location:Metro Manila, Philippines

Posted 17 October 2002 - 03:22 AM

is this a PROIV question or a logic question. seems to me more of a logic question.

i don't see any speficic command in PROIV or in any other language that can do this. i will be glad to know if there is any.

#3 Guest_Rajesh V.C_*

Guest_Rajesh V.C_*
  • Guests

Posted 17 October 2002 - 03:27 AM

Ya Dennis it is more of a logic question. Actually i was think is it possible to do so without using any command or help provided in PROIV. Surelly there will be a way, it just that, need to be discovered.

Raj

#4 Rob Donovan

Rob Donovan

    rob@proivrc.com

  • Admin
  • 1,640 posts
  • Gender:Male
  • Location:Spain

Posted 17 October 2002 - 03:30 AM

Hi,

Nope, there is no way to swap vars within ProIV. You have to include a 3rd var.

You can do it in lanugaes like Assembly/Machine Code, but I wont go into that :)

Rob D.

#5 Guest_Rajesh V.C_*

Guest_Rajesh V.C_*
  • Guests

Posted 17 October 2002 - 03:43 AM

I want to elobrate my question bit more further.

I have a screen in which


Step 1

Field #A (To accept value)

Field #B (To accept value)


Step 2

Field #A (Display only)

Field #B (Display only)


So in Step 1 i accept the value. Before Step 2 i want to interchange/swipe the values. I am sure you understand my question now.

#6 Dennis John Laceda

Dennis John Laceda

    Member

  • Members
  • PipPip
  • 36 posts
  • Gender:Male
  • Location:Metro Manila, Philippines

Posted 17 October 2002 - 05:22 AM

we clearly understand the question. but as Rob answered there's no special command to cater for what you need.

#7 Chris Pepper

Chris Pepper

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 369 posts
  • Gender:Male
  • Location:United Kingdom

Posted 17 October 2002 - 07:03 AM

You could always create a Global Logic to do this if you want so you don't 'see' the other variable, but you'll always need another variable.

e.g.

SWAP Swap two numeric variables
P PARMS(#ONE, #TWO)
#SAVE = #ONE
#ONE = #TWO
#TWO = #ONE

Then you could SWAP(#A,#:) in your main logic.

or

ORDER Put two numeric variables in order
P PARMS(#ONE, #TWO)
#SAVE = #ONE
#ONE = MIN(#TWO, #SAVE)
#TWO = MAX(#TWO, #SAVE)

The if you ORDER(#A,#B) you would always get the smaller value in #A and the larger in #B.

#8 Guest_Rajesh V.C_*

Guest_Rajesh V.C_*
  • Guests

Posted 17 October 2002 - 08:02 AM

Thanks guys for the help. Chris i actually wanted to do it without using third varible. After cracking my head for a long time, i found out this.

ex: #A = 5 and #B = 7

Step 1: #A = #A + #B
#A will have 12 now. (5+7)

Step 2: #B = #A - #B
#B will have 5 now. (12-7)

Step 3: #A = #A - #B
#A will have 7 now. (12-5).

Now the final value will be #A = 7 and #B = 5. Mission completed.

Thanks once again.

#9 Rick Young

Rick Young

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 265 posts
  • Gender:Male
  • Location:Guelph, Canada

Posted 17 October 2002 - 11:55 AM

I pray I never have to look at the code 6.2 years in the future, to figure out what the heck the programmer was trying to achieve - and why.

#10 Richard Bassett

Richard Bassett

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 696 posts
  • Location:Rural France

Posted 17 October 2002 - 12:35 PM

Rajesh,

I can't quite imagine why it is so important not to use another variable - I hope it's just as an instructive intellectual exercise and not code for a real system!

As Rick says, such code is much more confusing than a 3rd variable for any other programmer and requires an extensive comment which would otherwise be unnecessary.

Much more importantly, because of the nature of floating point arithmetic, your code DOES NOT WORK if the relative sizes of #A and #B are very different. To see this, try with #A=1234567890 and #B=0.00006

Sorry to be a spoilsport :)
Richard
Nothing's as simple as you think

#11 Rob Donovan

Rob Donovan

    rob@proivrc.com

  • Admin
  • 1,640 posts
  • Gender:Male
  • Location:Spain

Posted 17 October 2002 - 02:17 PM

Hmmm...

Well yes that would 'work', but why would you do that????

Richard... You would have use ##A & ##B with those big numbers, then it works :)

Rob D.

#12 Richard Bassett

Richard Bassett

    ProIV Guru

  • Members
  • PipPipPipPipPip
  • 696 posts
  • Location:Rural France

Posted 17 October 2002 - 03:47 PM

Ah yes Rob, thanks for that.. :)

Hopefully ProIV will implement ###-variables in V5.5 so we can safely use this new technique if we want to swap a couple of long numeric values B)
Nothing's as simple as you think

#13 Dennis John Laceda

Dennis John Laceda

    Member

  • Members
  • PipPip
  • 36 posts
  • Gender:Male
  • Location:Metro Manila, Philippines

Posted 18 October 2002 - 05:45 AM

there are other loopholes in that computation. such as pointed by our PROIV buddies here.

but again i won't even go into what you have as a solution. i will just give it a third variable. also i didn't even gave it a thought. cause basically it's not a PROIV question.

sorry for being a PROIV sensitive guy.... hehehe.

Cheers.

#14 Dennis John Laceda

Dennis John Laceda

    Member

  • Members
  • PipPip
  • 36 posts
  • Gender:Male
  • Location:Metro Manila, Philippines

Posted 18 October 2002 - 05:59 AM

BTW what apps are you developing this one for? why is it really important not to use a 3rd variable?

#15 Tim Woodall

Tim Woodall

    Member

  • Members
  • PipPip
  • 20 posts

Posted 18 October 2002 - 11:47 AM

Back in the good old days of 8 bit microprocessors (and even up as far as the x386 IIRC) this technique was useful in assembly as it could be done totally in registers without having to use another register or a read and write to ram

Pentium etc it doesn't help. Infact, on the pentium you will get a pipeline stall unless you can interleave some other stuff

(In assembly it was (almost?) always done with XO

XOR A,
XOR B,
XOR A,

You will occasionally see this written in C a

A ^= B
B ^= A
A ^= B

although nowadays this is frowned upon and usually slower than a simple swap using a third variable. (C++ templates and inline remove the few final objection for a swap macro

(Very occasionally you will see A ^= B ^= A ^= B; Unfortunately this has undefined behaviour in C so it isn't as clever as the author thought :-



Reply to this topic



  


0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users