Programming Language Pragmatics, Fourth Edition
Programming Language Pragmatics, Fourth Edition
4th Edition
ISBN: 9780124104099
Author: Michael L. Scott
Publisher: Elsevier Science
Expert Solution & Answer
Book Icon
Chapter 1, Problem 4E

Explanation of Solution

Justification:

“No”, this program does not compute the same result.

  • The variation between the two programs is with the assignments statements in each program.
  • The “Example 1.20” uses “i=i-j” and “j=j-i” but in the given program the assignment statement is represented as “i=i%j” and “j=j%i”.
  • Suppose “i>j”, then “i == i-(j*(i/j))”, here the slash (/) indicates integer division...

Explanation of Solution

The solution for the given program:

  • The following program fix the problems in the given program.

        //Defining the main function.

        int main()

        {

/*Getting the input of i and j using getint() function.*/

            int i = getint(), j = getint();

            //Checking the condition for i and j.

            if (i < j)

             {

/*Storing the j value to the temporary value.*/

                int t = j;

                //Swapping the value i and j.

                j = i;

    /*Storing the temporary value to the i value.*/

                i = t;

            }

            //Checking for the j value.

            while (j != 0)

            {

                //Assigning the j to temporary value.

                int t = j;

    /* Getting the i using computation of i%j. */

                i = i % j;

/* Storing the temporary value to the j. */

                j = t;

            }

            //Printing the i using putint() function...

Explanation of Solution

Situations in which the programs gives the faster result:

  • The value of “i and j” are in the same magnitude, the original subtraction based program may produce the faster output...

Blurred answer
Knowledge Booster
Background pattern image
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education