Can u please explain what is happening in this code in every line? Please explai
ID: 3746757 • Letter: C
Question
Can u please explain what is happening in this code in every line? Please explain every line?
#include <stdlib.h>
#include <stdio.h>
struct s_pair
{
int value;
int index;
};
int cmp_value (const void * a, const void * b) {
int diff;
diff = ((struct s_pair*)a)->value - ((struct s_pair*)b)->value;
if (diff == 0) {
diff = ((struct s_pair*)a)->index - ((struct s_pair*)b)->index;
}
return diff;
}
int cmp_index (const void * a, const void * b) {
return ((struct s_pair*)a)->index - ((struct s_pair*)b)->index;
}
int main()
{
int n, i, newn;
struct s_pair *pairs;
scanf("%d", &n);
pairs = malloc(sizeof(struct s_pair) * n);
for (i = 0; i < n; i++) {
scanf("%d", &(pairs[i].value) );
pairs[i].index = i;
}
qsort(pairs, n, sizeof(struct s_pair), cmp_value);
newn = 0;
for (i = 0; i < n; i++) {
if (newn == 0 || pairs[i].value != pairs[newn - 1].value) {
pairs[newn] = pairs[i];
newn++;
}
}
n = newn;
qsort(pairs, n, sizeof(struct s_pair), cmp_index);
printf("%d ", n);
for (i = 0; i < n; i++) {
printf("%d ", pairs[i].value);
}
free(pairs);
return 0;
}
Explanation / Answer
#include <stdlib.h>
#include <stdio.h>
struct s_pair
{
int value;
int index;
};
int cmp_value (const void * a, const void * b) { //comparator function for qsort function for value
int diff;
diff = ((struct s_pair*)a)->value - ((struct s_pair*)b)->value;
if (diff == 0) {
diff = ((struct s_pair*)a)->index - ((struct s_pair*)b)->index;
}
return diff;
}
int cmp_index (const void * a, const void * b) { //comparator function for qsort function for index
return ((struct s_pair*)a)->index - ((struct s_pair*)b)->index;
}
int main()
{
int n, i, newn; //takes 3 integer variable
struct s_pair *pairs; //takes a pointer type variable pairs for s_pair structure
scanf("%d", &n); //takes input
pairs = malloc(sizeof(struct s_pair) * n); //allocates space with pairs marking to the base address
for (i = 0; i < n; i++) {
scanf("%d", &(pairs[i].value) ); //takes value for every allocated space in "value" of pairs variable
pairs[i].index = i; //assigns value of "index" of pairs variable with i
}
qsort(pairs, n, sizeof(struct s_pair), cmp_value); //calls qsort function and sorts the array "pairs" on the basis of value
newn = 0; //initialize newn to 0
for (i = 0; i < n; i++) {
if (newn == 0 || pairs[i].value != pairs[newn - 1].value) { //checks if newn equals to 0 or value stored in pairs array at each index is not equal to value stored in pairs array at previous index
pairs[newn] = pairs[i]; //if condition is true, the value at current index is placed to its previous index
newn++; //value of newn is incremented
}
}
n = newn; //n is assigned newn value
qsort(pairs, n, sizeof(struct s_pair), cmp_index); //again the "pairs" array is sorted on the basis of index hence old array is revived again
printf("%d ", n); //final value of n is printed
for (i = 0; i < n; i++) {
printf("%d ", pairs[i].value); //array "pairs" is printed
}
free(pairs); //memory is deallocated
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.