Know how to process command line arguments

Assignment Help Basic Computer Science
Reference no: EM13861092

Through this programming assignment, the students will learn to do the following:

Know how to process command line arguments.

Perform basic file I/O.

Use structs, pointers, and strings.

Use dynamic memory.

This assignment asks you to sort the lines of an input file (or from standard input) and print the sorted lines to an output file (or standard output). Your program, called bstsort (binary search tree sort), will take the following command line arguments:

% bstsort [-c] [-o output_file_name] [input_file_name]

If -c is present, the program needs to compare the strings case sensitive; otherwise, it's case insensitive. If the output_file_name
is given with the -o option, the program will output the sorted lines to the given output file; otherwise, the output shall be the standard output. Similarly, if the input_file_name is given, the program will read from the input file; otherwise, the input will be from the standard input. You must use getopt() to parse the command line arguments to determine the cases.

In addition to parsing and processing the command line arguments, your program needs to do the following:

You need to construct a binary search tree as you read from input. A binary search tree is a binary tree. Each node can have at most two child nodes (one on the left and one on the right), both or either one can be empty. If a child node exists, it's the root of a binary search tree (we call subtree). Each node contains a key (in our case, it's a string). If the left subtree of a node exists, it contains only nodes with keys less than the node's key. If the right subtree of a node exists, it contains only nodes with keys greater than the node's key. In case of duplicate keys, you can use a counter scheme (i.e., using an integer to indicate the number of duplicate keys the node is currently representing). You can look up binary search tree on the web or in your Data Structure textbook. Note that you do not need to balance the binary search tree (that is, you can ignore all those rotation operations) in this assignment.

Initially the tree is empty (that is, the root is null). The program reads from the input file (or stdin) one line at a time; If the line is not an empty line, it should create a tree node that stores (a copy of) the string (you shall remove the trailing line feed) and then insert the tree node to the binary search tree. Pay attention to situations that a duplicate is found, in which case, you ought to simply increment the count and remove the new node, instead of inserting the duplicate node.

You must develop two string comparison functions, one for case sensitive and the other for case insensitive. You must not use strcmp() and strcasecmp() functions provided by the C library. You must implement your own version.

Once the program has read all the input (when EOF is returned), the program then performs an in-order traversal of the binary search tree to print out all the strings one line at a time to the output file or stdout.

Before the program ends, it must reclaim the tree! You can do this by performing a post-order traversal, i.e., reclaiming the children nodes before reclaiming the node itself. Make sure you also reclaim the memory occupied by the string as well.

It is required that you use getopt for processing command line and use malloc/free functions for dynamically allocating and deallocating nodes and the buffers for the strings. It is required that you implement your own string comparison functions instead of using the corresponding libc functions.

Here's an example:

bash$ cat myfile
bob is working.
david is a new hire.
alice is bob's boss.
charles doesn't like bob.
 
bash$ ./bstsort myfile
alice is bob's boss.
bob is working.
charles doesn't like bob.
david is a new hire.

One can also use './bstsort -o sortedfile < myfile' to take input from stdin (in this case, it's replaced by a file by the shell) and output the sorted lines in the output file (called sortedfile in the example).

Please submit your work through moodle as one zip file. Follow the instructions below carefully (to avoid unnecessary loss of grade):

To start, first create a directory for this homework and name it firstname-lastname-assignment1 (of course, you'd use your real name here). You should place the source code and the Makefile in the directory. One should be able to create the executable by simply 'make'. The Makefile should also contain a 'clean' target for cleaning up the directory (removing all temporary files, object files and executable files). Make sure you don't include intermediate files: *.o, executables, *~, etc., in your submission. (There'll be a penalty for including unnecessary intermediate files).

Please make sure you submit homework before the deadline. Late submission will incur penalties as specified in the syllabus.

Reference no: EM13861092

Questions Cloud

Prompts the user for a hexadecimal digit : Write a MIPS program that prompts the user for a hexadecimal digit and then displays the decimal equivalent multiplied by 2. For example, the program displays 10 for hex '5', 20 for 'A', etc. If the user enters an invalid value, the program should di..
A program that will prompt the user for a date and a time : It's to be written in C++. You will be writing a program that will prompt the user for a date and a time. For the date, the user will be asked to enter in three integers (month, day, year). There will be four functions related to the date:
Sample the dribbling to extract its frequency : Consider a basketball being dribbled. If the height of the basketball can be described by a sine wave of maximum height 2h, average height h and minimum height 0, and the ball hits the ground once per second, how fast would a video camera have to sam..
What are your thoughts about your results : What are your thoughts about your results?  What are you feelings towards this assignment?  How would you improve it? Submit this with your assignment.
Know how to process command line arguments : Through this programming assignment, the students will learn to do the following: Know how to process command line arguments. Perform basic file I/O.
Theater seating revenue with input validation : Theater Seating Revenue with Input Validation: A dramatic theater has three seating sections, and it charges the following prices for tickets in each section:  Section A seats cost $20 each
Discuss the differences between external and internal threat : Discuss the differences between external and internal threats as they relate to network security. Which threat is most difficult to defend against and why? What protections would be used specifically related to internal threats? Why is it important f..
Display to the screen the first 20 numbers : Design an algorithm that will display to the screen the first 20 numbers, with their squares and cubes, as follows: Number Square Cube
The ph of an aqueous solution is a measure : Microsoft Word - HW#9.docx In chemistry, the pH of an aqueous solution is a measure of its acidity. A solution with a pH of 7 is said to be neutral, a solution with a pH greater than 7 is basic, and a solution with a pH less than 7 is acidic.

Reviews

Write a Review

Basic Computer Science Questions & Answers

  What would be the financial advantage of a malware designer

What would be the financial advantage of a malware designer to create lots of different malicious code instances that all exploit the same vulnerability yet have different malware signatures?

  How do you create a 4d array of int in c++

How do you create a 4D array of int in C++

  Calculate the net profit for all the products

Given the list of all the product prices and wholesale prices as well as a list of all the items sold for each product calculate the net profit for all the products.

  Netstack college has received a grant to wire all

Netstack College has received a grant to wire all of its six buildings for 10/100 Ethernet. Four of the buildings contain classrooms and laboratories and will need to support 250 network connections each. One building is for administration and ..

  Simplify boolean expressions to a minimum number of literals

Simplify the subsequent Boolean expressions to a minimum number of literals (SHOW WORK):

  How many days they worked and the total pay

Must have a input box to ask the user how many days they worked and the total pay must be in the form of currency.

  Specify the most significant advantages and disadvantages

Specify the most significant advantages and disadvantages that could be realized by the organization in adopting a server virtualization infrastructure. Prepare a plan for implementing Hyper-V (or an alternate solution, such as VMware) as a solution ..

  Notes on lyt2

seabreeze is a fictional, midsize city in southern California. Once prosperous due to its enviable location on the coastline, the city has undergone a change in fortune.

  The ceo throws a billion dollar party

The CEO throws a billion dollar party

  Create a formula in the last cell on the last row to average

Create a formula in the last cell on the last row to average the Salary column. In the Formula dialog box, remove the suggested function, but leave the =, and paste an Average function, indicating that the average should include the numbers Above.

  Convert decimal mumbers into bit binary number

Convert the following decimal mumbers into 8-bit binary numbers a required for 2's complement math, and perform the indicated operations.

  Explain how the method represents knowledge

Select a method for knowledge representation and reasoning that we have not covered in lectures and write 1{2 pages addressing the following: briefly describe how the method represents knowledge and include an example; briefly describe the inferenc..

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd