+1 678 648 4277 
Table Of Contents
  • Writing a Code Fragment to Perform a Specified Task in C

Writing a Code Fragment to Perform a Specified Task in C

Write a code fragment to perform the given task; please remember that it's a fragment, therefore, you need not to show the #include statements or main(), but you must write a proper C code that can be compiled and run correctly.
Writing a Function To Sort a Multidimensional Integer Array in C
int* sortRemoveDuplicated(int input[5][2], int *num_unique_values); to sort a multi-dimensional integer array input of a fixed size 5 by 2, remove duplicated values, update num_unique_values and return the unique sorted values as an 1D array, which is dynamically allocated in the function.
Example
intinput[5][2] = { {2, 3}, {2, 3}, {2, 3}, {1, 2}, {5, 4} };
intnum_unique_values = 0;
int* output = sortRemoveDuplicated(input, &num_unique_values);
for (inti = 0; i
It should print 1 2 3 4 5.
int* sortRemoveDuplicated(int input[5][2], int *num_unique_values) {
/// Write your code here
Solution:
int*sortRemoveDuplicated(int input[5][2],int*num_unique_values){
// Find the highest number in the list in the 2D array
inthighestNumber=0;
for(int r =0; r <5; r++){
for(int c =0; c <2;c++){
if(input[r][c]>highestNumber)
highestNumber= input[r][c];
}
}
// Create an array that keeps track the frequencies of each number
int*frequencies =(int*)malloc(sizeof(int)*(highestNumber+1));
intnumUnique=0;
for(inti=0;i<=highestNumber;i++)
        frequencies[i]=0;
for(int r =0; r <5; r++){
for(int c =0; c <2;c++){
int number = input[r][c];
if(frequencies[number]==0)
numUnique++;
            frequencies[number]++;
}
}
// Create the sorted unique numbers
int*sortedNumbers=(int*)malloc(sizeof(int)*numUnique);
int j =0;
for(inti=0;i<=highestNumber;i++){
if(frequencies[i]>0)
sortedNumbers[j++]=i;
}
*num_unique_values=numUnique;
    free(frequencies);
returnsortedNumbers;
}
Writing a Function That Reads and Stores a Line Into a C String
2.Complete the below function bool readLine(FILE* fp, char** line, int* array_size); that reads a line from a file given by the file pointer fp, stores the line into the C string *line, and updates the array size array_size if the array size of *line has changed. Your implementation should meet the following requirements:
• If *line is NULL, your function should use malloc to allocate enough memory.
• If *line is not NULL and *array_size is not large enough to store the line, you should use realloc to resize the C string.
• *array_size should be updated to reflect the array size of the character array.
• The C string *linemust be null-terminated.
boolreadLine(FILE* fp, char** line, int* array_size) {
// temp buffer, assuming a line has at most 1024 characters
chartemp[1024];
if (fgets(temp, 1024, fp)) {
// TODO: temp contains the line, your tasks are:
// 1. Handle the dynamic memory allocation for *line,
// 2. Update *array_size if necessary
// 3. Copy values from temp to *line
// Note: line is a pointer to a c-string, when you
// allocate memory, think carefully which
// variable you need to use
// Write your code below
returntrue;
    }
returnfalse;
}
Solution:
boolreadLine(FILE*fp,char** line,int*array_size){
// temp buffer, assuming a line has at most 1024 characters
chartemp[1024];
if(fgets(temp,1024,fp)){
// TODO: temp contains the line, your tasks are:
// 1. Handle the dynamic memory allocation for *line,
// 2. Update *array_size if necessary
// 3. Copy values from temp to *line
// Note: line is a pointer to a c-string, when you
// allocate memory, think carefully which
// variable you need to use
// Write your code below
if(*line == NULL)
*line = malloc(sizeof(char)*1025);
elseif(*array_size
*line =realloc(*line,strlen(temp)+1);
*line[0]='\0';
strcpy(*line, temp);
*array_size=strlen(*line);
returntrue;
}
returnfalse;
}