In the definition of BIG, I am subtracting one from unsigned zero, producing the highest possible unsigned int value.
In the line labeled SHOOP DA WOOP! I am first comparing either u or o, depending on whether x is even or odd, to x. If x is smaller, then I am assigning x to e or o. The part (x%2?&e:&o) gets either the address of e or o, depending on if x is even or odd. Then I derefernce it and assign x to either e or o.
EDIT: Also this should not inspire you. It is an example of how to write code that wlll make your coworkers try to eat your soul.
EDIT2: You can further simplify this by using scanf:
1 2 3 4 5 6 7 8
#include "stdio.h"
#define BIG (0U-1)
int main(){
unsigned o=BIG,e=BIG,x,i=0;
while(i++<6&&scanf("%d",&x)>0)
(x%2?e:o)>x&&(*(x%2?&e:&o)=x);//SHOOP DA WOOP!
printf("min even: %u\nmin odd: %u\n",e,o);
}
#include <iostream>
#include <climits>
usingnamespace std;
#define C( D ) ((D<0)?(-D):D)
#define p( u ) const char* u
#define l( o ) (C(o)%2)
#define u( a , b) ((a<b)?a:b)
#define s( s ) int s
#define P for
#define L cout
#define U(dot) S(dot)=u(S(dot),dot)
#define S(com) SS[l(com)]
int main()
{
p(VV[2])={"even","odd"};s(SS[2])={INT_MAX,INT_MAX};
P(s(n)=0;n<6;n++) {s(z);cin>>z;U(z);}
P(s(n)=0;n<2;n++) {L<<"The smallest "<<VV[n]<<" number is "<<SS[n]<<"\n";}
}
For a valid homework solution that your professor will fail you for turning in (because it obviously isn't your work):
#include <iostream>
#include <climits>
usingnamespace std;
inlineint abs( int n ) { return (n < 0) ? -n : n; }
inlineint min( int a, int b ) { return (a < b) ? a : b; }
inlinebool odd( int n ) { return abs( n ) % 2; }
int main()
{
int smallest_odd = INT_MAX;
int smallest_even = INT_MAX;
for (unsigned n = 0; n < 6; n++)
{
int z;
cin >> z;
if (odd( z )) smallest_odd = min( smallest_odd, z );
else smallest_even = min( smallest_even, z );
}
cout << "The smallest even number is " << smallest_even << endl;
cout << "The smallest odd number is " << smallest_odd << endl;
return 0;
}
I notice that a lot of the solutions here don't strictly conform with the assignment:
- avoid arrays or other (unnamed) things not yet taught
- accept negative numbers as input
- format output as given
#include "stdio.h"
#define BIG (~0U>>1)
int main(){
signed o=BIG,e=BIG,x,i=0;
while(i++<6&&scanf("%d",&x)>0)
(x&1?o:e)>x&&(*(x&1?&o:&e)=x);//SHOOP DA WOOP!
printf("min even: %d\nmin odd: %d\n",e,o);
}