8000 Added code and CMake file for fourth session · estimand/intro-to-cpp@094fb45 · GitHub
[go: up one dir, main page]

Skip to content

Commit 094fb45

Browse files
committed
Added code and CMake file for fourth session
1 parent d9a334f commit 094fb45

File tree

7 files changed

+200
-0
lines changed

7 files changed

+200
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <fstream>
2+
#include <iostream>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
ifstream input("01_input.txt"); // Default mode: ios::in
9+
if (!input) {
10+
cerr << "Unable to open input file" << endl;
11+
return 1;
12+
}
13+
14+
ofstream output("01_output.txt"); // Default mode: ios::out | ios::trunc
15+
16+
int n;
17+
while (input >> n)
18+
{
19+
output << n * n << endl;
20+
}
21+
22+
input.close();
23+
output.close();
24+
25+
return 0;
26+
}
27+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
0
2+
1
3+
2
4+
3
5+
4
6+
5
7+
6
8+
7
9+
8
10+
9
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
0
2+
1
3+
4
4+
9
5+
16
6+
25
7+
36
8+
49
9+
64
10+
81
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <string>
3+
4+
using namespace std;
5+
6+
int main()
7+
{
8+
string s("Test");
9+
10+
cout << "The string \"" << s << "\" has length " << s.length() << endl;
11+
12+
// Alternatively:
13+
cout << s.size() << endl;
14+
15+
s += " and more test";
16+
17+
cout << "The string \"" << s << "\" has length " << s.length() << endl;
18+
19+
return 0;
20+
}
21+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#include <iostream>
2+
#include <random>
3+
#include <string>
4+
5+
using namespace std;
6+
7+
const int N_WORDS = 15;
8+
const string WORDS[N_WORDS] = {
9+
"black", "blue", "brown", "cyan", "gray", "green", "indigo", "magenta",
10+
"orange", "pink", "purple", "red", "violet", "white", "yellow"
11+
};
12+
13+
int main()
14+
{
15+
random_device rd;
16+
mt19937 prng(rd());
17+
uniform_int_distribution<> uniform(0, N_WORDS - 1);
18+
19+
string target = WORDS[uniform(prng)];
20+
21+
string guess(target.length(), '*');
22+
string used;
23+
int n = 0;
24+
char letter;
25+
while (guess != target)
26+
{
27+
cout << "Your current guess: " << guess << endl;
28+
cout << "Letter? ";
29+
cin >> letter;
30+
31+
if (used.find(letter) != string::npos ||
32+
guess.find(letter) != string::npos)
33+
{
34+
cout << "You already used this letter. Try again!" << endl;
35+
continue;
36+
}
37+
38+
n++;
39+
40+
if (target.find(letter) == string::npos)
41+
{
42+
cout << "Sorry, this letter doesn't appear in the word." << endl;
43+
used += letter;
44+
}
45+
else
46+
{
47+
cout << "Good guess!" << endl;
48+
int position = -1;
49+
while (true)
50+
{
51+
position = target.find(letter, position + 1);
52+
if (position == string::npos) {
53+
break;
54+
}
55+
guess[position] = letter;
56+
}
57+
}
58+
}
59+
cout << "You got it! "
60+
<< "The word was \"" << target << "\". "
61+
<< "It took you " << n << " guesses." << endl;
62+
63+
return 0;
64+
}
65+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void print(const vector<int> & v)
7+
{
8+
cout << "Elements: ";
9+
for (int i = 0; i < v.size(); i++)
10+
{
11+
cout << v[i] << " "; // [] doesn't check bounds
12+
}
13+
cout << endl;
14+
}
15+
16+
int main()
17+
{
18+
vector<int> v(10);
19+
20+
print(v);
21+
cout << "Size: " << v.size() << endl;
22+
cout << "Capacity: " << v.capacity() << endl;
23+
24+
for (int i = 0; i < v.size(); i++)
25+
{
26+
v.at(i) = i * i; // .at() checks bounds
27+
}
28+
29+
print(v);
30+
31+
cout << "First: " << v.front() << endl;
32+
cout << "Last: " << v.back() << endl;
33+
34+
for (int i = v.size(); i < 20; i++)
35+
{
36+
v.push_back(i * i); // Append element at the end
37+
}
38+
39+
print(v);
40+
cout << "Size: " << v.size() << endl;
41+
cout << "Capacity: " << v.capacity() << endl;
42+
43+
v.pop_back(); // Remove the last element
44+
45+
print(v);
46+
cout << "Size: " << v.size() << endl;
47+
cout << "Capacity: " << v.capacity() << endl;
48+
49+
v.clear(); // Remove all elements
50+
51+
print(v);
52+
cout << "Size: " << v.size() << endl;
53+
cout << "Capacity: " << v.capacity() << endl;
54+
55+
return 0;
56+
}
57+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set(CMAKE_CXX_STANDARD 14)
2+
3+
add_executable(01_file_io 01_file_io.cpp)
4+
5+
add_executable(02_strings 02_strings.cpp)
6+
7+
add_executable(03_word_guess 03_word_guess.cpp)
8+
9+
add_executable(04_vectors 04_vectors.cpp)
10+

0 commit comments

Comments
 (0)
0