Question: C programming: In this program, you will read in an array of lines from stdin, sort them according to strcmp, and print the sorted strings

C programming:

In this program, you will read in an array of lines from stdin, sort them according to

strcmp, and print the sorted strings back. Keep reading lines until EOF is read. Finally,

you will submit a test case that will break your code, causing it to crash.

1. Assume that each line is at most 100 characters. You may not assume that the

number of input lines is smaller than some number.

2. Constraints:

a. Use the macro-defined constant MAX_LINE_SIZE. Your code must contain

the line

#defineMAX_LINE_SIZE100

b. As before, you will need to use dynamic allocation to keep track of your

data. This time you will be keeping track of lines, not strings. Use the

macro-defined constant INITIAL_BUFFER_SIZE to define your initial buffer

size. Start with 16 characters. That is, your code needs to contain the line

#defineINITIAL_BUFFER_SIZE16

c. Define the following functions, comment as indicated, and call each

function from main:

i. char**IncreaseBuffer(char**buffer,int*buffer_size);

1. This function doubles the capacity of your buffer by creating

a new one and cleaning up the old one.

a. IncreaseBuffer returns the new buffer to the caller.

b. IncreaseBuffer doubles buffer_size. 2. It must call malloc and free.

3. Above the function, comment as follows:

/*

*Input:describetheparametersofthefunction

*Output:describethevariablebeingreturned

*Summary: briefsummaryoffunction

*/

ii. voidPrintLines(char**lines,inttotal_lines);

1. This function prints lines the lines in lines. 2. Above the function, comment as follows:

/*

*Input:describetheparametersofthefunction

*Summary: briefsummaryoffunction

*/

iii. intMinLineIndex(char**buffer,inti,intj);

1. This function gets the index of the minimum string, as

determined by strcmp. 2. Above the function, comment as follows:

/*

*Input:describetheparametersofthefunction

*Summary: briefsummaryoffunction

*/

iv. voidSwapLines(char**buffer,inti,intj);

1. This function swaps buffer[i] and buffer[j]. 2. Above the function, comment as follows:

/*

*Input:describetheparametersofthefunction

*Summary: briefsummaryoffunction

*/

d. Use gets from stdio.h to read each line. You may not use any other

function to read input from stdin. Do not print a prompt for user input.

When you compile using gets, you will see warnings similar to this:

sort_lines.c:Infunctionmain:

sort_lines.c:21:11:warning:implicitdeclarationoffunction

gets[Wimplicitfunctiondeclaration]

while(gets(line_buffer[lines++])){

/tmp/cclEFOX2.o:Infunction`main':

sort_lines.c:(.text+0xaa):warning:the`gets'functionis

dangerousandshouldnotbeused.

This tells you in general that gets is dangerous, and you shouldnt use it

(which is true). Were using it for educational purposes, so you may ignore

both of these warnings (but know this is a bad habit). In fact, you will

^

create a test case that breaks your program due to the fragility of gets.

6. Create a .txt file that breaks your code (e.g. causes a SegmentationFault). Save

this file as crash_test_case.txt .

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!