5071c5b861341c0dcfcf6ac86327701f

I tried to use as less variables and less operators as possible.
Please help to make the code better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int max = rand(); // or any init value
int min = rand(); // or any init value
int tmp = rand(); // or any init value

if (max < min) {
  if (tmp < min) {
    if (max < tmp) {
      tmp = max;
      max = min;
      min = tmp;
    } 
    else { // max < min && tmp < min && tmp <= max
      max = min;
      min = tmp;
    }
  } 
  else { // max < min && min <= tmp
    min = max;
    max = tmp;
  }
  
}
else { // min <= max
  if (tmp < min)
    min = tmp;

  else // min <= max && min <= tmp
  if (max < tmp)
    max = tmp;
}

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

November 18, 2008, November 18, 2008 16:34, permalink

2 ratings. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <limits.h>

#define MIN(a,b) ((a) > (b) ? (b) : (a))
#define MAX(a,b) ((a) > (b) ? (a) : (b))

int main(int argc, char **argv)
{
    int i;
    int min = INT_MAX;
    int max = 0;
    int numbers[] = { 10, 2, 5 };
        
    for (i = 0; i < 3; i++) {
        min = MIN(numbers[i], min);
        max = MAX(numbers[i], max);
    }
    
    return 0;
}
5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

November 18, 2008, November 18, 2008 23:55, permalink

No rating. Login to rate!

Try this code, it was a kind of magic to me since I see it the first time in a programing class eons ago.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int max = rand(); // or any init value
int min = rand(); // or any init value
int tmp = rand(); // or any init value


if (tmp < min) {
  min ^= tmp ^= min ^= tmp;
}

if (max < tmp) {
  max ^= tmp ^= max ^= tmp;
}

// now min , tmp and max are ordered
5071c5b861341c0dcfcf6ac86327701f

Tien Dung

November 20, 2008, November 20, 2008 00:01, permalink

No rating. Login to rate!

Hi Eineki, please explain how it works?

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

November 20, 2008, November 20, 2008 00:52, permalink

1 rating. Login to rate!

It is quite simple, once you know.
All the trick is based upon the XOR operator.
the table of truth of xor is:
T ^ T = F
F ^ F = F
T ^ F = T
F ^ T = T

I hope the explanation will be sufficient.
Try by yourself with a piece of paper and a pencil a couple of time and you will find the solution very easy.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* Lets say we have two numbers: 27 and 145 (just two random values) into two separate variable */

int min = 27;  // 00011011 binary 8bit for 27  
int tmp = 145; // 10010001 

/* lets put the result into a third one */

int xored;
xored = min ^ tmp; // now contains 10001010

/* applying the table of xor you will find that starting
 * from xored you can obtain one of the value applying
 * the xor between xored itself and the other value. Let's try */

printf ("apply min (%d) to xored should obtain tmp (%d)\n", min, xored ^ min); // xored ^ min is 10010001
printf ("apply tmp (%d) to xored should obtain min (%d)\n", tmp, xored ^ tmp); // xored ^ tmp is 00011011

/* it is a well known property of xor used, for example, in 
 * computer security to share a secret between two or more actors
 * being sure that no guardian can reach the secret without the 
 * intervention of all the other ones */

/* now we can explode the totem: min ^= tmp ^= min ^= tmp
 * remember that you have to read the assignments right to left */

min = min ^ tmp;  // min becomes the xored signature;
tmp = min ^ tmp;  // xoring tmp versus the signature you obtain min original value and put it in tmp
min = min ^ tmp;  // min contains the signature, tmp contains the original value contained in min
                  // if you do the xor of the two values you obtain tmp original value.
Avatar

udi.m.myopenid.com

January 11, 2009, January 11, 2009 17:23, permalink

No rating. Login to rate!

I think about:

1
2
3
4
5
int a, b, c, T;

...

printf("%d\n", (T=(a>b)?a:b)>c?T:c);
7d773811fbf15b5f527a20917a84e62a

vabuk

February 12, 2009, February 12, 2009 04:49, permalink

No rating. Login to rate!

Have a look at the code below. This is for max and u can apply same concept for min. Just try by yourself

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>

