Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Two-dimensional Arrays board.cpp Message board Use the shell program on D2L Mess

ID: 3768259 • Letter: T

Question

Two-dimensional Arrays                                         

board.cpp                      Message board

                                                Use the shell program on D2L

     

Message Board Description:

In this exercise you will use a two-dimensional array of characters to represent a message board. For example, you can display the message

“I C U” on a 5 x 11 message board as follows:

,,,,,,,,,,,

,I,CCC,U,U,

,I,CC,,U,U,

,I,CCC,UUU,

,,,,,,,,,,,

The function fillRectangle() fills in a specified rectangle on the message board with a given character.

void fillRectangle(char board[] [ NUM_COLS ], int row, int col,

                             int width, int height, char fillChar)

Input parameters:

row, col: row and column of the upper, left corner of the rectangle

width, height: rectangle dimensions

fillChar: character to fill the rectangle with

Input/Output parameter

board: (input) message board

             (output) updated message board, including specified rectangle

The displayBoard() function displays the message board.

void displayBoard(const char board[] [NUM_COLS])

Input parameter

board: message board

Outputs

Displays the completed new message board

The following calls

fillRectangle(board, 0, 0, 11, 5, ’,’ );

fillReatangle(board, 1, 1, 1, 3, ’I’)

produce part of the message board shown.

_________________________________________________________________________________

_______________________________________________________________________________
_______________________________________________________________________________

Searching

Num                File Name                   Description

1                      holidays.cpp      Holiday structure array - linear search

                                                    Use this shell program located in D2L

                                      NOTES: - You will need the file holidays.txt file in the same
                                                                 directory as your C++ program.
   - 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 Holidays.txt 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


____________________________txt.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

__________________________________shell____________________________________________

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

const int MAX_DATES = 60,     // Max number of holidays in list
MAX_NAME_LEN = 81; // Max length of holiday name

#include <iostream>
#include <fstream>
using namespace::std;


// Definition of DayData type
struct DayData
{
int month,                    // Month / day of holiday
day;
char holiday[MAX_NAME_LEN];   // Name of holiday
};

// Function prototype
void findHoliday ( const DayData holidayList[], int listLength,
   int month, int day, char holidayCopy[] );

void main ()
{
DayData holidayList[MAX_DATES];   // List of holidays
int count = 0,                    // Number of holidays in list
searchMonth,                        // Input month / day
searchDay;
char holidayName[MAX_NAME_LEN];   // Name of selected holiday

// Open the designated file for input.
ifstream holidayFile(" holidays.txt");
if (!holidayFile) {
cout << "Can NOT open file " << endl;
return;
}

// Read in the list of holidays.
while (holidayFile.good() && holidayFile >>
holidayList[count].month>> holidayList[count].day )
{
holidayFile.get();   // Remove blank after day from the stream
holidayFile.getline(holidayList[count].holiday,
MAX_NAME_LEN,' '); // Read holiday name
count++;                       //    including spaces
}

// 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 >> searchMonth >> searchDay;

// Display the holiday (if any) for the requested date.
findHoliday(holidayList,count,searchMonth,searchDay,holidayName);
if ( holidayName[0] != '' )
   cout << holidayName << endl;
else
   cout << "No holiday listed" << endl;
}

//--------------------------------------------------------------------
// Insert your findHoliday() function here.
//--------------------------------------------------------------------
void findHoliday (const DayData holidayList[],int listLength,
int month,int day,char holidayCopy[])
{














} // end findHoliday


/******** Sample Output

Enter the month and day for a holiday: 3 31
Bunsen Burner Day


Enter the month and day for a holiday: 2 4
No holiday listed


*/

Explanation / Answer

void fillRectangle(char board[NUM_ROWS][NUM_COLS], int row, int col, int width, int height, char fillChar)
{
for (int i = col; i < width + col; i++)
{
for (int j = row; j < height + row; j++)
{
board[j][i] = fillChar;
}
  
void displayBoard(const char board[NUM_ROWS][NUM_COLS])
{
for (int i = 0; i < NUM_ROWS; i++)
{
for (int j = 0; j < NUM_COLS; j++)
{
cout << board[i][j];
}
cout << endl;
}
}


***************************************************

void findHoliday (const DayData holidayList[],int listLength,int month,int day,char holidayCopy[])
{
int i;
int j;
for ( i = 0; i < listLength; i++ )
{
if ( holidayList[i].month == month && holidayList[i].day == day )
{
for ( j = 0; j < MAX_NAME_LEN ; j++ )
{
holidayCopy[j] = holidayList[i].holiday[j];
}
}
}
holidayCopy[j]= '';
}