Skip to content

Commit dec839d

Browse files
committed
Add recursion
1 parent cdf2a05 commit dec839d

15 files changed

+174
-0
lines changed
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Recursion/.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"iostream": "cpp",
4+
"ostream": "cpp"
5+
}
6+
}

Recursion/counting.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
void printTillN(int n){
5+
//base case
6+
if(n==0){
7+
return;
8+
}
9+
//work
10+
cout<<n<<" ";
11+
//recursive call
12+
printTillN(n-1);
13+
}
14+
15+
int main(){
16+
int n;
17+
cout<<"Enter a number"<<endl;
18+
cin>>n;
19+
printTillN(n);
20+
}

Recursion/print_spelling.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
string spelling(int curr)
5+
{
6+
if (curr == 1)
7+
{
8+
return "One ";
9+
}
10+
else if (curr == 2)
11+
{
12+
return "Two ";
13+
}
14+
else if (curr == 3)
15+
{
16+
return "Three ";
17+
}
18+
else if (curr == 4)
19+
{
20+
return "Four ";
21+
}
22+
else if (curr == 5)
23+
{
24+
return "Five ";
25+
}
26+
else if (curr == 6)
27+
{
28+
return "Six ";
29+
}
30+
else if (curr == 7)
31+
{
32+
return "Seven ";
33+
}
34+
else if (curr == 8)
35+
{
36+
return "Eight ";
37+
}
38+
else if (curr == 9)
39+
{
40+
return "Nine ";
41+
}
42+
else if (curr == 0)
43+
{
44+
return "Zero ";
45+
}
46+
}
47+
48+
void printSpelling(int n)
49+
{
50+
n = n / 10;
51+
string s = "";
52+
if (n < 1)
53+
{
54+
s = s + spelling(n % 10);
55+
return;
56+
}
57+
int curr = n % 10;
58+
s = s + spelling(curr);
59+
printSpelling(n);
60+
cout << s;
61+
}
62+
63+
int main()
64+
{
65+
printSpelling(120);
66+
}

Vector/.vscode/settings.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"files.associations": {
3+
"ostream": "cpp",
4+
"iostream": "cpp"
5+
}
6+
}

Vector/capacity.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
int main(){
7+
vector<int> vec;
8+
cout<<vec.max_size()<<endl; //2305843009213693951
9+
10+
for (int i = 1; i<=5; i++){
11+
vec.push_back(i);
12+
}
13+
cout<<vec.size()<<endl; //5
14+
cout<<vec.capacity()<<endl; //8
15+
cout<<vec.empty()<<endl; //0
16+
}

Vector/capacity.exe

3.15 MB
Binary file not shown.

Vector/introduction.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
/*Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
7+
Vector elements are placed in contiguous storage so that they can be accessed and traversed using iterators. In vectors, data is inserted at the end.
8+
*/
9+
10+
11+
int main()
12+
{
13+
vector<int> g1;
14+
15+
for (int i = 1; i <= 5; i++)
16+
g1.push_back(i);
17+
18+
cout << "Output of begin and end: ";
19+
for (auto i = g1.begin(); i != g1.end(); ++i)
20+
cout << *i << " ";
21+
22+
cout << "\nOutput of cbegin and cend: ";
23+
for (auto i = g1.cbegin(); i != g1.cend(); ++i)
24+
cout << *i << " ";
25+
26+
cout << "\nOutput of rbegin and rend: ";
27+
for (auto ir = g1.rbegin(); ir != g1.rend(); ++ir)
28+
cout << *ir << " ";
29+
30+
cout << "\nOutput of crbegin and crend : ";
31+
for (auto ir = g1.crbegin(); ir != g1.crend(); ++ir)
32+
cout << *ir << " ";
33+
34+
return 0;
35+
}

Vector/introduction.exe

3.16 MB
Binary file not shown.

Vector/modifiers.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void print(vector<int> v){
7+
for (int i = 0; i < v.size(); i++)
8+
cout << v[i] << " ";
9+
cout<<endl;
10+
}
11+
12+
int main()
13+
{
14+
vector<int> v;
15+
v.assign(8, 2);
16+
cout << "The vector elements are: ";
17+
print(v); //2 2 2 2 2 2 2 2
18+
v.push_back(10);
19+
print(v); //2 2 2 2 2 2 2 2 10
20+
v.pop_back();
21+
v.insert(v.begin(), 0);
22+
print(v); //0 2 2 2 2 2 2 2 2
23+
24+
return 0;
25+
}

Vector/modifiers.exe

3.17 MB
Binary file not shown.

0 commit comments

Comments
 (0)