holidays.shl Holiday structure array - linear search Use the shell program in D2
ID: 3694368 • Letter: H
Question
holidays.shl Holiday structure array - linear search
Use the shell program in D2L
NOTES: - You will also need the file holidays.dat file in D2L
- Be sure and check for successful disk file open
Holidays Description:
Sometimes when searching for a particular item in an array, instead of returning the index of where the item was found in the array ( if found ), you can return the item itself in the function parameter that was passed by reference. Then the function itself returns a bool true if the item was found; it return bool false if the item wasn’t found.
In this exercise you create a function that searches an array of structures for a specified date ( month and day pair) and if found, copies the C++ string from the
array of structures into the array of characters that is the last parameter. If the date is not found, ‘’ is placed in cell 0 of the last parameter and a false is returned.
The following is a list of the data in the file:
1 11 Hostos Day (Puerto Rico)
1 15 Martin Luther King Jr. Day
1 23 Handwriting Day
2 3 Setsubun (Bean-throwing festival in Japan)
2 5 Cham Cham Mapinduzi Day (Tanzania)
2 6 Babe Ruth's Birthday
2 9 Feast of Saint Appolonia (patron saint of dentists)
2 10 Feast of St. Paul's Shipwreck (Malta)
3 31 Bunsen Burner Day
4 22 Earth Day
Explanation / Answer
const int MAX_DATES = 60;
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct DayData
{
int month, // Month / day of holiday
day;
string holiday; // Name of holiday
};
bool findHoliday ( const vector<DayData> &holidayList, int month, int day, string &holiday, int count);
int main ()
{
vector<DayData> holidayList(MAX_DATES); // List of holidays
int count=0,
month, day;
string holiday;
// Open the designated file for input.
ifstream holidayFile;
holidayFile.open("holidays.dat");
holidayFile >> holidayList[count].month >> holidayList[count].day;
holidayFile.get();
getline(holidayFile,holidayList[count].holiday);
}
while( holidayFile )
{
count++;
holidayFile >> holidayList[count].month >> holidayList[count].day;
holidayFile.get();
getline(holidayFile,holidayList[count].holiday);
}
holidayList.resize(count);
holidayFile.close();
cout << endl << "Enter the month and day for a holiday: ";
cin >> month >> day;
// Display the holiday (if any) for the requested date.
if ( findHoliday(holidayList, month, day, holiday, count) )
cout << "The holiday is " << holiday << endl;
else
cout << "The month/day is not in the holiday list." << endl;
return 0;
}
bool findHoliday ( const vector<DayData> &holidayList, int month, int day, string &holiday, int count);
{
int count = 0;
while (count < size) // make sure we stay inside our array
{
if (month == holidayList[count].month)
{
if (day == holidayList[count].day)
{
holiday = holidayList[count].holiday;
return 1;
}
}
else
count++;
}
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.