1. Homepage
  2. Programming
  3. COMP 322 Introduction to C++ - Assignment 1: Exploring Functions and Files

COMP 322 Introduction to C++ - Assignment 1: Exploring Functions and Files

Engage in a Conversation
CanadaMcgill UniversityCOMP 322COMP322Introduction to C++Exploring Functions and FilesC++

Assignment 1: Exploring Functions and Files.

Due date: 10 February 2023, 11:59 PM. Before you start: ● Research for similar problems is tolerated. However, your submission should reflect individual work and personal effort. CourseNana.COM

● Some of the topics may not be covered in class due to our limited time. You are encouraged to find answers online. You can also reach out to the TAs for guidance. CourseNana.COM

● Please submit your assignment before the due date to avoid penalties or worse risking your assignment being rejected. CourseNana.COM

● Submit one file called assignment1_yourIDNumber.cpp (assignment1_followed by your McGill ID number) containing all the functions together with their implementations. It will also contain the main() function that runs everything. CourseNana.COM

Make sure your code is clear and readable. Readability of your code as well as the quality of your comments will be graded. ● No submission by email. Submit your work to mycourse. CourseNana.COM

● If your code does not compile it will not be graded. CourseNana.COM

● Be happy when working on your assignment, because a happy software developer has more inspiration than a sad one :). CourseNana.COM

Software developers use multiple tools to help them achieve their tasks in the most efficient way possible. One of these tools is a “diff tool” used to compare files. This is especially useful when you want to know whether a code file that you were working on had been changed by another developer. All versioning control systems (such as Git) have some kind of diff utility embedded in them. CourseNana.COM

In this first assignment we will be creating our own diff tool with your beloved programming language C++. CourseNana.COM

For the sake of simplicity we will limit the comparison to only text files (no pictures, no binaries etc.). CourseNana.COM

Question 1 [10 points]

In this first step of the assignment we will be starting very simple. Let’s write a function that compares 2 strings and returns true if they were identical, false otherwise. The signature of this function should be: CourseNana.COM

bool word_diff(std::string word1, std::string word2);

For example: CourseNana.COM

std::string str1 = "Hello World";
std::string str2 = "hEllO World";
std::string str3 = "World";
std::string str4 = "Hello World";
bool result = word_diff(str1, str2); // False
bool result = word_diff(str1, str3); // False
bool result = word_diff(str1, str4); // True

Question 2 [20 points]

Now let’s follow a classical approach to compare the content of 2 files. We should open each file, read their content word by word and compare them until a first mismatch occurs. CourseNana.COM

Let’s implement a function called classical_file_diff that takes 2 arguments each of which is a file name and returns a boolean indicating whether the 2 files are identical or not. The signature of this function should be: CourseNana.COM

bool classical_file_diff(std::string file1, std::string file2); CourseNana.COM

This function must use the helper function from Question 1 that compares 2 words. CourseNana.COM

Example: CourseNana.COM

Create a folder within the same directory where your executable resides and name it “txt_folder”. In it create 2 files: file1.txt and file2.txt. In file1.txt copy/paste the following lines: CourseNana.COM

My dear C++ class.
I hope that you enjoy this assignment.

file2.txt copy/paste the following lines: CourseNana.COM

My dear C++ class.
I hope that you like this assignment.

The following code should yield false in the variable result: CourseNana.COM

std::string file1 = "./txt_folder/file1.txt";
std::string file2 = "./txt_folder/file2.txt";
bool result = classical_file_diff(file1, file2); // False

Please note that you may need to modify the slashes in the path name according to the expectations of your operating system. CourseNana.COM

Question 3 [10 points]

Now let’s push it a bit further and try to optimise. Comparing word by word is time consuming. A better strategy would be to take advantage of hashing functions. A hash function is a function that maps data of arbitrary size to fixed-size value as per wikipedia: https://en.wikipedia.org/wiki/Hash_function. CourseNana.COM

Implementing a robust hash function is a tricky thing and out of scope for this assignment. To make things easier we’ll take advantage of C++ default hash object used by the standard library std::hash. CourseNana.COM

You are invited to research std::hash in order to understand how it works. In its basic form std::hash can take a string as input and returns its hashed value. CourseNana.COM

Example: CourseNana.COM

std::string mystr = "I love this assignment";
std::size_t h1 = std::hash<std::string>{}(mystr);
std::cout << h1 << std::endl;

Don’t mind much of the weird syntax required for std::hash, just use it in a similar fashion to this example. CourseNana.COM

Now let’s write a function called hash_it that takes a string as an input parameter and returns the hashed value. The signature of this function must be: std::size_t hash_it (std::string someString); CourseNana.COM

For example: std::string mystr = "I love this assignment"; std::size_t h1 = hash_it (mystr); std::cout << h1 << std::endl; CourseNana.COM

Question 4 [20 points]

