Mit3543_lab1 - 1 -

HONG KONG INSTITUTE OF VOCATIONAL EDUCATION (Morrison Hill)

Department of Information and Communications Technology

C++ Programming (MIT3543) Laboratory #1

Name : Class No. :

Class : Date :

Objective : To practice the fundamental features of C++ programming

Equipment : PC, Visual C++ 2005

Tasks :

(1) Write a statement (or comment) to accomplish each of the following: (Assume that using

statements have been used)

a) State that a program will calculate the product of three integers.

b) Declare the variables x, y, z and result to be of type int.

c) Prompt the user to enter three integers.

d) Read three integers from the keyboard and store them in the variables x, y and z.

e) Compute the product of the three integers contained in variables x, y and z, and assign

the result to the variable result.

f) Print ¡§The product is¡¨ followed by the value of the variable result.

g) Return a value from main indicating that the program terminated successfully.

h) Using the statements you wrote in Task (1), write a complete program that calculates

and displays the product of three integers. Note: you will need to write the necessary

using statements.

C++ Program: Lab1_1.cpp

Mit3543_lab1 - 2 -

(2) Write single C++ statements that do the following:

a) Input integer variable x with cin and >>.

b) Input integer variable y with cin and >>.

c) Initialize integer variable i to 1.

d) Initialize integer variable power to 1.

e) Multiply variable power by x and assign the result to power.

f) Increment variable i by 1.

g) Test i to see if it is less than or equal to y.

h) Output integer variable power with cout and <<.

i) Write a C++ program that uses the statements in Task (2) to calculate x raised to the y

power. The program should have a while repetition control structure.

C++ Program: Lab1_2.cpp

Mit3543_lab1 - 3 -

Lab1_3.cpp

(3) Write a program that calculates the squares and cubes of the numbers from 0 to 10

and uses tabs to print the following table of values:

Number Square Cube

0 0 0

1 1 1

2 4 8

3 9 27

4 16 64

5 25 125

6 36 216

7 49 343

8 64 512

9 81 729

10 100 1000

Mit3543_lab1 - 4 -

(I) Procedures to create a new project in Microsoft Visual C++:

Press [ File ] ¡V [ New ] ¡V [ Project¡K ] (or press Ctrl+Shift+N) to open a new dialogue as

follows:

(1) Select "Win32" from the Project types column and select

"Win32 Console Application" from the Templates column.

(2) Input a project name at the Name field, for example¡§Lab1_1¡¨.

(3) Input the location (or press the [ Browse¡K ] button to select the location) for storing

the files of the project at the Location field.

(4) Then press [ OK ] button.

??

??

??

??

Mit3543_lab1 - 5 -

(5) Select "Application Settings" on the left hand side and check the Empty project.

(6) At last, press the [ Finish ] button.

(II) Procedures to create a new file in Microsoft Visual C++:

Press [ Project ] ¡V [ Add New Item ¡K ] from the menu to open a new dialogue as follows:

(1) Choose "Code" from the Categories column.

(2) Then choose "C++ File (.cpp)" from the Templates column.

(3) Input the file name of the C++ program at the Name field, e.g. "Lab1_1_main".

(4) The press [ Add ] button.

??

??

?? ??

??

??

Mit3543_lab1 - 6 -

Then edit the program in the space provided.

From the menu, select [Debug] ¡V [ Compile ] to compile the program.