Friday, July 31, 2009

Switch-case statement?

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


i just need another operator for if-else statement is commonly used. our professor needs switch-case or conditional operator...





in C programming please





please.. help me?111!!

Switch-case statement?
You could use the division operator with a switch-case statement.





switch (a/16)


{


case 0: //means a is between 1 and 15


return 'B';


case 1: //means a is between 16 and 30


return 'I';


case 2: //means a is between 31 and 45


return 'N';


case 3: //means a is between 46 and 60


return 'G';


case 4: //means a is between 61 and 75


return 'O';


default:


return '0';


}
Reply:you could do something like this...





char bingo (int a)


{


switch( a / 15 )


{


case 0: return 'B'; break;


case 1: return 'I'; break;


case 2: return 'N'; break;


case 3: return 'G'; break;


case 4: return 'O'; break;


default: return 0;


}


}





there is another operator.. ? : (its called ternary operator..)


it works like an if-else


it look likes this..





%26lt;condition%26gt; ? %26lt;true%26gt; : %26lt;false%26gt;





for example..





if (a/2==1)


return 1;


else


return 0;





then it will become





a/2==1 ? 1 : 0





try browsing the web for more info..





hope, these things help you.. :)

narcissus

No comments:

Post a Comment