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 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#include <iostream> using namespace std; // Function to get the weekday name using pass-by-value void getWeekdayByValue(int day) { switch (day) { case 1: cout << "The day is Monday." << endl; break; case 2: cout << "The day is Tuesday." << endl; break; case 3: cout << "The day is Wednesday." << endl; break; case 4: cout << "The day is Thursday." << endl; break; case 5: cout << "The day is Friday." << endl; break; case 6: cout << "The day is Saturday." << endl; break; case 7: cout << "The day is Sunday." << endl; break; default: cout << "Invalid day number. Please enter a number between 1 and 7." << endl; break; } } // Function to get the weekday name using pass-by-reference void getWeekdayByReference(int day, string &weekday) { switch (day) { case 1: weekday = "Monday"; break; case 2: weekday = "Tuesday"; break; case 3: weekday = "Wednesday"; break; case 4: weekday = "Thursday"; break; case 5: weekday = "Friday"; break; case 6: weekday = "Saturday"; break; case 7: weekday = "Sunday"; break; default: weekday = "Invalid day number. Please enter a number between 1 and 7."; break; } } int main() { int day; string weekdayByRef; // Input the weekday number cout << "Enter the weekday number (1-7): "; cin >> day; // Get the weekday name using pass-by-value cout << "Using pass-by-value: "; getWeekdayByValue(day); // Get the weekday name using pass-by-reference getWeekdayByReference(day, weekdayByRef); cout << "Using pass-by-reference: The day is " << weekdayByRef << "." << endl; return 0; } |
Explanation:
- Pass-by-Value (
getWeekdayByValue
):- Function Definition:
void getWeekdayByValue(int day)
- This function takes the integer
day
by value. - It uses a
switch
statement to determine the weekday based on the number. - It prints the corresponding weekday directly.
- Function Definition:
- Pass-by-Reference (
getWeekdayByReference
):- Function Definition:
void getWeekdayByReference(int day, string &weekday)
- This function takes the integer
day
by value and a reference to astring
variableweekday
. - It uses a
switch
statement to determine the weekday and sets theweekday
reference variable accordingly. - The result is updated through the reference variable.
- Function Definition:
- Main Function (
main
):- Prompts the user to input a weekday number (from 1 to 7).
- It uses both pass-by-value and pass-by-reference methods to get and display the weekday name.
Example Outputs:
Example 1:
Input:
- Day number:
3
Output:
1 2 3 |
Enter the weekday number (1-7): 3 Using pass-by-value: The day is Wednesday. Using pass-by-reference: The day is Wednesday. |
Example 2:
Input:
- Day number:
6
Output:
1 2 3 |
Enter the weekday number (1-7): 6 Using pass-by-value: The day is Saturday. Using pass-by-reference: The day is Saturday. |
Example 3:
Input:
- Day number:
8
Output:
1 2 3 |
Enter the weekday number (1-7): 8 Using pass-by-value: Invalid day number. Please enter a number between 1 and 7. Using pass-by-reference: The day is Invalid day number. Please enter a number between 1 and 7. |