In this program, you will read in an array of lines from stdin, sort them accord
ID: 3668151 • 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 answer https://www.chegg.com/homework-help/questions-and-answers/c-programming-program-read-array-lines-stdin-sort-according-strcmp-print-sorted-strings-ba-q10579349
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
Explanation / Answer
//header files
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
// global variables
#define MAX_LINE_SIZE 100
#define INITIAL_BUFFER_SIZE 16
// function prototypes
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);
// define main function
int main(){
// declare variables
char **buffer;
int *buffer_size;
int *line_size;
int s,t;
int i = 0;
char *q;
s = INITIAL_BUFFER_SIZE;
buffer_size = &s;
t = MAX_LINE_SIZE;
line_size = &t;
printf("Get lines: ");
buffer = malloc(sizeof(char *) * *buffer_size);
buffer[0] = malloc(sizeof(char) * *line_size);
while (1) {
q = gets(buffer[i]);
if (q == NULL) {
i = i - 1;
*buffer[i + 1] = '';
break;
}
i++;
buffer[i] = malloc(sizeof(char)**line_size);
printf("%d%s ", i, buffer[i]);
}
}
sample output
sh-4.3$ gcc -o main *.c
main.c: In function 'main':
main.c:46:3: warning: 'gets' is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
q = gets(buffer[i]);
^
/tmp/ccezlr9p.o: In function `main':
main.c:(.text+0x85): warning: the `gets' function is dangerous and should not be used.
sh-4.3$ main
Get lines:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.