Vous êtes sur la page 1sur 12

5

ATGE2053 C++ Programming Assignment (2014)



Q1.
Write a program to prompt user to enter 3 sides of a triangle. The program then determines whether the
dimensions could form a valid triangle. If it is, the program needs to determine the type of triangle and
all its three internal angles: alpha, beta and theta, where alpha is the biggest angle, beta is the second
and theta is the smallest. Inform the user when the 3 sides cannot form a triangle.

Figure Q1

6

Q2.
a) Table Q2 is a list of favourite football players in the recent World cup 2014.
Write a program to collect the vote for these players and display the names of the players with top 5
highest votes.

1 Lionel Messi (Argentina)
2 Arjen Robben(Netherland)
3 Thomas Mller (Germany)
4 James Rodrguez (Columbia)
5 Neymar da Silva Santos Jnior (Brazil)
6 Cristiano Ronaldo (Portugal)
7 Jan Vertonghen (Belgium)
8 Robin van Persie (Netherlands)
9 Miralem Pjanic (Bosnia-Herzegovina)
10 Karim Mostafa Benzema (France)
Table Q2. Favourite football players in World Cup 2014.


Figure Q2(a).
7

Q2.
b) A rubber ball dropping from a height, h attains a speed, v just before it hits the ground, where v,
the impact speed is given by
: = 2gb

and g is the gravitational force. Assume that the ball losses one third of its height on every impact on
earth and one ninth on the moon, write a program to display a table showing the impact speed and the
bounce back height of the ball on every impact on earth and on the moon.

Prompt the user for the initial and the final height of the ball. Your program should calculate and
display in tabular form, the impact speed and the bounce back height on every impact until the ball
bounces lower than the final height on moon.

At the end, display the number of bounces it takes for the bounce back height to fall below the final
height. Assume the gravity on earth is 9.81 m/s
2
and on the moon is 1.62m/s
2
. The calculated values
must be displayed in 3 decimal places. A sample of the output is shown in Figure Q2(b).


Figure Q2(b)




8

Q3.
Write a program to count the quantity of certain character or word in a text file entered by the user.
You can assume the length of the longest word in the file does not exceed 15 characters. The search
should be case sensitive. For example, "you" and "You" are not the same word. Inform the user in case
of a file open error and prompt for the correct filename. The program should work with any text
file.Create a text file using notepad with the following content and name it as poem.txt for testing
purpose.







[1]


Figure Q3
[1] Family Friends Poems,
http://www.familyfriendpoems.com/poems/life/lesson/#ixzz37AjwEs5D
It has been said that life is the most patient teacher. You will be presented with the same
experience over and over until you learn the best way to deal with the situation. This is not
because life is cruel. Rather, it is because things have a way of coming back to haunt us
when we don't deal with them. One form of intelligence is the ability to learn from
mistakes. When you are presented with a painful experience, take the time to think about
how you can avoid it in the future. This is an example of a lesson learned.
9

Q4.

Study the sample runs of the program and write a program to match the capabilities of the program.



Unsuccessful Guess





10

Successful Guess




If y, a new secret number will be regenerate with the same range of min and max of the secret
number entered previously. The same user can continue the game with the latest balance.


Game termination

If user choose n, the game will be terminated. Display the latest balance on the screen.



11

Q5.
a) You have been commissioned by the ABC College to write a program that determines the
deviation of overall academic performance of the students for a semester. Suppose that each input is the
scores of the students in that particular subject, the program should compute the average and standard
deviation of total input scores. The standard deviation is defined to be the square root of the average of
the squared differences of the values from their average value. Mathematically, the standard deviation,
can be expressed as
o = _
1
N
(s

N
=1
- o)
2

where N is the total number of samples,
i is the sample number,
si is the i
th
score and
a is the average value.

The standard deviation is a measure of the dispersion of a set of data from its mean. The more spread
apart the data, the higher the deviation. The program should perform the following tasks:

