Question
i, j, and k have been declared as integers, duplicateFound has been declared to be boolean, and a has been declared to be an integer
i, j, and k have been declared as integers, duplicateFound has been declared to be boolean, and a has been declared to be an integer array with at least k+1 cells. duplicateFound = false; for (j = 1; j < k; ++j) for (i = k; i > j; i) if (a[i] == a[j]) duplicateFound = true; The "outer loop" is the "for" loop controlled by the loop index j . The "body of the outer loop" consists, in this example, of another loop, the "inner" (or "nested") loop controlled by the loop index i . The "body of the inner loop" in this example is an "if" statement.
(a) How many times will the body of the inner loop be executed on the first time that the body of the outer loop is executed (i.e., when j has the value 1 ) ? (Express your answer in terms of k .)
(b) How many times will the body of the inner loop be executed on the second time that the body of the outer loop is executed (when j has the value 2 ) ? (Express your answer in terms of k .)
(c) How many times will the body of the inner loop be executed on the last time that the body of the outer loop is executed (when j has the value k1 ) ?
(d) How many times altogether will the test expression a[i] == a[j] be evaluated? Express your answer as an exact polynomial in k .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started