# 如何編寫以下程式?

1.Write a function definition called even that takes one argument of type int and returns a bool value.The function returns true if its one argument is an even number;otherwise it returns false.

2.Write a void function definition for a function called zeroboth that has two call-by-reference parameters,both of which are variables of type int,and that sets the values of both variables to 0.

3.Write a definition for a void-function that has two int value parameters and outputs to the screen the product of these arguments.Write a main function that asks the user for these two numbers,reads them in,calls your function,then terminates.

### 1 個解答

• 最佳解答

#include <iostream>

using namespace std;

bool even( int num ) {

return ( !( num & 1 ) );

}

void zeroboth( int &a, int &b ) {

a = b = 0;

}

void product( int a, int b ) {

cout << "product of a and b is " << a * b << endl;

}

int main() {

int a, b, num;

cout << "Please input a number: ";

cin >> num;

cout << num << " is " << ( even( num ) ? "even" : "not even" ) << endl;

cout << "Please input a number for a: ";

cin >> a;

cout << "Please input a number for b: ";

cin >> b;

zeroboth( a, b );

cout << "After zeroboth(), a = " << a << " b = " << b << endl;

cout << "Please input a number for a: ";

cin >> a;

cout << "Please input a number for b: ";

cin >> b;

product( a, b );

system("pause");

return 0;

}

參考資料： 個人的經驗