
Patrick B. answered 03/20/20
Math and computer tutor/teacher
ASSUMING the switch statement is inside function int g( int x , int y),
then the return value shall be 5.
for x=9 and y=5, x-y=9-5=4, so case 4 in the switch gets executed, which
does nothing except break; 5 gets returned following the switch
The same ASSUMPTION for problem #2, is that the switch lies inside function int f( int x, int y)..
the function will then return 80...
for x=5 and y=10, x+2 < y ---> 5+2 < 10 ---> 7 < 10 is true;
so then x becomes 5+3=8
finally 8*10 = 80 is returned
also note there is } missing, so that shall be a compile error; even though it
is technically unreachable code, some compilers will demand it... I'm using Bloodshed Dev C++
and it caused a compile error despite the fact it is unreachable
Here is the complete code:
#include <stdio.h>
int g(int x, int y)
{
switch(x - y)
{
case 0:
return x;
case 4:
break;
case 7:
x = x - 1;
case 9:
return x*y;
case 3:
default:
return y - x;
} //switch
return y;
}
int f( int x, int y)
{
if (x + 2 < y)
{
x = x + 3;
return y * x;
} //compile error: missing }
else {
return x + y + 2;
}
}
int main()
{
printf("%d \n",g(9,5));
printf("%d \n ",f(5,10));
}