I am going to write an exam I found a model question.
could u pls solve this question.

An array named 'DATE_ARRAY' with n elements present in the memory.
Each element in the array is a structure representing the 'Date' information and its type defination is shown below.

Type def struct DATE_st
int MM;
int DD;
int YYYY;

Assume that the elements in the DATE_ARRAY are sorted in the ascending order for example{date1(3,21,1975)<date(3,22,1975)}.
Write a c program that takes a date as its command line arguement(in the form of numeric string "MMDDYYYY")and use the binary search algorithm to determine wether the input date exists DARE_ARRAY are not.

Could u pls write a c progra for this and wt s the theory behind it.
Thanks & Regards.

10 Years
Discussion Span
Last Post by Bench

Here's a rough algorhythm, based on my understanding of the problem as stated and what is done in a binary search of an array of sorted items, that may or may not be useful.

1) Break the input string into three separate substrings representing day string, month string and year string, and then convert those strings into ints that are data members of a holding date structure.

2) Write a function that compares two dates for equality.

3) Write a function that compares two dates for less than.

4) Determine size of DATE_ARRAY and thereby the index of the last element of DATE_ARRAY

5) Determine that holding date not equal to first and last element of DATE_ARRAY

6) Determine the index of the element that is half way between the first and the last element of the array.

7) Let the index b be the beginning of the array, the index m be the midpoint of the array, and the index e be the end of the array.

8) Determine if the holding date is the same, less than, or greater than m. If it's the same as m, then the holding date, which is the passed in date, exists in the array. If it's less than m then let m be e, redetermine the new m between b and e and repeat. If it's not m, and it's not less than m, then it must be greater than m so let m be b and redetermine the new m between b and e and repeat. Continue until you have completed the comparisons or found the date. If b == m == e or e == b + 1 and holding date not equal b or e, then the holding date is not in DATE_ARRAY.

Good luck on writing your program.


>>Could u pls write a c progra for this

Ok, wrote it in about 30 minutes, but I'm not going to post my solution until you post yours (that works)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.