For different input n, i.e., n = 1, n = 10, n = 20, write down the final value of counter for function1, function2, and function3. Explain the counter result through math calculation.
#include <iostream> using namespace std; void function1(int n){ int count = 0; for(int x = 0; x < 12; x++){ cout<<"counter:"<< count++<<endl; } } void function2(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x="<<x<<"------"<<endl; for(int i =0; i < n; i++){ cout<<"counter:"<< count++<<endl; } } } void function3(int n){ int count = 0; for(int x = 0; x < n; x++){ cout<<"--- x ="<<x<<"------"<<endl; for(int i = 0; i < x; i++){ cout<<"counter:"<< count++<<endl; } } } int main() { int input = 10; cout<<"********function 1*********"<<endl; function1(input); cout<<"********function 2*********"<<endl; function2(input); cout<<"********function 3*********"<<endl; function3(input); return 0; }
function 1:
In function 1 the value of the parameter 'n' is not used in computing the value of the counter. Therefore, no matter what value of 'n' we pass to function 1 the value of counter will be the same.
Here a loop runs 12 times and for each iteration, the value of count is incremented by 1. As the count variable starts from 0 so, the final value of the counter will be printed as 11 when we call function 1.
function 2:
In function 2 there is a nested loop and for each loop runs 'n' times. For each iteration of the outer loop the inner loop runs 'n' times and the value of the count variable is incremented accordingly.
For each iteration of the outer loop, the value of count is incremented by 'n'.
For the first iteration of the outer loop, the value of count will be incremented by 'n-1' as it starts from 0.
For the rest of the iterations, the value of count will be incremented by 'n'.So the final value of the counter will be (n-1) + n (n-1 times.. i.e for the rest of the iterations).
Therefore,
counter final value = (n-1) + (n-1)*n
or,
(n-1)*(n+1)
function 3:
In function 3 there is also a nested loop. For each iteration of the outer loop the inner loop runs 'x' times and the value of the count variable is incremented accordingly.
For each iteration of the outer loop, the value of count is incremented by 'x'.
For the first iteration of the outer loop, the value of count will be incremented by 0 as it starts from x=0.
When x=1:
the inner loop runs 0 times.
When x=2:
the inner loop runs 1 time.
.
.
.
.
When x=n:
the inner loop runs n-1 times.
Therefore,
counter final value = 1 + 2 + 3 + ........... + n-1 - 1= n(n-1)/2 - 1. ( -1 because value of count starts from 0)
NOTE: When n=1 no value of counter will be printed as the inner loop will never run.
Hence:
n=1
function 1:
final counter value = 11
function 2:
final counter value = (n-1)*(n+1) = (1-1)*(1+1) = 0
function 3:
final counter value = n(n-1)/2 - 1 = 1(1-1)/2 - 1 = 0
but here no value of the counter will be printed.
n=10
function 1:
final counter value = 11
function 2:
final counter value = (n-1)*(n+1) = (10-1)*(10+1) = 99
function 3:
final counter value = n(n-1)/2 - 1 = 10(10-1)/2 - 1 = 44
n=20
function 1:
final counter value = 11
function 2:
final counter value = (n-1)*(n+1) = (20-1)*(20+1) = 399
function 3:
final counter value = n(n-1)/2 - 1 = 20(20-1)/2 - 1 = 189
Get Answers For Free
Most questions answered within 1 hours.