GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 07-Mar-2004, 18:01
anignna anignna is offline
New Member
 
Join Date: Mar 2004
Location: Chicago
Posts: 8
anignna is on a distinguished road
Question

Linear search on a multidimensional array.


I need help programming a linear search for a multidimensional array of 3 by 10.
The info in the array is read from the sample data file shown below. The frist num is the month the second is the day and the rest is the name of the holiday. The program asks for the month and day and the using a linear search finds them and outputs the corresponding holiday. The code i have so far is included below. Any help would be appreciated.

Sample data 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

CPP / C++ / C Code:

//--------------------------------------------------------------------
//
//  Laboratory 13, Core Ex. 1, Application shell        holidays.shl
//
//--------------------------------------------------------------------

// Finds a holiday for a specified date from a list of holidays.

const int MAX_DATES = 367;     // Max number of holidays in list

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

// Definition of DayData type
struct DayData
{
    int month,              // Month / day of holiday
        day;
    string holiday;       // Name of holiday
};

// Function prototype
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,                    // Index for holidayList elements
        month, day;                 // Input month / day
    string holiday;               // Holiday returned from findHoliday 

    // 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);

    // Read in the list of holidays.
    while( holidayFile )
    {
	   	count++;

        holidayFile >> holidayList[count].month
        			>> holidayList[count].day;
        holidayFile.get();
        getline(holidayFile,holidayList[count].holiday);
    }

    holidayList.resize(count);

    // Close the file.
    holidayFile.close();

    // Prompt the user for the date of the desired hoilday.
    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); 
{
//Rest of code to be inserted here.

}

  #2  
Old 07-Mar-2004, 19:35
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
To find the item you're looking for, just go through the array and check for the data using the same method you used to add it to your structure. Just start at zero, then check the user input date against the date in each element of the array. If you get a match, set your output string equal to the one in your current element, then return a 1 to signal that the element was found. If the element isn't in the list, return a 0. If you can't figure out how to check for the day once you know you've got the right month, try using nested-if statements or nested-switch statements. When you pass count to your find function, you should rename it to something like "size" while you're in the scope of the find function, so it's not confused with a counter. You also forgot to check to see if the file-open operation was successful. If you fail to check for this, your program will be doomed to failure! If the file fails to open, your program will continue to run and all subsequent reading/writing operations will fail. This is bad

Here's my version of your function:
CPP / C++ / C Code:
bool findHoliday ( const vector<DayData> &holidayList, int month, int day, string &holiday, int size) 
{
  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;
}

Here are a couple of questions you should ask yourself:
What if the user enters a date for which there are two holidays?
What if the user enters data that doesn't comply with your specification?

Asking yourself these sorts of questions and implementing the answers will make your program robust, and you should practice this as much as you can with your programs. Hope this helped!
Attached Images
File Type: jpg window.jpg (22.4 KB, 11 views)
Last edited by aaroncohn : 07-Mar-2004 at 20:13.
  #3  
Old 07-Mar-2004, 20:05
anignna anignna is offline
New Member
 
Join Date: Mar 2004
Location: Chicago
Posts: 8
anignna is on a distinguished road
Thanks for the help.
I appreciate it.
  #4  
Old 07-Mar-2004, 20:11
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
I posted the code above for your understanding of how to solve the problem. I didn't post it so you could copy my work, so please don't do that. You're welcome
  #5  
Old 07-Mar-2004, 21:07
aaroncohn's Avatar
aaroncohn aaroncohn is offline
Regular Member
 
Join Date: Feb 2004
Location: Bay Area, CA.
Posts: 564
aaroncohn is a jewel in the roughaaroncohn is a jewel in the roughaaroncohn is a jewel in the rough
Hope you didn't copy my code, because I made a small mistake. The code is correct only if you add an else statement after you check to see if the day matches. The else statement should increment count, otherwise the loop will repeat itself forever when you try to look for a day in a month that has several entries.

Example:

Let's say I enter: 2 11

The program will start the loop... but since the 11th is not the first holiday to occur in the month of february, the loop will just keep seeing that the day is wrong, won't increment the counter, and will continue to repeat itself until the user intervenes.
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
c: array comparison jack C Programming Language 7 26-Jan-2004 12:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 12:52
Search Engine Positioning 101 and 201 "How To" Tips... 000 Search Engine Optimization Forum 0 29-May-2003 11:34
Array_search on a multidimensional array JdS MySQL / PHP Forum 3 11-May-2003 07:22
[class] 404 search function code jrobbio MySQL / PHP Forum 6 22-Apr-2003 10:32

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:03.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.