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;
}
Related Blogs
What Are The Advantages Of C Over Java?C and Java are some of the most commonly used programming languages today. Each has unique features that make it appropriate for the disciplines in which it is applied. But C has been found to be more prevalent than Java when it comes to application development...
C Programming Homework Help – The Best of the Best in the UKThe C programming language is one of the most common languages and the most preferred by the majority of institutions when it comes to introducing learners to programming. Most of the high-level programming languages we have today are a lot...
How to Complete Your C Programming Homework with Linux?Almost everyone is familiar with Windows, but a lot of courses use Linux and so you should be prepared to program under Linux. Most programming is similar to Windows but the biggest difference is fork since that has no equivalent on Windows. Whe...