main()
{
	int a,b,c;
	int max;
	printf("Enter three numbers:\n");
	scanf("%d%d%d",&a,&b,&c);
	

	max =a;

	if(max<b)
	{
		max=b;
	}
	if(max<c)
	{
		max=c;
	}

	printf("The Maximum is %d\n",max);

}
Avatar

Pax

March 6, 2009, March 06, 2009 03:58, permalink

No rating. Login to rate!
1
2
3
4
5
int n1 = 7;
int n2 = 9;
int n3 = 4;
int min = (n1 < n2) ? ((n1 < n3) ? n1 : n3) : ((n2 < n3) ? n2 : n3);
int max = (n1 > n2) ? ((n1 > n3) ? n1 : n3) : ((n2 > n3) ? n2 : n3);
18e9e67139daa390598fbdeb62d903f6

Denis

June 10, 2009, June 10, 2009 08:31, permalink

No rating. Login to rate!

Dear Eineki,

I was amazed by your idea, but unfortunately when I tried your code it didn't work for all the time... Sorry...

Sincerely,
Denis

18e9e67139daa390598fbdeb62d903f6

Denis

June 10, 2009, June 10, 2009 09:58, permalink

No rating. Login to rate!

P.S.: say, if min=4, tmp=5 and max=3 at the beginning, after xoring operations they will be:

min = 4
tmp = 3
max = 5

which means that they are not ordered...

6e0bae5b958a62a7475f2e426acca6cc

aycan

July 8, 2009, July 08, 2009 12:48, permalink

No rating. Login to rate!

what about if we don't know the number of entrance? I mean how should the code be if I want the reader to choose how many numbers to enter?

E8ba2f720d00c160c7e7562b10c585ee

whooda

July 24, 2009, July 24, 2009 13:37, permalink

No rating. Login to rate!

then you can use one of the many proven sort algorithms :)
http://en.wikipedia.org/wiki/Sorting_algorithm#Summaries_of_popular_sorting_algorithms

5a00a3a98dcf6f9cd717440fd2b606e5

Eineki

October 5, 2009, October 05, 2009 18:33, permalink

No rating. Login to rate!

Hi Denis,
I'd to perform some other tests before posting, bolow you will find the correct version,
there is an additional test and swap in order to permit the "max" value to travel two
position down.

You know, maybe the algorithm is not the best one you can write since it count on swapping
only thre variables but I think, given the original limitation, is one of the more efficient.

Thanks for your annotations,
Eineki

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int max = rand(); // or any init value
int min = rand(); // or any init value
int tmp = rand(); // or any init value


if (max < min) {
  min ^= max ^= min ^= max;
}

if (tmp < min) {
  min ^= tmp ^= min ^= tmp;
}


if (max < tmp) {
  max ^= tmp ^= max ^= tmp;
}

// now min , tmp and max are ordered
E3cefc267d1f687a9256120b320ee6d4

dicones

October 27, 2009, October 27, 2009 21:48, permalink

No rating. Login to rate!

guyzzz nid help!!
i have a program that converts octal numbers into decimal, binary and hexadecimal,
and i want to limit the octal input to only four bits.
any idea how can i achieve the limit of four bits.
tnx.
godspeed

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include<stdio.h>
#include<conio.h>
#include<string.h>

void od();
void ob();
void oh();


void main()
{
		 int n;
		 char c;
		 begin:
		 clrscr();
		 printf("		--OCTAL CONVERSIONS--\n");
		 printf("Enter your choise.(1-3)\n");
		 printf("\n1.  Octal to Decimal.");
		 printf("\n2.  Octal to Binary.");
		 printf("\n3.  Octal to Hexadecimal.\n");

		 scanf("%d",&n);
		 if(n<1 || n>12)
		  printf("Invalid Choice");
		 if(n==1)
		  od();
		 else if(n==2)
		  ob();
		 else if(n==3)
		 {
		  unsigned long n,i=0,a,p=1,t=0;
		  clrscr();
		  printf("Conversion from Octal to Hexadecimal.\n");
		  printf("Enter an Octal number");
		  scanf("%ld",&n);
		  i=0;
		  while(n!=0)
		  {
		   a=n%10;
		   if(a>7)
		    t=1;
		   n=n/10;
		   i=i+a*p;
		   p=p*8;
		  }
		  if(t==0)
		  {
		   printf("Hexadecimal equivalent =");
		   oh(i);
		  }
		  else if(t==1)
		   printf("The number you entered is not an octal number!.");
		 }
		 /*else if(n==12)*/
		  /*ho();*/
		 printf("\nDo you want to continue(Y/N)");
		 scanf("%s",&c);
		 c=toupper(c);
		 if(c=='Y')
		  goto begin;
		 getch();
}




