Skip to content

Commit ac22b3f

Browse files
committed
local
1 parent 3ef8756 commit ac22b3f

File tree

7 files changed

+135
-1
lines changed

7 files changed

+135
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
,moustafa,moustafa-Aspire-VN7-793G,07.06.2020 11:06,file:///home/moustafa/.config/libreoffice/4;

18-Programming-4kids/10_homework_04_answer.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using namespace std;
33

44
int main() {
5-
int n, mn[3];
5+
int n, tmp, mn[3];
66

77
cin >> n;
88
for (int i = 0; i < n; i++) {
@@ -24,8 +24,28 @@ int main() {
2424
}
2525
}
2626

27+
// let's order the first 3 values. We can do in several ways
28+
// Find maximum position
29+
int mx_pos = 0;
30+
for (int j = 1; j < 3; ++j) {
31+
if (mn[mx_pos] < mn[j])
32+
mx_pos = j;
33+
}
34+
// swap max with last
35+
tmp = mn[2];
36+
mn[2] = mn[mx_pos];
37+
mn[mx_pos] = tmp;
38+
39+
// Swap first 2 elements if needed
40+
if (mn[0] > mn[1]) {
41+
tmp = mn[0];
42+
mn[0] = mn[1];
43+
mn[1] = tmp;
44+
}
45+
2746
for (int i = 0; i < 3; i++)
2847
cout << mn[i] << " "; // not ordered
2948

49+
3050
return 0;
3151
}

18-Programming-4kids/15_1.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int factorial(int n) {
5+
int res = 1;
6+
7+
for (int i = 2; i <= n; ++i)
8+
res *= i;
9+
10+
return res;
11+
}
12+
13+
int main() {
14+
cout << factorial(3) << "\n"; // 1 * 2 * 3
15+
cout << factorial(4) << "\n"; // 1 * 2 * 3 * 4
16+
17+
cout << factorial(5) << "\n"; // 1 * 2 * 3 * 4 * 5 = 120
18+
// factorial(4) * 5 = 120
19+
20+
cout << factorial(6) << "\n"; // 1 * 2 * 3 * 4 * 5 * 6 = 720
21+
// factorial(5) * 6 = 720
22+
// factorial(4) * 5 * 6 = 720
23+
// factorial(3)*4* 5 * 6 = 720
24+
25+
return 0;
26+
}
27+

18-Programming-4kids/15_2.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int factorial1() {
5+
return 1; // base case. No subproblems
6+
}
7+
8+
int factorial2() {
9+
return factorial1() * 2;
10+
}
11+
12+
int factorial3() {
13+
return factorial2() * 3;
14+
}
15+
16+
int factorial4() {
17+
return factorial3() * 4;
18+
}
19+
20+
int factorial5() {
21+
return factorial4() * 5;
22+
}
23+
24+
int factorial6() {
25+
return factorial5() * 6;
26+
}
27+
28+
int main() {
29+
cout << factorial6() << "\n";
30+
return 0;
31+
}
32+

18-Programming-4kids/15_3.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
int factorial(int n) {
5+
cout<<"Function Call: factorial: n="<<n<<"\n";
6+
7+
if (n == 1)
8+
return 1;
9+
return factorial(n-1) * n;
10+
}
11+
12+
int main() {
13+
cout << factorial(6) << "\n";
14+
return 0;
15+
}
16+

18-Programming-4kids/15_4.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void print_triangle(int levels) {
5+
if (levels == 0)
6+
return;
7+
8+
for (int i = 0; i < levels; ++i)
9+
cout << "*";
10+
cout << "\n";
11+
12+
print_triangle(levels - 1);
13+
}
14+
15+
int main() {
16+
print_triangle(5);
17+
return 0;
18+
}
19+

18-Programming-4kids/15_5.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include<iostream>
2+
using namespace std;
3+
4+
void print_triangle(int levels) {
5+
if (levels == 0)
6+
return;
7+
8+
print_triangle(levels - 1);
9+
10+
for (int i = 0; i < levels; ++i)
11+
cout << "*";
12+
cout << "\n";
13+
}
14+
15+
int main() {
16+
print_triangle(5);
17+
return 0;
18+
}
19+

0 commit comments

Comments
 (0)