i. read the total number of subjects registered by the students.
ii. read the input scores (in fraction form) of each registered subjects.
iii. calculate the average and standard deviation of input scores.
iv. output the average and standard deviation (in decimal form) of the input scores to determine
whether the special attention should be given to the students should be printed to the
lecturer. The attention should be given to the students only if the calculated deviation is more
than 20% of the average input scores.

You are required to complete the following function definitions to compute average and standard
deviation and write a main() function to call the functions written. Include a loop that is not
case-sensitive to allow the user repeat this computation for new input values again and again until the
lecturer says he or she wants to end the program.

double frac_to_decimal(int numerator, int denominator)
{ //write the function definition }

double average_compute (int get_numerator[],int get_denominator[],int get_size)
{ //write the function definition }

double deviation_compute (int get_nume[],int get_deno[],int get_subject,double average)
{ //write the function definition }


Hint: Your program may use DYNAMIC ARRAY to capture the input scores in fraction (i.e. score
/total score) and perform TYPE CASTING to achieve accurate result.

12

Sample output:



13

Q5. b)

Cryptography - Data Encryption and Decryption

A company wants to transmit data over the telephone, but is concerned that its phones could be tapped.
All of the data are transmitted as four-digit integers.

The company has asked you to write a program that:

i. encrypts the data so that it can be transmitted more securely.
ii. decrypts the encrypted data to read the original contents of the data


Pseudo code for encryption:
1) The user would initially be asked to enter a four-digit integer.
2) The integer is assigned to a integer variable.
3) The program identifies (extract) each digit from the integer variable (Hint: uses %, /)
4) Each identified digit is reassigned to another variable respectively (there should be 4 variables, one
variable for each digit).E.g. digit1, digit2, digit3, digit4.
5) Each digit is encrypted with the formula: (k*3) % 10
6) Swap the encrypted first and third digit
7) Swap the encrypted second and fourth digit
8) Show the encrypted integer from the four integer variables.
Under the same C++ source file, write a decryption source code that inputs a four-digit encrypted
integer and decrypts it to form the original number.


Pseudo code for decryption:
1) The user would initially be asked to enter a four-digit encrypted integer
2) Decrypt the four-digit encrypted integer using your own algorithm
3) Show the decrypted integer
4) Swap the decrypted first and third digit
5) Swap the decrypted second and fourth digit
6) Show the decrypted integer from the four integer variables.


Your program should continuously display a Menu to let the user choose which operation he wants
to execute (encryption or decryption). Terminate your program ONLY IF the user decides to quit.


In cryptography, encryption is the process of encoding messages or information in such a way
that only authorized parties can read it.

Encryption translate plain text data (plaintext) into
something that appears to be random and meaningless (ciphertext). Decryption is the process of
converting ciphertext back to plaintext.
14

Sample output:


15

Q6.

a) Write a program to perform tasks as show below:



Write a function called double round(double argument_1, int argument_2) to do the rounding
algorithm and return the rounded value to the main().

Argument_1 is the original value to be rounded and argument_2 is the number of decimal places
(fractional part) to be applied on the value received.

PROGRAM REQUIREMENTS:

Display the data in column 1 and column 2 to 6 decimal places.
Display the data in column 3 and 4 to the number of decimal places decided by the user.
Hint:
Utilize the predefined function in <cmath> library, floor(x) to round x to the largest integer not
greater than x where floor(9.2) is 9.0 and floor(-9.8) is -10.

For example,
y = floor( x + .5 )
Rounds x to the nearest integer.

y = floor( x * 10 + .5 ) / 10;
Rounds x to the tenths position (one decimal place).

y = floor( x * 100 + .5 ) / 100;
Rounds x to the hundredths position (two decimal places).


16

Q6.

b) The end of the boom O in Fig. 6 b) is subjected to three concurrent forces. Write a program
to compute the vector FR, magnitude FR and orientation of the resultant force when being
10, 20, 30, 40, 50, 60, 70, 80 degrees.









Fig. 6 b) (i) Fig. 6 b) (ii)


Sample Output:

Vous aimerez peut-être aussi