Function was checked to return value A, but in main, the value was found to be -1

Function was checked to return value A, but in main, the value was found to be -1



The bug seems to be in binarySearch function and the for-loop it is in. I have placed a few printf to check for values. binarySearch supposed to return value: 8, but index variable in for-loop is -1.



I apologise if the formatting looks terrible. Please give me guidance on how to to post nice looking code here.



Thank you so much!


#include <stdio.h>
#include <string.h>

int binarySearch(char[7], char, int, int);
void deleteAndMoveEntry(char[7], int, int);

int main()
// all the variables
const int MAXIMUM = 5000;
char temp[7];
char symbol[MAXIMUM][7];
int sizeSymbol = 0, sizeDeleted = 0;
char toDelete[MAXIMUM][7];
int i = 0, j;
int index;
FILE *in = fopen("stockin.txt", "r");
FILE *deleted = fopen("stockdel.txt", "r");
FILE *out = fopen("stockout.txt", "w");
FILE *errorFile = fopen("error.txt", "w");

while (fscanf(in, "%sn", temp) == 1 && i < MAXIMUM)
strcpy(symbol[i], temp);
i++;
sizeSymbol++;


i = 0;

while (fscanf(deleted, "%sn", temp) == 1 && i < MAXIMUM)
printf("Hin");
strcpy(toDelete[i], temp);
i++;
sizeDeleted++;

// Should we sort array symbol? To implement later for fun

for (i = 0; i < sizeDeleted; i++)
index = binarySearch(symbol, toDelete[i], 0, sizeSymbol);
printf("%dn", index);

if (index != -1)
deleteAndMoveEntry(symbol, index, sizeSymbol);
sizeSymbol--;
else if (index == -1)
fprintf(errorFile, "%sn", toDelete[i]);


for (i = 0; i < sizeSymbol; i++)
fprintf(out, "%sn", symbol[i]);

fclose(in);
fclose(out);
fclose(deleted);
fclose(errorFile);
return 0;


int binarySearch(char symbol[7], char toFind, int left, int right)
if (right >= left)
int mid = left + ((right - left) / 2);
if (strcmp(toFind, symbol[mid]) == 0)
printf("%dn", mid);
return mid;


if (strcmp(toFind, symbol[mid]) < 0)
binarySearch(symbol, toFind, left, mid - 1);

if (strcmp(toFind, symbol[mid]) > 0)
binarySearch(symbol, toFind, mid + 1, right);

return -1;


void deleteAndMoveEntry(char symbol[7], int index, int size)
int i;
for (i = index; i < size - 1; i++)
strcpy(symbol[i], symbol[i + 1]);


strcpy(symbol[i + 1], "");





Post example contents of input files like stockin.txt, stockdel.txt
– chux
Aug 26 at 1:38



stockin.txt, stockdel.txt





when calling fopen(), always check (!=NULL) the returned value to assure the operation was successful. IF not successful, then call perror() to display your error message AND the text reason the system thinks the function failed, to stderr
– user3629249
Aug 26 at 2:41



fopen()


perror()


stderr




2 Answers
2



The problem is most likely caused by missing return statements in the recursive calls. Change the lines


return


if (strcmp(toFind, symbol[mid]) < 0)
binarySearch(symbol, toFind, left, mid - 1);

if (strcmp(toFind, symbol[mid]) > 0)
binarySearch(symbol, toFind, mid + 1, right);



to


if (strcmp(toFind, symbol[mid]) < 0)
return binarySearch(symbol, toFind, left, mid - 1);

if (strcmp(toFind, symbol[mid]) > 0)
return binarySearch(symbol, toFind, mid + 1, right);



Without those return statements, execution falls to the bottom of the function, where the return value is -1, which explains the behavior you observed in main.


return


main





a good start, but dues not catch all the problems in the OPs posted code
– user3629249
Aug 26 at 2:42





@user3629249, you have commented a bit about that. I encourage you to post an answer.
– R Sahu
Aug 26 at 2:47





Thanks @RSahu. I corrected my code and it worked well now. What a folly I have made!
– Tng Jun Wei
Aug 26 at 10:21



regarding: Please give me guidance on how to to post nice looking code here. One way to easily clean up the code formatting is to highlight/select all your code, then click the as that will (mostly) format your code. Then follow the axiom: only one statement per line and (at most) one variable declaration per statement.




In C, the range of valid index values is from 0 through (number of elements in array -1)



Regarding:


for (i = index; i < size - 1; i++) {
strcpy(symbol[i], symbol[i + 1]);



this expression: symbol[i+1], during the last loop, will be accessing beyond the end of the array.


symbol[i+1]



regarding:


strcpy(symbol[i + 1], "");



the index i will be pointing past the last entry in the array when the preceding for() loop has exited.


i


for()



So this: symbol[i+1] will be accessing 2 entries beyond the end of the array.


symbol[i+1]



Both of these problems result in undefined behavior






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)