1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
#include <iostream>
#include <time.h>
using namespace std;
const int size = 50;
int search(int months[], int days[], int amount, int month, int day)
{
int first = 0, last = amount-1, mid=(first+last)/2;
while (first <= last)
{
if (month > months[mid] || month == months[mid] && day > days[mid])
{
first = mid + 1;
mid = (first + last) / 2;
}
else if (month < months[mid] || month == months[mid] && day < days[mid])
{
last = mid - 1;
mid = (first + last) / 2;
}
else
return mid;
}
return -1;
}
void sort(int months[],int days[], int amount)
{
int temp;
for (int i=0;i<=amount;i++)
{
for (int j=1;j<amount-1;j++)
{
if (months[j]<months[i]|| months[j]==months[i]&&days[j]>days[i])
{
temp=months[i];
months[i]=months[j];
months[j] = temp;
temp=days[i];
days[i]=days[j];
days[j]=temp;
temp=days[i];
}// end of if
}// end of for within for
}// end of first for
}
int main()
{
int amount, year,month,day,place;
char slash = '/';
int months[size], days[size];
cout << "Enter the amount of digits you wish to enter.";
cin >> amount;
cout << "now enter the year you wish to use.";
cin >> year;
for(int i=0; i<amount;i++)
{
month = rand() % 12+1;
months[i]=month;
if (month == 4 || month == 6 || month == 9 || month == 11)
{
day = rand() % 30+1;
}//end of if(first if ) bracket
else if (month == 2){
if (year%4==0 && year%100>0 || year%400 == 0)
{
day=rand()% 29+1;
}// end of if bracket of else if
else
day = rand()% 28+1;
}// end of else if bracket for feb
else
day=rand()%31+1;
days[i]=day;
cout << month << slash << day << endl;
}// end of for bracket
sort(months,days, amount);
cout << "the sorted dates.";
for (int i=0;i<=amount;i++)
{cout << endl << months[i] << slash << days[i];
}
cout << "enter a date in mm/dd format";
cin >> month >> slash >> day;
place = search(months, days, amount, month,day);
if (place == -1)
cout << endl << month << '/' << day << " could not be found.";
else
cout << endl << month << '/' << day << " was found at " << place << " index.";
}// end of main bracket
|