Sunteți pe pagina 1din 5

#include "stdafx.h" #include "iostream" #include "math.

h" using namespace::std; void getTwoNumber(int &num1,int &num2); int addNumber(int num1,int num2); void main() { int num1, num2,total; getTwoNumber(num1,num2); total=addNumber(num1,num2); cout<<"Total is "<<total<<endl; system("pause"); } void getTwoNumber(int &num1,int &num2) { cout<<"Enter 1st number: "; cin>>num1; cout<<"Enter 2nd number: "; cin>>num2; } int addNumber(int num1,int num2) { int total; total=num1+num2; return total; } Comparing Call by Value and Call By Reference with Reference Parameters with a Single Variable.

Statement Prototype Call

Call by Value
double F1(); void main() { double x;

Call by Reference with Reference Parameters


void F1(double &x); void main() { double x;

x=F1();

F1(x); } void F1(double &x) { x=5.0; }

header line

} double F1() { double x; x=5.0; return x;

Notes

} The main and F1 both have a local copy of x. F1 does not access mains variable. F1 simply passes a copy of xs value to main.

F1s x also contains the address of mains x. The x is an implicit pointer.

Exercises

1.
void main() { int x,y; x=5; y=12; cout<<"x before update is "<<x<<endl; cout<<"y before update is "<<y<<endl; update(x,y); cout<<"x after update is "<<x<<endl; cout<<"y after update is "<<y<<endl; system("pause"); } void update(int &x,int &y) { x=x*2; y=y+5; }

2. Write a modular program that contains 2 functions: main() and letterType(). In function letterType() user should be prompted to enter 5 letters and count the vowel and consonant letter. Display the total of vowel and consonant letter total in function main().

3. Write a modular program that contains 2 functions: main() and numberType(). In function numberType(), user should be prompted to enter 10 integer numbers and determine whether the number is odd or even. Count the odd and even number. Display the total of odd and even number in function main().
4. Write a modular program that mimics ATM machine that provides 4 types of transactions which are balance inquiry transfer, money withdrawal, cash deposit and fund transfer for the third party account. Create at least 5 methods for your program. The details of each method are as follows: Main() Display menu B Balance Inquiry W Withdrawal F Fund Transfer D Cash Deposit Display current balance after withdrawal Display current balance after fund transfer and third partys current balance Display balance after the deposit transaction

Balance() Display current balance

Withdrawal() Prompt user to enter amount to withdrawal Calculate the balance after withdrawal If the amount of withdrawal larger than the balance, discard the transaction and prompt user to enter valid amount.

FundTransfer() Prompt user to enter amount to transfer to third party If the amount entered is larger than the balance, discard the transaction and prompt user to enter valid amount Calculate balance in account and also third partys current balance

CashDeposit()

Prompt the user to enter amount to deposit into account. Calculate the balance in account.

*Initial balance in account = RM504.23 *Third partys balance in account = RM238.41 *User can choose to select several transactions until he/she chooses to stop.

5. Write a program that consists of a main() and one or more other function. One of the function named printSymbol() where the formal parameters are (char symbol, int height). symbol is a character such as * and height specifies the number of symbol characters that compose the height. The printSymbol() function prints a letter with the height specified using the specified symbol. The width will always be 7. The program will continuously run if the user entered option Y to continue or N to terminate. Once the program terminate, the system will print out number of task perform by user. Sample output: Enter symbol Enter height :* :5

******* ******* ******* ******* ******* Do you wish to continue? Y/N: Y Enter symbol Enter height ####### ####### ####### Do you wish to continue? Y/N: Y Number of tasks executed: 2 :# :3

6. The following formula gives the distance between two points (x1, y1) and (x2, y2) in the Cartesian plane:

(x2-x1)2 + (y2-y1)2

Given the center and a point on the circle, you can use this formula to find the radius of the circle. Write a program that prompts the user to enter the centre and a point on the circle. The program should then output the circles radius, diameter, circumference and area. Your program must have at least the following function:

a. radius: this function takes as its parameters four numbers that represent two points in the plane and returns the distance between them.

b. diameter: this function takes as its parameter a number that represent the radius of the ircle and returns the diameter of the circle.

c. circumference: this function takes as its parameter a number that represents the radius of the circle and returns the circles circumference. (If r is the radius, the circumference is 2r.)

d. area: this function takes as its parameter a number that represents the radius of the circle and returns the circles area. (if r is the radius, the area is r2 )

Assume that = 3.142.

S-ar putea să vă placă și