In: Computer Science
Run the time complexity.cpp, which you can find at directory Code/Chapter 1. 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; }
OUTPUT:
********function 1*********
counter:0
counter:1
counter:2
counter:3
counter:4
counter:5
counter:6
counter:7
counter:8
counter:9
counter:10
counter:11
Explanation:
count value assigned to 0. In for loop, the condition is x<12
and is initialized to 0 .so the counter is printed 12 times.
count++ is a post increment operator it will count after the
current count value printed
********function 2*********
--- x=0------
counter:0
counter:1
counter:2
counter:3
counter:4
counter:5
counter:6
counter:7
counter:8
counter:9
Explanation:
The first for loop variable x initialized to 0 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n the loops prints count value upto n.
--- x=1------
counter:10
counter:11
counter:12
counter:13
counter:14
counter:15
counter:16
counter:17
counter:18
counter:19
Explanation:
The first for loop variable x initialized to 1 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=2------
counter:20
counter:21
counter:22
counter:23
counter:24
counter:25
counter:26
counter:27
counter:28
counter:29
Explanation:
The first for loop variable x initialized to 2 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=3------
counter:30
counter:31
counter:32
counter:33
counter:34
counter:35
counter:36
counter:37
counter:38
counter:39
Explanation:
The first for loop variable x initialized to 3 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=4------
counter:40
counter:41
counter:42
counter:43
counter:44
counter:45
counter:46
counter:47
counter:48
counter:49
Explanation:
The first for loop variable x initialized to 4 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=5------
counter:50
counter:51
counter:52
counter:53
counter:54
counter:55
counter:56
counter:57
counter:58
counter:59
Explanation:
The first for loop variable x initialized to 5 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=6------
counter:60
counter:61
counter:62
counter:63
counter:64
counter:65
counter:66
counter:67
counter:68
counter:69
Explanation:
The first for loop variable x initialized to 6 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=7------
counter:70
counter:71
counter:72
counter:73
counter:74
counter:75
counter:76
counter:77
counter:78
counter:79
Explanation:
The first for loop variable x initialized to 7 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=8------
counter:80
counter:81
counter:82
counter:83
counter:84
counter:85
counter:86
counter:87
counter:88
counter:89
Explanation:
The first for loop variable x initialized to 8 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
--- x=9------
counter:90
counter:91
counter:92
counter:93
counter:94
counter:95
counter:96
counter:97
counter:98
counter:99
Explanation:
The first for loop variable x initialized to 9 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<n. the loops prints count value upto n.
********function 3*********
--- x =0------
Explanation:
The first for loop variable x initialized to 0 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<0) the condition FALSE and the inside loop will never executed.
--- x =1------
counter:0
Explanation:
The first for loop variable x initialized to 1
and the condition x<n .inside the loop there is
another loop has variable i
initialized to 0 and the condition
i<x(0<1) the condition TRUE
and the inside loop will executes upto x.
--- x =2------
counter:1
counter:2
Explanation:
The first for loop variable x initialized to 2 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<2) the condition TRUE and the inside loop will executes upto x.
--- x =3------
counter:3
counter:4
counter:5
Explanation:
The first for loop variable x initialized to 3 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<3) the condition TRUE and the inside loop will executes upto x.
--- x =4------
counter:6
counter:7
counter:8
counter:9
Explanation:
The first for loop variable x initialized to 4 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<4) the condition TRUE and the inside loop will executes upto x.
--- x =5------
counter:10
counter:11
counter:12
counter:13
counter:14
Explanation:
The first for loop variable x initialized to 5 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<5) the condition TRUE and the inside loop will executes upto x.
--- x =6------
counter:15
counter:16
counter:17
counter:18
counter:19
counter:20
Explanation:
The first for loop variable x initialized to 6 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<6) the condition TRUE and the inside loop will executes upto x.
--- x =7------
counter:21
counter:22
counter:23
counter:24
counter:25
counter:26
counter:27
Explanation:
The first for loop variable x initialized to 7 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<7) the condition TRUE and the inside loop will executes upto x.
--- x =8------
counter:28
counter:29
counter:30
counter:31
counter:32
counter:33
counter:34
counter:35
Explanation:
The first for loop variable x initialized to 8 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<8) the condition TRUE and the inside loop will executes upto x.
--- x =9------
counter:36
counter:37
counter:38
counter:39
counter:40
counter:41
counter:42
counter:43
counter:44
Explanation:
The first for loop variable x initialized to 9 and the condition x<n .inside the loop there is another loop has variable i initialized to 0 and the condition i<x(0<9) the condition TRUE and the inside loop will executes upto x and finally the loop terminates.