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 |
#include <iostream> using namespace std; // Function to count notes using pass-by-value void countNotesByValue(int amount, int ¬es100, int ¬es50, int ¬es20, int ¬es10, int ¬es5, int ¬es1) { notes100 = amount / 100; amount %= 100; notes50 = amount / 50; amount %= 50; notes20 = amount / 20; amount %= 20; notes10 = amount / 10; amount %= 10; notes5 = amount / 5; amount %= 5; notes1 = amount; // Remaining amount is in 1-rupee notes } // Function to count notes using pass-by-reference (without modifying function signature) void countNotesByReference(int amount, int ¬es100, int ¬es50, int ¬es20, int ¬es10, int ¬es5, int ¬es1) { countNotesByValue(amount, notes100, notes50, notes20, notes10, notes5, notes1); } int main() { int amount; int notes100, notes50, notes20, notes10, notes5, notes1; // Input the amount cout << "Enter the amount: "; cin >> amount; // Count notes using pass-by-value countNotesByValue(amount, notes100, notes50, notes20, notes10, notes5, notes1); cout << "Using pass-by-value:" << endl; cout << "100 rupee notes: " << notes100 << endl; cout << "50 rupee notes: " << notes50 << endl; cout << "20 rupee notes: " << notes20 << endl; cout << "10 rupee notes: " << notes10 << endl; cout << "5 rupee notes: " << notes5 << endl; cout << "1 rupee notes: " << notes1 << endl; // Count notes using pass-by-reference countNotesByReference(amount, notes100, notes50, notes20, notes10, notes5, notes1); cout << "Using pass-by-reference:" << endl; cout << "100 rupee notes: " << notes100 << endl; cout << "50 rupee notes: " << notes50 << endl; cout << "20 rupee notes: " << notes20 << endl; cout << "10 rupee notes: " << notes10 << endl; cout << "5 rupee notes: " << notes5 << endl; cout << "1 rupee notes: " << notes1 << endl; return 0; } |
Explanation:
- Pass-by-Value (
countNotesByValue
):- Function Definition:
void countNotesByValue(int amount, int ¬es100, int ¬es50, int ¬es20, int ¬es10, int ¬es5, int ¬es1)
- This function takes the
amount
as input by value (i.e., a copy of the amount). - It calculates the number of each type of note (100, 50, 20, 10, 5, 1) needed for the given amount and stores the results in the reference variables (
notes100
,notes50
, etc.). - The function uses modulo operations to determine the number of notes for each denomination and updates the remaining amount accordingly.
- Function Definition:
- Pass-by-Reference (
countNotesByReference
):- Function Definition:
void countNotesByReference(int amount, int ¬es100, int ¬es50, int ¬es20, int ¬es10, int ¬es5, int ¬es1)
- This function also takes the
amount
and updates the reference variables (notes100
,notes50
, etc.) with the number of notes for each denomination. - In this example, it simply calls
countNotesByValue
to do the actual calculation, demonstrating that pass-by-reference can be used to refer to the same variables.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input an amount.
- It then counts the notes required using both methods and prints the results.
Example Outputs:
Example 1:
Input:
- Amount:
273
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter the amount: 273 Using pass-by-value: 100 rupee notes: 2 50 rupee notes: 1 20 rupee notes: 1 10 rupee notes: 2 5 rupee notes: 0 1 rupee notes: 3 Using pass-by-reference: 100 rupee notes: 2 50 rupee notes: 1 20 rupee notes: 1 10 rupee notes: 2 5 rupee notes: 0 1 rupee notes: 3 |
Example 2:
Input:
- Amount:
485
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter the amount: 485 Using pass-by-value: 100 rupee notes: 4 50 rupee notes: 1 20 rupee notes: 1 10 rupee notes: 1 5 rupee notes: 1 1 rupee notes: 0 Using pass-by-reference: 100 rupee notes: 4 50 rupee notes: 1 20 rupee notes: 1 10 rupee notes: 1 5 rupee notes: 1 1 rupee notes: 0 |
Example 3:
Input:
- Amount:
89
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
Enter the amount: 89 Using pass-by-value: 100 rupee notes: 0 50 rupee notes: 1 20 rupee notes: 1 10 rupee notes: 1 5 rupee notes: 1 1 rupee notes: 4 Using pass-by-reference: 100 rupee notes: 0 50 rupee notes: 1 20 rupee notes: 1 10 rupee notes: 1 5 rupee notes: 1 1 rupee notes: 4 |