Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1. (30 points) Write a C program mergeSort.c that accepts three strings as parameters from the command line in main(). These three strings are names

1. (30 points) Write a C program mergeSort.c that accepts three strings as parameters from the command line in main(). These three strings are names of three files with the first two files containing each a sorted list of numbers separated by spaces. The program performs the following operation: a. open the first two files as input files for reading data in text mode b. create a new text file as the output file using the third string as the file name c. read from the first file a number and store it in n1 d. read from the second file a number and store it in n2 e. append the smaller of the two numbers to the output file f. read the next number from the input file and then if n1 was smaller, read from the first file the next number and put it in n1 if n2 was smaller, read from the second file the next number and put it in n2 g. continue with step e h. if in step f. the input file contains no more numbers, i.e. the end of the file was reached, append the remaining numbers of the other file to the output file i. close all files and terminate the program Your main function should take command line arguments. int main(int argc, char **argv) Where argc is the amount of argument passed in and argv is an array of string which contains the arguments. So argv[0] is the first argument, etc. Use feof to check the end of file; use fprintf to write to file and fscanf to read from file. There is a special case in step c. and d. above. If either the first file or the second file contains no numbers, i.e. the file is empty, append numbers from the corresponding second or first file to the output file, close all files, and terminate. Your program implements the classic merge operation of a merge sort using two smaller lists of already sorted numbers to generate a complete list of sorted numbers. Example command line: mergeSort numbers1.txt numbers2.txt sortedNumbers.txt

2. (70 points) Extend the program from project 4 to make the data in your program persistent by storing all car and rental records into separate files and using the information when the program starts up. The program must implement four new functions: void writeRentals(char *filename, RentalT *allRentals, int totalRentals); int readRentals(char *filename, RentalT *allRentals); void writeCars(char *filename, CarT *allCars, int totalCars); int readCars(char *filename, CarT *allCars); Use the command line arguments to read two text strings from the command line when the program is started. The first string must specify the file name of the rental records file, the second string specifies the file name of the car records file. The program must call the two read functions. Note, the previous call of createInventory() in main() is no longer needed as the car inventory is read from a file. Before the program terminates, it must store the rental and car records using the corresponding functions. For implementing the functions, you must use text-based file IO. When you create the file to write rental or car records to the file, begin by writing the entire record on a single line with each field separated by one space using fprintf(), then continue with the next record the same way until all records have been written to the file. You should have as many text lines as records. For example, the format for the 3 cars specified in the project description of project 4 should look as follows: 1234 VW Golf 2 66.00 2241 Ford Focus 4 45.00 3445 BMW X3 4 128.00 You may assume that none of the text strings for make or model contains spaces in their names (so %s in fscanf will work). For reading the records, do the same but use fscanf() to read numbers or strings for the individual fields of records. Assume that each record is completely stored in the file. If you reach the end of a file, then you should have a complete count of records that have been successfully read. Return that number as the number of rentals or cars. Your program must be robust to handle empty text files or missing files. Hint: Each function can be implemented by a few lines of code.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design For Mere Mortals

Authors: Michael J Hernandez

4th Edition

978-0136788041

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago