SPL Programming Exercise - Chapter 1 Doing Arithmetic

 

1.1 Data

1. Write 3.5*108 in a programming language.

2. Have a try


A B C
1 =2+5 =2-5 =2*5
2 =10/5 =10\5 =10%5
3 =11/5 =11\5 =11%5
4 =2+3*30 =(2+3)*30 =2.0+3*30
5 =4194304*4194304 =4194304*4194304*4194304 =4194304*4194304*4194304.0

ⅰ Check result of the code in every cell and find which are integers and which are floating point numbers.

ⅱ Try running the above code and observe the result.

1.2 Variable and statement

1. Write a piece of code to calculate the area of a square.

When side length is 3, calculate area of the square;

Increase the square’s side length two times and calculate the area.

2. Write a piece of code to convert Fahrenheit to Celsius using the formula C=(F-32)*5/9.

Enter F=68 and calculate Celsius.

Enter F=100 and calculate Celsius.

3. Write a piece of code to calculate the volume of a hollow cylinder. Enter outer diameter R, inner diameter r and height h to calculate the volume.

Tips: The formula for calculating volume of a cylinder is ...

4. Give meanings of the following statements.

x+=y
x-=y
x*=y
x/=y
x%=y

5. Find value of A1 when each line of the following codes is executed.


A
1 5
2 =A1+=3
3 =A1-=2
4 =A1*=5
5 =A1*=A2+2
6 =A1\=2
7 =A1%=2

1.3 Function

1. Find result of the piece of code below after it is executed.


A B
1 =ceil(9.2) =ceil(-9.8)
2 =floor(9.2) =floor(-9.8)
3 =lg(10) =lg(100)
4 =power(2,7) =power(900,0.5)
5 =sqrt(900) =sqrt(16,4)
6 =exp(1) =exp(2)

2. Reference Function Reference and find the right function to finish the following programming exercises.

ⅰ Calculate absolute value of -3245;

ⅱ Input three random numbers and find the largest value;

ⅲ Generate three random numbers within [0,100] using rand() function to represent the exam scores, and calculate the total score and average score.

ⅳ Write a piece of code to implement the coordinate rotation formula. Input .. and the angle (rotation angle) and output x, y.

..

Tips: ..

ⅴ Input coordinate (x,y) of two points and output the distance between the two points.

Formular of distance between two points: ..

Answers:

1.1 Data

1. 3.5E8

2. Results of A1, B1, C1, B2, C2, B3, C3, A4, B4 and A5 are integers; result of B5 is out of range; results of other cells are floating point numbers.

1.2 Variable and statement

1.


A
1 3
2 =A1*A1
3 >A1=2*A1
4 =A1*A1

2.


A
1 >F=68
2 =(F-32)*5/9
3 >F=100
4 =(F-32)*5/9

3.


A B C
1 >R=8 >r=3 >h=5
2 =3.14*R*R*h-3.14*r*r*h

1.3 Function

2.


A
1 =abs(-3245)


A B C
1 1 2 3
2 =max(A1,B1,C1)


A B C
1 =rand(100) =rand(100) =rand(100)
2 =sum(A1,B1,C1)
3 =avg(A1,B1,C1)


A B C
1 3 4 90
2 =C1*pi()/180
3 =A1*cos(A2)-B1*sin(A2)
4 =A1*sin(A2)+B1*cos(A2)
5 >output(A3,A4)


A B
1 3 4
2 4 3
3 =sqrt(power(A2-A1)+power(B2-B1))