C programming: In this program, you will read in an array of lines from stdin, s
ID: 3669573 • 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. Four different functions need to be defined described below.
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:
1. char** IncreaseBuffer(char**buffer,int*buffer_size);
a.This function doubles the capacity of your buffer by creating
a new one and cleaning up the old one.
b. IncreaseBuffer returns the new buffer to the caller.
c. IncreaseBuffer doubles buffer_size.
d. It must call malloc and free.
e. Above the function, comment as follows:
/*
*Input:describetheparametersofthefunction
*Output:describethevariablebeingreturned
*Summary: briefsummaryoffunction
*/
2. voidPrintLines(char**lines,inttotal_lines);
a. This function prints the lines in lines.
b) Above the function, comment as follows:
/*
*Input:describetheparametersofthefunction
*Summary: briefsummaryoffunction
*/
3. intMinLineIndex(char**buffer,inti,intj);
a. This function gets the index of the minimum string, as
determined by strcmp.
b. Above the function, comment as follows:
/*
*Input:describetheparametersofthefunction
*Summary: briefsummaryoffunction
*/
4. voidSwapLines(char**buffer,inti,intj);
a. This function swaps buffer[i] and buffer[j].
b. Above the function, comment as follows:
/*
*Input:describetheparametersofthefunction
*Summary: briefsummaryoffunction
*/
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.
Explanation / Answer
#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);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.