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

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

ID: 3668610 • Letter: I

Question

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.

Input Verification: Not necessary for this program. (Bad habit, but we’re simplifying the assignment).

***Note*** - Question has been asked already but no answerhttps://www.chegg.com/homework-help/questions-and-answers/c-programming-program-read-array-lines-stdin-sort-according-strcmp-print-sorted-strings-ba-q10579349 but the answer isn't complete

Assume that each line is at most 100 characters. You may not assume that the number of input lines is smaller than some number.

Constraints:

Use the macro-defined constant MAX_LINE_SIZE. Your code must contain the line
   #define MAX_LINE_SIZE 100

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

Define the following functions, comment as indicated, and call each function from main:

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

This function doubles the capacity of your buffer by creating a new one and cleaning up the old one.

IncreaseBuffer returns the new buffer to the caller.

IncreaseBuffer doubles buffer_size.

It must call malloc and free.

Above the function, comment as follows:
/*
* Input: describe the parameters of the function
* Output: describe the variable being returned
* Summary: … brief summary of function …
*/

void PrintLines(char** lines, int total_lines);

This function prints the lines in lines.

Above the function, comment as follows:
/*
* Input: describe the parameters of the function
* Summary: … brief summary of function …
*/

int MinLineIndex(char** buffer, int i, int j);

This function gets the index of the minimum string, as determined by strcmp.

Above the function, comment as follows:
/*
* Input: describe the parameters of the function
* Summary: … brief summary of function …
*/

void SwapLines(char** buffer, int i, int j);

This function swaps buffer[i] and buffer[j].

Above the function, comment as follows:
/*
* Input: describe the parameters of the function
* Summary: … brief summary of function …
*/

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: In function ‘main’:
sort_lines.c:21:11: warning: implicit declaration of function ‘gets’ [-Wimplicit-function-declaration]
    while(gets(line_buffer[lines++])) {
          ^
/tmp/cclEFOX2.o: In function `main':
sort_lines.c:(.text+0xaa): warning: the `gets' function is dangerous and should not be used.

This tells you in general that gets is dangerous, and you shouldn’t use it (which is true). We’re 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.

Create a .txt file that breaks your code (e.g. causes a Segmentation Fault). 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.txt on my code and make sure my code crashes too.

Libraries: ctype.h stdlib.h stdio.h string.h

can you also type the main function and the other 4 functions. Also the .txt file that's supposed to crash my program?

Explanation / Answer

can help you with this

#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_LINE_SIZE 100
#define INITIAL_BUFFER_SIZE 16
char** IncreaseBuffer(char** buffer, int* buffer_size);
void PrintLines(char** lines, int total_lines);
int MinLineIndex(char** buffer, int i, int j);
void SwapLines(char** buffer, int i, int j);
int main(int argc, char *argv[])
{
char **line_buffer = (char**)malloc(INITIAL_BUFFER_SIZE * sizeof(char*));
for(int i = 0; i < INITIAL_BUFFER_SIZE; i++)
{
line_buffer[i] = (char *)malloc(MAX_LINE_SIZE * sizeof(char));
}
int lines = 0;
int buffer_size = INITIAL_BUFFER_SIZE;
while(gets(line_buffer[lines++]))
{
if(lines == buffer_size - 1)
{
line_buffer = IncreaseBuffer(line_buffer, &buffer_size);
}
}
for(int i = 0; i < lines - 1; i++)
{
for(int j = 0; j < lines - i - 1; j++)
{
int min_index = MinLineIndex(line_buffer, j, j + 1);
if (min_index == j + 1) {
SwapLines(line_buffer, j, j + 1);
}
}
}
PrintLines(line_buffer, lines);
for(int i = 0; i < buffer_size; i++)
{
free(line_buffer[i]);
}
free(line_buffer);
return 0;
}
char** IncreaseBuffer(char** buffer, int* buffer_size)
{
char **new_buffer = (char **)malloc(*buffer_size * 2 * sizeof(char*));
for(int i = 0; i < *buffer_size * 2; i++)
{
new_buffer[i] = (char *)malloc(MAX_LINE_SIZE * sizeof(char));
}
for(int i = 0; i < *buffer_size; i++)
{
strcpy(new_buffer[i], buffer[i]);
}
for(int i = 0; i < *buffer_size; i++)
{
free(buffer[i]);
}
free(buffer);
*buffer_size *= 2;
return new_buffer;
}
void PrintLines(char** lines, int total_lines)
{
for(int i = 1; i < total_lines; i++)
{
printf("%s ", lines[i]);
}
}
int MinLineIndex(char** buffer, int i, int j)
{
int ret = strcmp(buffer[i], buffer[j]);
if (ret < 0)
{
return i;
}
else
{
return j;
}
}
void SwapLines(char** buffer, int i, int j)
{
char *temp = (char *)malloc((strlen(buffer[i]) + 1) * sizeof(char));
strcpy(temp, buffer[i]);
strcpy(buffer[i], buffer[j]);
strcpy(buffer[j], temp);
free(temp);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote