asked 49.1k views
4 votes
a program is created to perform arithmetic operations on positive and negative integers. the program contains the following incorrect procedure, which is intended to return the product of the integers x and y. the program consists of 11 lines. begin program line 1: procedure multiply, open parenthesis, x comma y, close parenthesis line 2: open brace line 3: count, left arrow, 0 line 4: result, left arrow, 0 line 5: repeat until, open parenthesis, count equals y, close parenthesis line 6: open brace line 7: result, left arrow, result plus x line 8: count, left arrow, count plus 1 line 9: close brace line 10: return, open parenthesis, result, close parenthesis line 11: close brace end program. a programmer suspects that an error in the program is caused by this procedure. under which of the following conditions will the procedure not return the correct product? select two answers. responses

1 Answer

3 votes

Final answer:

The given multiplication procedure will fail when y is a negative integer because the loop won't execute and when x is negative and y is positive because it doesn't account for the sign of x.

Step-by-step explanation:

The incorrect procedure you've described in your program for the multiplication of integers will not return the correct product under two specific conditions:

  • When y is a negative integer. Since the 'repeat until' loop compares 'count' to 'y', it never enters the loop if 'y' is negative, thus 'result' remains 0, which is incorrect.
  • When x is a negative integer and 'y' is positive. The loop will execute, but since it does not account for the sign of 'x', it will incorrectly add a negative number 'y' times, instead of subtracting it.

The program should include a check for the sign of 'x' and 'y' and adjust the multiplication logic accordingly. For instance, if either or both 'x' and 'y' are negative, the program should correctly calculate the number of times 'x' is added or subtracted to/from 'result'.

answered
User Sean Carpenter
by
7.8k points

No related questions found