void od()
{
		 unsigned long n,i=0,a,p=1,t=0;
		 clrscr();
		 printf("Conversion from Octal to Decimal\n");
		 printf("Enter an Octal number");
		 scanf("%ld",&n);
		 i=0;
		 printf("Decimal equivalent of %ld",n);
		 while(n!=0)
		 {
		  a=n%10;
		  if(a>7)
		   t=1;
		  n=n/10;
		  i=i+a*p;
		  p=p*8;
		 }
		 if(t==0)
		  printf("= %ld",i);
		 else if(t==1)
		  printf(" cannot be calculated because it is not an Octal Number.");
}

void ob()
{
		 int n,a[6],i=0,t=0;
		 clrscr();
		 printf("Convertion from Octal to Binary.\n");
		 printf("Enter an Octal Number.");
		 scanf("%d",&n);
		 printf("The Binary equivalent is \n");
		 while(n!=0)
		 {
		  a[i]=n%10;
		  n=n/10;
		  if(a[i]>7)
		   t=1;
		  i++;
		 }
		 i--;
		 if(t==0)
		  for(;i>=0;i--)
		  {
		   switch(a[i])
		   {
		    case 0:
		     printf("000");
		     break;
		    case 1:
		     printf("001");
		     break;
		    case 2:
		     printf("010");
		     break;
		    case 3:
		     printf("011");
		     break;
		    case 4:
		     printf("100");
		     break;
		    case 5:
		     printf("101");
		     break;
		    case 6:
		     printf("110");
		     break;
		    case 7:
		     printf("111");
		     break;
		   }
		  }
		 if(t==1)
		  printf("Not an Octal number!");
}




void oh(long n)
{
		 long i;
		 if(n>0)
		 {
		  i=n%16;
		  n=n/16;
		  oh(n);
		  if(i>=10)
		  {
		   switch(i)
		   {
		    case 10:
		     printf("A");
		     break;
		    case 11:
		     printf("B");
		     break;
		    case 12:
		     printf("C");
		     break;
		    case 13:
		     printf("D");
		     break;
		    case 14:
		     printf("E");
		     break;
		    case 15:
		     printf("F");
		     break;
		   }
		  }
		  else
		   printf("%ld",i);
		 }
}


23f39c3d212219f619acc6689548c276

aakashdeep26

April 3, 2010, April 03, 2010 08:07, permalink

No rating. Login to rate!

Hi guyz...!
Here is the most easiest way to find max n min bet 3 no.(s)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include<stdio.h>
#include<conio.h>

void main()
{
	int a=0,b=0,c=0;
	clrscr();
	printf("Enter three no.(s) :- ");
	scanf("%d%d%d",&a,&b,&c);
	if(a>b&&a>c)
	{
		printf("%d is greater.",a);
	}
	else if(b>a&&b>c)
	{
		printf("%d is greater.",b);
	}
	else
	{
		printf("%d is greater.",c);
	}
	getch();
}
Ede382088e4b660ea3083f73cb525120

DINAKARAN

April 17, 2010, April 17, 2010 07:11, permalink

No rating. Login to rate!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<stdio.h>
#include<conio.h>

void main()
{
	int a=0,b=0,c=0;
	clrscr();
	printf("Enter three no.(s) :- ");
	scanf("%d%d%d",&a,&b,&c);
	if(a>b&&a>c)
	{
		printf("%d is greater.",a);
	}
	else if(b>a&&b>c)
	{
		printf("%d is greater.",b);
	}
	else
	{
		printf("%d is greater.",c);
	}
	getch();
}

Your refactoring





Format Copy from initial code

or Cancel