Tải bản đầy đủ (.pdf) (4 trang)

Lab 4 selection statements

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (239.1 KB, 4 trang )

Course: Programming Fundamentals (C language)
LAB 4 – Selection Statements
Main content:
1.
2.
3.
4.

Get used to simple selection statements
Get used to nested selection statements
Start complex data processing
Check coding styles

Practice:
1. Get used to simple selection statements
1.1. Write a C program to perform a simple calculation between two numbers input by a user
from the keyboard. Calculation is requested from a starting menu as follows:
What kind of calculation do you want?
1. a + b
2. a – b
3. a * b
4. a / b
5. None
Please enter your choice (1 for addition, 2 for subtraction, 3 for multiplication, 4 for division,
or 5 for none to exit the calculation program):
1.2. Write a C program to check if a character input by a user from the keyboard is a digit, a
punctuation mark, an alphabetic letter, a bracket, or other special character. Print a
corresponding check result along with the input character. If the user enters ‘#’, the
program is ended with a prompt message: “END”.
1.3. Write a C program to check the characteristics of a polygon in a two-dimensional space
according to the vertices input by a user from the keyboard as follows:


1. If there is one vertex, check if the polygon lies on the x axis or the y axis or both.
2. If there are two distinct vertices, check if the polygon is a square or a rectangle whose
has edges parallel to the axes.
3. If there are three distinct vertices, check if the polygon is an equilateral, isosceles, or
squared triangle. An equilateral triangle is a triangle with all the sides of the same
length. An isosceles triangle is a triangle with two sides of the same length. A squared
triangle is a triangle with a 90o angle.
4. Otherwise, print a message: “Undiscovered polygons”.

1


1.4. Write a C program to calculate a distance in day between two given dates input by a user
from the keyboard. It is noted that a leap year has February with 29 days instead of 28 days.
How many days are there between 15/01/2016 and 22/06/2017? How many days are there
between 24/05/2017 and 29/06/2017?
2. Get used to nested selection statements
2.1. Write a C program to solve a quadratic equation a*x2 + b*x + c = 0 where a, b, c, and x are
any floating-point numbers input by a user from the keyboard.
2.2. Write a C program to calculate the cost for travelling from point A to point B in a 2-D space
by taxi with the following price board as follows:
Distance (km)
0<=5.0
5.0<=10.0
10.0<=18.5
> 18.5

Unit price (K)
10.0
9.5

8.2
7.5

If a distance between A and B is 6.5 km, the cost is calculated as: 5*10.0 + (6.5-5)*9.5=
64.25K.

Full
name
John
Smith
Peter
Brown
Alice
Depp
March
Carter

Passport

Given the information of the tourists as follows:
Ending
Nationality Fee
Starting date
date

First location
Name X

Y


Last location
Name X
Y

Tour type

B1203457

English

15.3

13/2/2017

18/2/2017

L1

2.4

5

L2

5.2

8.3 SPECIAL

A3910385


English

13.2

23/12/2016

03/1/2017

L1

2.4

5

L3

4.8

7.5 NORMAL

B2384021

English

13

23/12/2016

03/1/2017


L2

5.2

8.3

L4

6.2

8.9 NORMAL

A3830291

English

10.5

28/12/2016

04/1/2017

L3

4.8

7.5

L5


6.5

9.2 SALE

If these tourists don’t travel with their tours, calculate the cost of each of them once they
travel by taxi. Who has the lowest cost and who has the highest cost?
2.3. Write a C program to ranking the following students according to their average grade in a 4scale grading system: ‘A’ for grade in [8.5, 10], ‘B’ for grade in [6.5, 8.5), ‘C’ for grade in [5.0,
6.5), and ‘D’ for grade in [0, 5). Display the name of each student along with their rank and
classification with “Excellent” for ‘A’, “Good” for ‘B’, “Average” for ‘C’, and “Again” for ‘D’.
After that, print the percentage of each classification as a summary.
2


Student Name
John Smith
Peter Brown
Alice Depp
March Carter

CO1003
8.6
9
7
5.3

CO1001
7.5
8.3
6.2
4.2


CO1010
7
8
6.9
5.1

CO1005
8.3
9.1
7.4
4.5

2.4. Write a C program to group the following customers into the given four segments as follows
based on their transaction values:
Segment VIP: transaction value >= 160
Segment MEMBER: 120 <= transaction value < 160
Segment FRIENDLY: 80 <= transaction value < 120
Segment NONE: transaction value < 80
The customer list:
Full name
Scott Blair
John Smith
Peter Brown
Alice Depp
March Carter

Product identifier
45678901
23456789

45678901
12345678
12345678

Unit Price
10.9
24.8
10.9
12.5
12.5

Quantity
7
10
5
20
11

Display the customer name along with the transaction value and segment of each
customer. Also calculate and display the percentage and total value of each segment.
3. Start complex data processing
3.1. Write a C program to display all the possible moves of a knight from a given position on a
chess board input by a user from the keyboard. Each position on a chess board is
represented by a capital letter for a row from A to H and a number for a column from 1 to 8.
Some obstacles represented by a cross-check are supposed to be available on the board in
advance as follows. Each position can hold only one object which is either an obstacle or a
knight.
1
A
B

C
D
E
F
G
H

2

3

4

5

6

7

8

x
x
x
x
x

3



For example, a position of a knight is given at C5. All the possible moves of this knight are:
B3, D3, E4, E6, D7, and B7. The moves to A4 and A6 are impossible due to the obstacle at B5.
3.2. Write a C program to calculate the monthly salary of each employee in the following list:
Full name
Scott Blair
John Smith
Peter Brown
Alice Depp
March Carter

Standard Level
3.3
3.0
2.7
3.6
3.0

Incremental Level
A
B
A
C
D

Extra Hours
12
9.5
8
15
10


A unit amount for one standard level is 1.21, a unit amount for one extra hour is 0.2, a unit
amount for incremental level is 3.4 and then A level is 100% of a unit amount, B level is 75%
of a unit amount, C level is 50% of a unit amount, and D level is 25% of a unit amount.
If an employee receives more than 12 per month, it is possible for him/her to pay an income
tax. Therefore, display the information of each employee with their name, salary, and TAX if
they might pay an income tax or NON-TAX if not.
Also, list all the employees who have monthly salaries higher than a standard salary input by
a user from the keyboard. What is the percentage of those?
4. Check coding styles
For each C program that you have written, check coding styles with the following points:
- How have you used blank lines?
- How have you used tabs for alignment?
- How have you used comments in your source code files?
- Above all, how about your naming conventions? What have you named in your source code
files?

4



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×