Hi everyone.. If someone could please help with my issue, it would be greatly appreciated. I have searched everywhere online and worked for hours trying to figure out how to perform this operation. I need to make a binary search function that returns the correct index which can then be used to reference different data via parallel arrays. I am getting confused because I specifically need to use a function provided by my professor (Compare Dates).This is supposed to be called within the binary search function in order to obtain the correct index. I am sure there are other errors in my code, however if someone could please help me understand how to return the correct index i would definitely make all the difference
Code:
"../market/market.h"
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
int binary_search(int array_size, char *month_array, char *date);
// Function Prototypes
//---- Provided by TA. Returns 0 if dates match, returns negative long if date1 is less than date2, returns positive long if date 1 is greater than date 2
long compare_dates(const char* date1, const char* date2);
long date_to_long(const char* date);
int main(){
//length of each date array being set to variables that will then be passed into binarysearch function
int jan_size = sizeof(january_2015_date_recorded)/sizeof(january_2015_date_recorded[0]);
int feb_size = sizeof(february_2015_date_recorded)/sizeof(february_2015_date_recorded[0]);
int mar_size = sizeof(march_2015_date_recorded)/ sizeof(march_2015_date_recorded[0]);
char date[20];
int i;
//Obtains user input
puts("Enter the date in the following format --> Month day, year (2015)");
puts("As an example March 26, 2015");
fgets(date, 20, stdin);
// determines whether the input month was January, February or March based on the first element of input string.
while (i == -1){
puts("The market was not open on that day");}
if(tolower(date[0])=='j')
{
i = binary_search(jan_size, *january_2015_date_recorded, date);
}else if(tolower(date[0])== 'f')
{
i = binary_search(feb_size, *february_2015_date_recorded, date);
}else if(tolower(date[0])== 'm'){
i = binary_search(mar_size, *march_2015_date_recorded, date);
//once you have the correct index use it ----> double march_2015_opening_average[i]
}
returnEXIT_SUCCESS;
}
int binary_search(int array_size, char *month_array, char *date) {
int start = 0; //the starting index of the part we're interested in
int end = array_size;
long c;
int middle = (end - start) / 2 + start;
while (end - start > 0) {
int middle = (end - start) / 2 + start;
c = compare_dates(date, month_array);
if(c==0)
{ return month_array [middle]; //The date was found
} else if(c<0) //search the upper end of array
{
start = middle + 1;
}
else
{
end = middle - 1;
} return middle; } return -1; }
// given functions that must be used
long compare_dates(const char* date1, const char* date2)
{
long d1 = date_to_long(date1);
long d2 = date_to_long(date2);
return (d1 - d2);
}
long date_to_long(const char* date) {
struct tm tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
strptime(date, "%h %d, %Y", &tm);
return mktime(&tm);
}