Sunday, July 12, 2009

Switch case or conditional operator?

1 to 15 is letter "B"


16 to 30 is letter "I"


31 to 45 is letter "N"


46 to 60 is letter "G"


61 to 75 is letter "O"





with this solution..





char bingo(int a)


{


if(a%26gt;=1 %26amp;%26amp; a%26lt;=15) return 'B';


else if(a%26gt;=16 %26amp;%26amp; a%26lt;=30) return 'I';


else if(a%26gt;=31 %26amp;%26amp; a%26lt;=45) return 'N';


else if(a%26gt;=46 %26amp;%26amp; a%26lt;=60) return 'G';


else if(a%26gt;=61 %26amp;%26amp; a%26lt;=75) return 'O';


else return '0';


}


main (void)


{


/* whatever has to be done */





}

















i need to use another control structure like switch-case or conditional operator...





in C programming please








please.. help

Switch case or conditional operator?
I think you could try something like this:





switch( (a - 1) / 15){


case 0: return 'B';


case 1: return 'I';


case 2: return 'N';


case 3: return 'G';


case 4: return 'O';


default: return '0';


}





This relies on the quirk that if you use the / operator between integers it always returns an integer (with the part after the decimal point simply cut off).





I haven't checked it, but I think it's perfectly correct.

columbine

No comments:

Post a Comment