Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#Program Analysis# – What is output by in the following program? Show All Work v

ID: 3579512 • Letter: #

Question

#Program Analysis# – What is output by in the following program? Show All Work

void func (int i, int j);

{

          int iRetVal;

if ( i >= -2 && j <= 2 )

          if (j % 3)

iRetVal = i – j;

                   else

                             iRetVal = i + j

          else

                   iRetVal = i – j * 2;

          cout << iRetVal <<endl;

}

Problem (a)   i = 4 and j = 2                       _____________________

Problem (b)   i = 5 and j = 3                      _____________________

Explanation / Answer

As per the given code.
the programe execution will throw an error saying that "declaration terminated incorrectly." irrespective of input values.
Because there is a semicolon(;) just after the signature of the method "func" . due to presence of that semicolon compiler will see curley braces as illegal.

But if we ignore that semicolon and assume that it's there by mistake then the program would look somthing like this.

#include<iostream.h>
#include<conio.h>

void func(int i,int j)
{
int iRetVal;

if(i>=-2 && j<=2)
{
   if(j%3)
   {
       iRetVal=i-j;

   }
   else
   iRetVal=i+j;
}
else
iRetVal=i-j*2;
cout<<iRetVal<<endl;
}

int main()
{
clrscr();
func(5,3);
getch();
return 0;
}

for inputs i=4 and j=2
output would be 2

Since i>=-2 and j<=2 both the statements are satisfied to control will enter in if section.
then it will check j%3 .in this case it's 2%3 =2 which is not 0. that mean the nested "if" will be executed and result will be i-j that is 4-2=2

for inputs i=5, j=3
Output would be -1

Since i>=-2 but j >=2 so the if condition is not satisfied and thus else block will be executed.
So the result would be i-j*2 = 5-3*2= 5-6=-1.