 # Can any 1 help me debug the code for TOURMAP

Here is the code :
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

int compare(const void * a, const void * b)
{
return strcmp(a, b);
}
struct node {
char name;
int index;
};
/* 1.Sort the Array cities (which was copy of A) and Array B2 (which was copy of B).
2.Do binary search in sorted array B (called B2) to find a city in A which is not present in B2.
3.If the index found is i, then print "A[i] B[i] cost[i]". 4.Do this for n - 2 times : For each B[indx], find it in sorted array cities and use its index(called indx) to print A[indx] B[indx] cost[indx] and repeat the same
/
main()
{
int t;
scanf("%d", &t);
int n;
int c;
struct node cities;// stored sorted array A with original index
char A;
char B;
char B2;// stored sorted array B to find(using binary search) first city which is present in A but not in B
int cost;
int total_cost;
int i, j, k;
while (t–) {
scanf("%d", &n);
if (n == 1) {
printf(“0\$\n”);
} else {
/
for (i = 0; i < n - 1; i++) {
scanf("%s", A[i]);
cities[i].index = i;
strcpy(cities[i].name, A[i]);
scanf("%s", B[i]);
strcpy(B2[i], B[i]);
while (isspace (c = getchar()))
;
cost[i] = cost[i] * 10 + c - ‘0’;
while (isdigit(c = getchar()))
cost[i] = cost[i] * 10 + c - ‘0’;
}
/
/
Shell Sort starts for array “cities” and “B2”
note : original index in array “cities” is stored*/
int gaps[] = {701, 301, 132, 57, 23, 10, 4, 1};
for (k = 0; k < 8; k++)
for (i = gaps[k]; i < n - 1; ++i) {
char temp;
strcpy(temp, cities[i].name);
int ind = cities[i].index;

``````			for (j = i; j >= gaps[k] && strcmp(cities[j-gaps[k]].name, temp) > 0; j -= gaps[k]) {
strcpy(cities[j].name, cities[j-gaps[k]].name);
cities[j].index = cities[j-gaps[k]].index;
}
strcpy(cities[j].name, temp);
cities[j].index = ind;

char temp2;
strcpy(temp2, B2[i]);

for (j = i; j >= gaps[k] && strcmp(B2[j-gaps[k]], temp2) > 0; j -= gaps[k])
strcpy(B2[j], B2[j-gaps[k]]);
strcpy(B2[j], temp2);
}

for (i = 0; i < n - 1; i++) {
char *t = (char *)bsearch(A[i], B2, n-1, sizeof(char), compare);
if (t == NULL)
break;
}

printf("%s %s %d\$\n", A[i], B[i], cost[i]);
total_cost = cost[i];
int low, high, mid, indx;
indx = i;
for (i = 0; i < n - 2; i++) {
low = 0;
high = n - 2;
while (low <= high) {
mid = (low+high) / 2;
if (strcmp(B[indx], cities[mid].name) < 0)
high = mid - 1;
else if (strcmp(B[indx], cities[mid].name) > 0)
low = mid + 1;
else
break;
}
indx = cities[mid].index;
total_cost += cost[indx];
printf("%s %s %d\$\n", A[indx], B[indx], cost[indx]);
}
printf("%d\$\n", total_cost);
for (i = 0; i < n - 1; i++)
cost[i] = 0;
}}
return 0;
``````

}

This link has better view of the code.
HERE

When I tried that code I got

prog.cpp: In function ‘int compare(const void*, const void*)’: prog.cpp:8: error: invalid conversion from ‘const void*’ to ‘const char*’ prog.cpp:8: error: initializing argument 1 of ‘int strcmp(const char*, const char*)’ prog.cpp:8: error: invalid conversion from ‘const void*’ to ‘const char*’ prog.cpp:8: error: initializing argument 2 of ‘int strcmp(const char*, const char*)’

( int ( * ) (const void * ,const void * ) ) strcmp

here what i think is that the compare function which you have created does pass on the const void * to the strcmp which is then typecasted to char * . And as bruce eckel CHAPTER 4 1st VOLUME says that assigning other data types to void * is valid but the opposite may create errors and may result in sometimes woeful bugs. therefore I would instead go with the above line inside your bsearch function which would be explicitly dynamically done.

I have made a new binary search instead of calling a library function.
Still getting wrong answer for the code!