diff --git a/.vscode/launch.json b/.vscode/launch.json index e0a4ead..71daa57 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -104,6 +104,20 @@ "MIMode": "gdb", "miDebuggerPath": "/usr/bin/gdb", "preLaunchTask": "C/C++: g++ build active file" + }, + { + "name": "lab07", + "type": "cppdbg", + "request": "launch", + "program": "${fileDirname}/${fileBasenameNoExtension}", + "args": [ + "input.txt" + ], + "cwd": "${fileDirname}", + "environment": [], + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", + "preLaunchTask": "C/C++: g++ build single active file" } ] } diff --git a/labs/maps/checkpoint1.cpp b/labs/maps/checkpoint1.cpp new file mode 100644 index 0000000..80762f6 --- /dev/null +++ b/labs/maps/checkpoint1.cpp @@ -0,0 +1,43 @@ +#include +#include +#include +using namespace std; + +int main(int argc, char* argv[]) { + if (argc != 2) { + cerr << "Usage: " << argv[0] << " inputfile" << endl; + return 1; + } + + ifstream fin(argv[1]); + if (!fin) { + cerr << "Error opening file " << argv[1] << endl; + return 1; + } + + // Use operator[] to count frequency of each integer. + map counts; + int number; + while (fin >> number) { + counts[number] = counts[number] + 1; + } + + // Find the maximum frequency. + int maxCount = 0; + for (map::iterator it = counts.begin(); it != counts.end(); ++it) { + if (it->second > maxCount) { + maxCount = it->second; + } + } + + // Print all numbers with frequency equal to maxCount. + cout << "Modes: "; + for (map::iterator it = counts.begin(); it != counts.end(); ++it) { + if (it->second == maxCount) { + cout << it->first << " "; + } + } + cout << endl; + + return 0; +} diff --git a/labs/maps/checkpoint2 b/labs/maps/checkpoint2 new file mode 100755 index 0000000..86be206 Binary files /dev/null and b/labs/maps/checkpoint2 differ diff --git a/labs/maps/checkpoint2.cpp b/labs/maps/checkpoint2.cpp new file mode 100644 index 0000000..30c7f6b --- /dev/null +++ b/labs/maps/checkpoint2.cpp @@ -0,0 +1,48 @@ +#include +#include +#include +using namespace std; + +int main(int argc, char* argv[]) { + if (argc != 2) { + cerr << "Usage: " << argv[0] << " inputfile" << endl; + return 1; + } + + ifstream fin(argv[1]); + if (!fin) { + cerr << "Error opening file " << argv[1] << endl; + return 1; + } + + // Map to count frequencies; using find/insert instead of operator[]. + map counts; + int number; + while (fin >> number) { + map::iterator it = counts.find(number); + if (it != counts.end()) { + it->second = it->second + 1; + } else { + counts.insert(pair(number, 1)); + } + } + + // Find the maximum frequency. + int maxCount = 0; + for (map::iterator it = counts.begin(); it != counts.end(); ++it) { + if (it->second > maxCount) { + maxCount = it->second; + } + } + + // Print all numbers with frequency equal to maxCount. + cout << "Modes: "; + for (map::iterator it = counts.begin(); it != counts.end(); ++it) { + if (it->second == maxCount) { + cout << it->first << " "; + } + } + cout << endl; + + return 0; +} diff --git a/labs/maps/input.txt b/labs/maps/input.txt new file mode 100644 index 0000000..4affa6e --- /dev/null +++ b/labs/maps/input.txt @@ -0,0 +1 @@ +19 83 -12 83 65 19 45 -12 45 19 45 \ No newline at end of file diff --git a/labs/maps/phonebook2 b/labs/maps/phonebook2 new file mode 100755 index 0000000..d251bf1 Binary files /dev/null and b/labs/maps/phonebook2 differ diff --git a/labs/maps/phonebook2.cpp b/labs/maps/phonebook2.cpp new file mode 100644 index 0000000..8ac98eb --- /dev/null +++ b/labs/maps/phonebook2.cpp @@ -0,0 +1,42 @@ +#include +#include +#include +using namespace std; + +// Add a number, name pair to the phonebook using a map. +void add(map& phonebook, int number, const string &name) { + map::iterator it = phonebook.find(number); + if (it != phonebook.end()) { + it->second = name; + } else { + phonebook.insert(pair(number, name)); + } +} + +// Given a phone number, determine who is calling. +void identify(const map& phonebook, int number) { + map::const_iterator it = phonebook.find(number); + if (it == phonebook.end()) + cout << "unknown caller!" << endl; + else + cout << it->second << " is calling!" << endl; +} + +int main() { + // Init + map phonebook; + + // Add + add(phonebook, 1111, "fred"); + add(phonebook, 2222, "sally"); + add(phonebook, 3333, "george"); + add(phonebook, 1234567, "alice"); + add(phonebook, 7654321, "bob"); + + // Test + identify(phonebook, 2222); + identify(phonebook, 4444); + identify(phonebook, 1234567); + + return 0; +}