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

practice 14 (dynamic allocated two-dimensional arrays and class) Problem1 dynami

ID: 3696980 • Letter: P

Question

practice 14 (dynamic allocated two-dimensional arrays and class)

Problem1 dynamic allocated two-dimensional array

This program fills and print out the board.

The steps are:

Declare an int** variable named board which will be used for dynamical array;

Declare two int variables row and column.

Prompt user to enter the values of row and column;

Dynamically allocates a two-dimensional array large enough to hold that row*column values;

Prompt users to enter the values for the board.

Print out the board.

Problem 2 class (simple program)

Complete the following program:

#include <iostream>

using namespace std;

//define a class named DayOfYear with two public data members (month and day) and one public function member (function prototype: void output())

int main( )

{

//declare two variables named today and birthday of type DayOfYear.

   

       

//assign variable today’s member month and member day to be 2 and 15;

//print out today’s date (print out member month and member day of variable today using function member of output)

//prompt user to enter their birthday’s month and day, and save them in the corresponding members of variable birthday

//print out user’s birthday (print out member month and member day of variable birthday using function member of output)

//compare variables today and birthday, if they are same, print out “Happy Birthday!”; otherwise, print out “Happy Unbirthday!”

    return 0;

}

//complete the definition of function member output of class DayOfYear

void DayOfYear::output( )

{

   //print out the data members month and day

}

practice 16

Problem 1: Revise Problem 2 of practice 14 to do the following:

1)make members month and day private

2)add new functions:

a.getMonth: void function with one reference parameter which will take the value of member month

b.getDay: void function with one reference parameter which will take the value of member day

c.setMonth: void function with one value parameter newmonth which will be assigned to member month

d.setDay: void function with one value parameter newday, which will be assigned to member day

e.compareDate: bool function with one const reference parameter (say myday) of DayofYear type; return true if members month and day are same as myday’s month and day, otherwise, return false.

f.increMonth: increase member month by 1, if it is greater than 12, then set it to be 1

g.increDay: increase member day by 1, if it is greater than corresponding days of member month (assume it is leap year), then set it to be 1 and call increMonth

3)rewrite main function and test all your functions

Problem 2: Revise problem 1 to do the following:

1)Keep members month and day private same

2)Keep functions of getMonth, getDay, setMonth, setDay, increMonth, increDay, and output same as before

3)Add three constructors:

DayofYear(); which is a default constructor and sets member month and day to be 1 and 1.

DayofYear(int m, int d); which sets member month and day to be m and d if m is a valid month and d is a valid day in this month

DayofYear(int m); which sets member month to be m and d to be 1 if m is a valid month.

4)Revise function compareDate to a friend function of class DayofYear with two parameters which are both type DayofYear

5)rewrite main function and test all your functions

Explanation / Answer

#include <iostream>
using namespace std;

class DayOfYear                  
{
public:
    void output( );                            
    int   month;
    int   day;
};

int main( )
{
    DayOfYear   today,   birth_day;   

    cout << "Enter today's date: ";
    cout << "Enter month as a number: ";
    cin >> today.month;
    cout << "Enter the day of the month: ";
    cin >> today.day;
    cout << "Enter your birth_day: ";
    cout << "Enter month as a number: ";
    cin >> birth_day.month;
    cout << "Enter the day of the month: ";
    cin >> birth_day.day;
  
  
    cout << "Today's date is ";
         today. output( );                                                  
         cout << "Your birth_day is ";                                    
         birth_day.output( );
                                                                                       
        if (today.month == birth_day.month && today.day == birth_day.day)
            cout << "Happy birth_day! ";
        else
            cout << "Happy Unbirth_day! ";
  
        return 0;          
    }                   
  
    void DayOfYear :: output( )                                     
    {  
             cout << "month = " << month            
               << ", day = " << day << endl;
   }