Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C programming: In this program, you will read in an array of lines from stdin, s

ID: 3668142 • Letter: C

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 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

#define MAX_LINE_SIZE 100

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 lines. That is, your code needs to contain the line

#define INITIAL_BUFFER_SIZE 16

g. 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 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

*/

h. 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.

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

this file as crash_test_case.txt and turn it in on SmartSite with your code. You

do not need to fix your so that it runs successfully on your crash_test_case.txt. You can test crash_test_case.txton my code and make sure my code crashes

too.

5. Libraries to use: ctype.h, stdlib.h, stdio.h, & string.h

Explanation / Answer

file name: lines.c