Which Quadrant - Introduction to Computing Using MATLAB | CS 1112, Assignments of Computer Science

Material Type: Assignment; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Spring 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/30/2009

koofers-user-ajr
koofers-user-ajr 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Section Exercise 3
1 Which quadrant? (re-visited)
[Last week’s challenge question] Write a script using nested conditional statements without elseif to determine
in which quadrant a user-input value of Adegrees belongs. Assume that the user may enter any non-negative
number. For example, 725is the same, and should be treated, as 5. (Hint: the function rem that you saw last
week might be useful.) To avoid ambiguity, we use the following convention:
Quadrant is
1if0A<90
2 if 90 A<180
3 if 180 A<270
4 if 270 A<360
2 Multiples of k
The following program reads an integer kand outputs all the multiples of kup to 1000. Fill in the blank.
k = input(’Please enter a positive integer smaller than 1000: ’);
for j = ______________________
fprintf(’%d ’, j);
end
fprintf(’\n’);
3 Approximate π
[Modified from Insight Exercise P2.1.5] For large n,
Tn=1+ 1
22+···+1
n2=
n
X
k=1
1
k2π2
6
Rn=11
3+···− (1)n+1
2n1=
n
X
k=1
(1)k+1
2k1π
4
giving two different ways to estimate π:
τn=p6Tn
ρn=4Rn
Write a script that displays the value of |πρn|and |πτn|for n= 100 : 100 : 1000 in one table.
4 Divisors
Write a script to print all the numbers between 1 and n, exclusive, that divide n(without a remainder using
integer division). nis a user input positive integer. (Hint: Remember function rem?)
Optional Challenge Question
Refer to Question 4 and write a script to print the prime numbers that divide n. Do not use any built-in functions
other than rem.
1

Partial preview of the text

Download Which Quadrant - Introduction to Computing Using MATLAB | CS 1112 and more Assignments Computer Science in PDF only on Docsity!

CS1112 Section Exercise 3

1 Which quadrant? (re-visited)

[Last week’s challenge question] Write a script using nested conditional statements without elseif to determine in which quadrant a user-input value of A degrees belongs. Assume that the user may enter any non-negative number. For example, 725◦^ is the same, and should be treated, as 5◦. (Hint: the function rem that you saw last week might be useful.) To avoid ambiguity, we use the following convention:

Quadrant is

1 if 0 ≤ A < 90 2 if 90 ≤ A < 180 3 if 180 ≤ A < 270 4 if 270 ≤ A < 360

2 Multiples of k

The following program reads an integer k and outputs all the multiples of k up to 1000. Fill in the blank.

k = input(’Please enter a positive integer smaller than 1000: ’);

for j = ______________________ fprintf(’%d ’, j); end fprintf(’\n’);

3 Approximate π

[Modified from Insight Exercise P2.1.5] For large n,

Tn = 1 +^1 22

n^2

∑^ n

k=

k^2 ≈ π

2 6

Rn = 1 − 13 + · · · − (−1)n+ 2 n − 1 =

∑^ n

k=

(−1)k+ 2 k − 1 ≈ π 4

giving two different ways to estimate π:

τn =

6 Tn ρn = 4 Rn

Write a script that displays the value of |π − ρn| and |π − τn| for n = 100 : 100 : 1000 in one table.

4 Divisors

Write a script to print all the numbers between 1 and n, exclusive, that divide n (without a remainder using integer division). n is a user input positive integer. (Hint: Remember function rem?)

Optional Challenge Question

Refer to Question 4 and write a script to print the prime numbers that divide n. Do not use any built-in functions other than rem.