1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
#include <iostream> using namespace std; // Function to get the number of days in a month using pass-by-value void getDaysInMonthByValue(int month) { int days; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: days = 28; // Assuming non-leap year for simplicity break; default: cout << "Invalid month number. Please enter a number between 1 and 12." << endl; return; } cout << "Using pass-by-value: Month " << month << " has " << days << " days." << endl; } // Function to get the number of days in a month using pass-by-reference void getDaysInMonthByReference(int month, int &days) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: days = 31; break; case 4: case 6: case 9: case 11: days = 30; break; case 2: days = 28; // Assuming non-leap year for simplicity break; default: days = -1; // Invalid month break; } } int main() { int month, daysByRef; // Input the month number cout << "Enter the month number (1-12): "; cin >> month; // Get the number of days using pass-by-value getDaysInMonthByValue(month); // Get the number of days using pass-by-reference getDaysInMonthByReference(month, daysByRef); if (daysByRef != -1) { cout << "Using pass-by-reference: Month " << month << " has " << daysByRef << " days." << endl; } else { cout << "Using pass-by-reference: Invalid month number. Please enter a number between 1 and 12." << endl; } return 0; } |
Explanation:
- Pass-by-Value (
getDaysInMonthByValue
):- Function Definition:
void getDaysInMonthByValue(int month)
- This function takes the month number
month
as a parameter by value. - It uses a
switch
statement to determine the number of days in the month. - It prints the result directly.
- Function Definition:
- Pass-by-Reference (
getDaysInMonthByReference
):- Function Definition:
void getDaysInMonthByReference(int month, int &days)
- This function takes the month number
month
by value and a reference to anint
variabledays
. - It uses a
switch
statement to determine the number of days and updates thedays
reference variable accordingly. - The result is updated through the reference variable, and the function does not print directly. Instead, it relies on the
main
function to handle the output.
- Function Definition:
- Main Function (
main
):- Prompts the user to input a month number (from 1 to 12).
- It uses both pass-by-value and pass-by-reference methods to get and display the number of days in the month.
Example Outputs:
Example 1:
Input:
- Month number:
2
Output:
1 2 3 |
Enter the month number (1-12): 2 Using pass-by-value: Month 2 has 28 days. Using pass-by-reference: Month 2 has 28 days. |
Example 2:
Input:
- Month number:
4
Output:
1 2 3 |
Enter the month number (1-12): 4 Using pass-by-value: Month 4 has 30 days. Using pass-by-reference: Month 4 has 30 days. |
Example 3:
Input:
- Month number:
13
Output:
1 2 3 |
Enter the month number (1-12): 13 Invalid month number. Please enter a number between 1 and 12. Using pass-by-reference: Invalid month number. Please enter a number between 1 and 12. |