5071c5b861341c0dcfcf6ac86327701f

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

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!
#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.

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.

/* 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.
55502f40dc8b7c769880b10874abc9d0

udi.m.myopenid.com

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

No rating. Login to rate!

I think about:

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

#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);

}
D41d8cd98f00b204e9800998ecf8427e

Pax

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

No rating. Login to rate!
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

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

#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)

#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!
#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();
}
D41d8cd98f00b204e9800998ecf8427e

Rushabh u.

November 8, 2011, November 08, 2011 04:53, permalink

No rating. Login to rate!
#include<stdio.h>
#include<conio.h>
voidmain()
  {
int num1,num2,num3;
clrscr();
  printf("enter first num");
scanf("%d",&num1);
  printf("enter 2nd number");
scanf("%d",&num2);
  printf("enter 3rd number");
scanf("%d",&num3);
if(num1>num2 && num1>num2);
  printf("%d is largest number",num1);
  else
if("num2>num3 && num2>num1")
  printf("%d is largest number",num2);
  else
if("num3>num2 && num3>num1");
  printf("%d is largest number",num3);
getch();
B021b0b7631b8793b9b6667592974d4d

Rushabh u.

November 8, 2011, November 08, 2011 04:53, permalink

No rating. Login to rate!
#include<stdio.h>
#include<conio.h>
voidmain()
  {
int num1,num2,num3;
clrscr();
  printf("enter first num");
scanf("%d",&num1);
  printf("enter 2nd number");
scanf("%d",&num2);
  printf("enter 3rd number");
scanf("%d",&num3);
if(num1>num2 && num1>num2);
  printf("%d is largest number",num1);
  else
if("num2>num3 && num2>num1")
  printf("%d is largest number",num2);
  else
if("num3>num2 && num3>num1");
  printf("%d is largest number",num3);
getch();
D41d8cd98f00b204e9800998ecf8427e

Rushabh u.

November 8, 2011, November 08, 2011 04:53, permalink

No rating. Login to rate!
#include<stdio.h>
#include<conio.h>
voidmain()
  {
int num1,num2,num3;
clrscr();
  printf("enter first num");
scanf("%d",&num1);
  printf("enter 2nd number");
scanf("%d",&num2);
  printf("enter 3rd number");
scanf("%d",&num3);
if(num1>num2 && num1>num2);
  printf("%d is largest number",num1);
  else
if("num2>num3 && num2>num1")
  printf("%d is largest number",num2);
  else
if("num3>num2 && num3>num1");
  printf("%d is largest number",num3);
getch();
F9a9ba6663645458aa8630157ed5e71e

Ants

November 8, 2011, November 08, 2011 20:49, permalink

No rating. Login to rate!

@Rushabh u: Are you sure your code works? I don't think it even compiles. Even if it compiled, you've just succeeded in finding the max, but the problem is to find the max and min numbers while minimizing the comparisons and variables.

Your refactoring





Format Copy from initial code

or Cancel