Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.programming > #2016 > unrolled thread
| Started by | sathyashrayan <sathyashrayan@gmail.com> |
|---|---|
| First post | 2012-07-28 19:24 -0700 |
| Last post | 2012-08-01 12:26 -0400 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.programming
problem 2.1.2 from "how to solve it by computer, Dromey" sathyashrayan <sathyashrayan@gmail.com> - 2012-07-28 19:24 -0700
Re: problem 2.1.2 from "how to solve it by computer, Dromey" pete <pfiland@mindspring.com> - 2012-07-29 00:03 -0400
Re: problem 2.1.2 from "how to solve it by computer, Dromey" pete <pfiland@mindspring.com> - 2012-08-01 12:26 -0400
| From | sathyashrayan <sathyashrayan@gmail.com> |
|---|---|
| Date | 2012-07-28 19:24 -0700 |
| Subject | problem 2.1.2 from "how to solve it by computer, Dromey" |
| Message-ID | <e531d211-e0ad-47d5-8b68-542b3fef915f@googlegroups.com> |
I am a beginner in learning algo..So please bear with me.. 2.1.2 Design an algorithm that makes following exchanges of 3 varioubles.. a-->b-->c--| ^ | ^----------| So a_temp = a; b_temp = b; c_temp = c; a=c_temp; c=b_temp; b=a_temp; I know this process looks ugly. Can anyone suggest a good one so that I could learn.
[toc] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-07-29 00:03 -0400 |
| Message-ID | <5014B5FE.2FC3@mindspring.com> |
| In reply to | #2016 |
sathyashrayan wrote: > > I am a beginner in learning algo..So please bear with me.. > > 2.1.2 Design an algorithm that makes following exchanges of 3 varioubles.. > > a-->b-->c--| > ^ | > ^----------| > > So > a_temp = a; > b_temp = b; > c_temp = c; > > a=c_temp; > c=b_temp; > b=a_temp; > > I know this process looks ugly. Can anyone suggest a good one so that I could learn. temp = c c = b b = a a = temp -- pete
[toc] | [prev] | [next] | [standalone]
| From | pete <pfiland@mindspring.com> |
|---|---|
| Date | 2012-08-01 12:26 -0400 |
| Message-ID | <501958AC.5261@mindspring.com> |
| In reply to | #2017 |
pete wrote:
>
> sathyashrayan wrote:
> >
> > I am a beginner in learning algo..So please bear with me..
> >
> > 2.1.2 Design an algorithm
> > that makes following exchanges of 3 varioubles..
> >
> > a-->b-->c--|
> > ^ |
> > ^----------|
> >
> > So
> > a_temp = a;
> > b_temp = b;
> > c_temp = c;
> >
> > a=c_temp;
> > c=b_temp;
> > b=a_temp;
> >
> > I know this process looks ugly.
> > Can anyone suggest a good one so that I could learn.
>
> temp = c
> c = b
> b = a
> a = temp
If you were in a situation where you might
not be guaranteed to have a temp variable
suitable for the values of the variables in question,
but if you had a way to swap values between variables,
then something like this would work:
SWAP(b, c);
SWAP(a, b);
In C, that could look like this:
/* BEGIN new.c */
#include <stdio.h>
#define SWAP(X, Y) (swap(&X, &Y, sizeof X))
void swap(void *arg1, void *arg2, size_t size)
{
unsigned char temp;
unsigned char *a1 = arg1;
unsigned char *a2 = arg2;
while (size-- != 0) {
temp = *a1;
*a1++ = *a2;
*a2++ = temp;
}
}
int
main(void)
{
int a = 1;
int b = 2;
int c = 3;
printf("(a) is %d\n", a);
printf("(b) is %d\n", b);
printf("(c) is %d\n", c);
putchar('\n');
SWAP(b, c);
SWAP(a, b);
printf("(a) is %d\n", a);
printf("(b) is %d\n", b);
printf("(c) is %d\n", c);
return 0;
}
/* END new.c */
--
pete
[toc] | [prev] | [standalone]
Back to top | Article view | comp.programming
csiph-web