Let’s take advantage of the hashing function that you implemented in the previous question in order to use it in a smart way to check whether 2 files are identical or not without comparing the content of the 2 files word by word. CourseNana.COM

Write a function called enhanced_file_diff that takes 2 filenames as input parameters and returns true if the content of the 2 files were identical, false otherwise. Keep in mind that this time no content comparison should be used in your implementation (no word to word comparison is allowed). The signature of this function should be: bool enhanced_file_diff(std::string file1, std::string file2); CourseNana.COM

Example: Use the same files that you have created previously and run the following code: CourseNana.COM

std::string file1 = "./txt_folder/file1.txt";
std::string file2 = "./txt_folder/file2.txt";
bool result = enhanced_file_diff(file1, file2); // False

Modify the content on file2.txt to match exactly what’s in your file1.txt and rerun the code. It should yield true now. CourseNana.COM

Question 5 [20 points]

Up until now, we were able to tell whether 2 files were identical or not. For this question we need to provide more information about where the mismatch is happening. Write a recursive function called list_mismatched_lines that takes 2 filenames as input arguments and displays to the screen all mismatched lines in those files. CourseNana.COM

This function should use hashing techniques and shall not compare strings to detect mismatch. CourseNana.COM

The signature of this function should be: CourseNana.COM

void list_mismatched_lines(std::string file1, std::string file2);

Example: Running the following line of code: list_mismatched_lines(file1, file2); CourseNana.COM

Should print to the screen the mismatched lines only, from both files. The following output should be seen on the screen: file1.txt: I hope that you enjoy this assignment. file2.txt: I hope that you like this assignment. CourseNana.COM

Please note that if you have multiple mismatching lines, all of them should be printed out to the screen. CourseNana.COM

Question 6 [20 points]

Now let’s be more precise and write a function that pinpoints the mismatched words only together with the line number where the mismatch occurred. Here also you should use recursive functions and you should use hashing techniques (no one on one comparison of strings from the 2 files). Your function signature should be: void list_mismatched_words(std::string file1, std::string file2); CourseNana.COM

By calling this function on your same previous files, the output should be: file1.txt: enjoy (line 2) file2.txt: like (line 2) CourseNana.COM

Please note that if you have multiple mismatching words, all of them should be printed out to the screen. Please use the following main() function for testing: CourseNana.COM

int main()

{
// Q1
std::string str1 = "Hello World";
std::string str2 = "hEllO World";
std::string str3 = "World";
std::string str4 = "Hello World";
bool result = word_diff(str1, str2); // False
bool result = word_diff(str1, str3); // False
bool result = word_diff(str1, str4); // True
// Q2
std::string file1 = "./txt_folder/file1.txt";
std::string file2 = "./txt_folder/file2.txt";
bool result = classical_file_diff(file1, file2); // False
// Q3
std::string mystr = "I love this assignment";
std::size_t h1 = hash_it (mystr);
std::cout << h1 << std::endl;
// Q4
bool result = enhanced_file_diff(file1, file2); // False
// Q5
list_mismatch(file1, file2); // This should print to the screen the
mismatched lines
// Q6
list_mismatched_words(file1, file2); // This should print to the screen
the mismatched words

}

Do not modify the main() function and do not implement any logic in it. All your code should be provided within the provided functions. main() function will be used for testing only. You may only add printing statements (cout) for the purpose of adding more clarity by printing out some desired results. CourseNana.COM

Get in Touch with Our Experts

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Canada代写,Mcgill University代写,COMP 322代写,COMP322代写,Introduction to C++代写,Exploring Functions and Files代写,C++代写,Canada代编,Mcgill University代编,COMP 322代编,COMP322代编,Introduction to C++代编,Exploring Functions and Files代编,C++代编,Canada代考,Mcgill University代考,COMP 322代考,COMP322代考,Introduction to C++代考,Exploring Functions and Files代考,C++代考,Canadahelp,Mcgill Universityhelp,COMP 322help,COMP322help,Introduction to C++help,Exploring Functions and Fileshelp,C++help,Canada作业代写,Mcgill University作业代写,COMP 322作业代写,COMP322作业代写,Introduction to C++作业代写,Exploring Functions and Files作业代写,C++作业代写,Canada编程代写,Mcgill University编程代写,COMP 322编程代写,COMP322编程代写,Introduction to C++编程代写,Exploring Functions and Files编程代写,C++编程代写,Canadaprogramming help,Mcgill Universityprogramming help,COMP 322programming help,COMP322programming help,Introduction to C++programming help,Exploring Functions and Filesprogramming help,C++programming help,Canadaassignment help,Mcgill Universityassignment help,COMP 322assignment help,COMP322assignment help,Introduction to C++assignment help,Exploring Functions and Filesassignment help,C++assignment help,Canadasolution,Mcgill Universitysolution,COMP 322solution,COMP322solution,Introduction to C++solution,Exploring Functions and Filessolution,C++solution,