Thread: Binary Search with mandatory use of functions

  1. #1
    Registered User
    Join Date
    Oct 2015
    Posts
    2

    Binary Search with mandatory use of functions

    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:
    #include
    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);
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    278
    You said you need to use compare_dates(). Are you having trouble understanding how it works?
    Are there are specific parts of your code that you don't understand?

    There's a lot of code there. You'll need to break it down some and ask specific questions. Your post is so general that I don't even know what you are searching for...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linear and binary search in c functions
    By Hana Nasser in forum C Programming
    Replies: 16
    Last Post: 03-15-2015, 09:03 PM
  2. Binary search - two functions
    By kkk in forum C Programming
    Replies: 1
    Last Post: 08-01-2011, 01:14 PM
  3. A Binary Search Tree of... Binary Search Trees...
    By SlyMaelstrom in forum C++ Programming
    Replies: 5
    Last Post: 12-10-2005, 02:12 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM