![]() |
|
#1
|
|||
|
|||
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:
|
|
#2
|
||||
|
||||
|
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:
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! Last edited by aaroncohn : 07-Mar-2004 at 20:13.
|
|
#3
|
|||
|
|||
|
Thanks for the help.
I appreciate it. |
|
#4
|
||||
|
||||
|
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
|
||||
|
||||
|
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 GIDBlog
US Elections and the ?Voter?s Responsibility? by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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