You are on page 1of 6

School of Mathematical Sciences

Universiti Sains Malaysia

MAT 181 Programming for Scientific Applications


Lab 3

There are 2 ways to print the source file (.cpp file) and output:
i) To print directly from MS Visual Studio menu (for the .cpp file), or
ii) To copy the source program/output and paste it to a 3 rd party editor (e.g. MS Word). This is a more
preferred way if you want to include the source program and output in the documentation for better
formatting purpose.
Refer to Appendix A and Appendix B at the end of this lab sheet for step-by-step guide how to perform
copy-paste.

1. Consider the following: A

z
x


B C
y

It is known that z = x 2 + y 2 and the area of ABC is ½ |BC| |AC|. Write a complete program to compute z
x
and the area of ABC for given values of positive x and y. Also, find the angle , (  = tan −1
). If x or y is
y
not positive, the program should be terminated. Assume that the user can continue computing these values
for different x and y as long as he wants to.

Your output should look like the following,


Enter base and height of the triangle: 3 4
Hypotenuse is 5
Area is 6
theta is 0.643501

Compute another triangle? ('Y' to continue or any other key to stop): y


Enter base and height of the triangle: 3 -4
Hypotenuse is 5
Area is 6
Base and height must be positive values. Program stop!

Note 1. In C++, to compute y = x5 for example, we use the function pow(u,v)which is already
defined in an existing header file of the MS Visual Studio compiler. To compute y = x5 we
write y = pow(x,5).
2. To compute  = tan-1(x), use the function atan();  = tan-1(x) becomes theta =
atan(x); the function atan(x) is also already defined inside the compiler.

Page 1 of 6
2. The bending moment in a simply supported beam of span L subjected to a concentrated load P at the
midspan is given by
 x L
 P 2 0x <
M ( x) =  2
 P ( L − x) L
xL
 2 2
where x is the distance from the left support.

L/2 L/2

Write a C++ program to read the load, P, the beam span, L, and the distance x and computes the bending
moment. Your program must check for the validity of the x value, i.e. 0 ≤ x ≤ L, before proceeding to
compute the bending moment.
Your output should look like the following,

Enter load (P), beam span (L) and the distance (x): 20 56 89
The distance x must be within the value of 0 and 56. Try again.

Enter load (P), beam span (L) and the distance (x): 20 56 78
The distance x must be within the value of 0 and 56. Try again.

Enter load (P), beam span (L) and the distance (x): 20 56 34
The bending is 220

3. Write a program to calculate and output the minimum, maximum and average of a list of positive test scores.
Assume that the scores will be entered one per line and that the list will end with a negative sentinel score.

Your output should look like the following,

Enter score (enter -1 when there is no more score to enter): 45


Enter score (enter -1 when there is no more score to enter): 29
Enter score (enter -1 when there is no more score to enter): 68
Enter score (enter -1 when there is no more score to enter): 79
Enter score (enter -1 when there is no more score to enter): 12
Enter score (enter -1 when there is no more score to enter): -1

Minimum score is 12
Maximum score is 79
Average score is 46.6

Page 2 of 6
4. Write a complete C++ program to calculate the wages of employees which are based on the number of hours
worked times the hourly rate. The user must type in the employee number, the number of hours the employee
has worked and the hourly rate. Your output should look like the following:

Please enter the employee number: 1090


Please enter the hours worked and the hourly rate: 35 10

Employee number: 1090


Hours worked: 35
Hourly rate: RM 10
Total wages: RM 350
Do you want to process another data? Type ‘y’ for yes, ‘n’ for no: y

Please enter the employee number: 2121


Please enter the hours worked and the hourly rate: 40 11

Employee number: 2121


Hours worked: 40
Hourly rate: RM 11
Total wages: RM 440
Do you want to process another data? Type ‘y’ for yes, ‘n’ for no: n

Page 3 of 6
Appendix A
Step-by-step guide: How to copy a source file and paste to MS Word

To copy the source program, do the following,


iii) Open MS VC++ solution file in which you want the C++ program to be copied, ensure the cpp window
is the active window by clicking on any part of the cpp window, choose option ‘Edit/Select all’
from the main menu (Figure 1).
iv) When the texts in cpp window are highlighted, choose ‘Edit/Copy’ from the main menu, or press
hotkey Ctrl-C to copy the program (Figure 2)
v) Go to the MS Word editor, put your cursor at the point where you want to insert the program and press
Ctrl-V to paste the program. The entire program is copied to the MS Word (Figure 3).

Figure 1 Figure 2

Figure 3

Page 4 of 6
Appendix B
Step-by-step guide: How to copy the output and paste to MS Word

To copy the output, perform the following


i) In the MS Visual Studio output console window, click on the icon on top left hand corner of the window
and choose option ‘Edit/Select all’ (Figure 4).
ii) When the source program is highlighted, choose ‘Edit/Copy’ from the main menu to copy the output
(Figure 5).
iii) Go to the MS Word editor, put your cursor at the point where you want to insert the output and press
Ctrl-V to paste the output. The entire output is copied to the MS Word, where you can perform
formatting (Figure 6).
iv) If you are unable to paste the content in MS Word, open a notepad (available from
Start/Programs/Accessories/NotePad), press Ctrl-V to paste to the notepad. Then press Ctrl-C again
to copy the contents from the notepad
v) and repeat step (iii) above.

Figure 4

Figure 5

Page 5 of 6
Figure 6

Page 6 of 6

You might also like