// Name // CS201 HW4 // Problem 1 #include using namespace std; int multiply(int, int); int main() { int n1, n2; cout << "Enter your first interger \n"; cin >> n1; cout << "Enter your second interger \n"; cin >> n2; cout << multiply(n1, n2) << endl; return 0; } int multiply(int n1, int n2) { int i, sum = 0; if( n2 > 0 ){ for (i = 0; i < n2; i++) sum = sum + n1; } else{ for ( i = 0; i < -n2; i++ ) sum = sum + n1; sum = -sum; } return sum; } /* Enter your first interger -5 Enter your second interger -7 35 Press any key to continue */ // Name // CS201 HW4 // Problem 2 #include void num_change(int& input_1, int input_2); int main() { using namespace std; int first_num = 0, second_num = 0; cout << "Before the function call:\n"; cout << "The first number is " << first_num << endl; cout << "The second number is " << second_num << endl; num_change(first_num, second_num); cout << "After the function call:\n"; cout << "The first number is " << first_num << endl; cout << "The second number is " << second_num << endl; return 0; } void num_change(int& input_1, int input_2) { input_1 = 2; input_2 = 2; } /* Before the function call: The first number is 0 The second number is 0 After the function call: The first number is 2 The second number is 0 Press any key to continue */ /* Explanation: The first argument is passed by reference. In the call-by reference mechanism the argument variable is substituted for the formal parameter so that any change is made to the formal parameter is actually made to the argument variable. The second argument is passed by value, the formal paramenter is a local variable that is initialized to the value of the corresponding argument. */ // Name // CS201 HW4 // Problem 3 #include using namespace std; void compute_coin(int coin_value, int& number, int& amount_left); int main() { char repeat='y'; // for repeat the computation while(repeat=='y' || repeat=='Y'){ int amount_left=0; int number=0; while(amount_left <= 0 || amount_left >= 100){ cout<< "Input the amount of change: "; cin>> amount_left; if(amount_left <= 0 || amount_left >= 100){ cout<< "Must be between 1 and 99... try again...\n"; } } cout<< amount_left <<" cents can be given as\n"; compute_coin(25, number, amount_left); // get # of quarters cout<< number << " quarter(s) "; compute_coin(10, number, amount_left); // get # of dimes // Now amount_left is # of pennies cout<< number << " dime(s) " << amount_left << " penny(pennies).\n"; cout<<"Would you like to run the program again? y/n "; cin>> repeat; } return 0; } void compute_coin(int coin_value, int& number, int& amount_left) { number = amount_left / coin_value; amount_left = amount_left % coin_value; } /* Input the amount of change: 86 86 cents can be given as 3 quarter(s) 1 dime(s) 1 penny(pennies). Would you like to run the program again? y/n y Input the amount of change: 99 99 cents can be given as 3 quarter(s) 2 dime(s) 4 penny(pennies). Would you like to run the program again? y/n y Input the amount of change: 50 50 cents can be given as 2 quarter(s) 0 dime(s) 0 penny(pennies). Would you like to run the program again? y/n n Press any key to continue */