COSC 4377 - Introduction to Computer Networks

Spring 2012

MW 1:00-2:30pm at PGH347

InstructorOmprakash Gnawali

Homework 0 : Hello World!

Due: January 18, 2012

Write a C or C++ program (lets call it combine-files) that accepts three command line arguments: file1, file2, and file3. The program will merge the contents of file1 and file2 and outputs the merged content to file3. Here is how the merge works. The program reads the content of file1 and file2. File1 and file2 are text files with one integer per line. If the sum of all the integers in file1 is smaller than the sum of all the integers in file2, the program should output the content of file1 followed by the content of file2 to file3. If the sum of all the integers in file2 is smaller than the sum of all the integers in file1, the program should output the content of file2 followed by the content of file1 to file3. The following example will make it more clear what we are trying to do.

Lets say, this is the content of the file called myfilea.txt:

10
20
10
10
and here is the content of the file anotherfile.txt:
5
3
2
12
8
10
1
1
4
If we run your program like this:
./combine-files myfilea.txt anotherfile.txt myoutput.txt
your program should first read myfilea.txt, and compute the sum of all the integers in that file. For this file, the sum is 50. It will read the content of the second file (anotherfile.txt) and compute the sum of all the integers in that file, which is 46. Then, the program will create a file called myoutput.txt and output the following content to that file:
5
3
2
12
8
10
1
1
4
10
20
10
10
You will notice that the content of the output file starts with the content of the second input file followed by the content of the first input file. This is because the sum of all the numbers in the second file (46) is smaller than the sum of all the numbers in the first file (50).

There are many ways to write this program and we do not require a specific style or algorithm as long as you write your program in C or C++. We do require that you read the input files just once. That means your program should not open a file, read the content, close it, open the same file later, read it, etc. Then you might ask, what to do if you need the content of a file after closing it? In that case, you might consider storing the content in memory (array, etc.) so that you have the content without having to re-read from the file.

HW0 Submission

You should submit a single file, the source code. No need to submit the binary. Please submit your source code through Blackboard. Please include a few lines of comments at the top of your file describing how to compile your file and if there is anything unusual about your implementation (e.g., it does not work with certain inputs).