Vous êtes sur la page 1sur 3

CPS Lab 2

August 18, 2015

Instructions
1. Naming conventions are to be followed with extreme precision. A script
will automatically evaluate your programs. And any deviation from the
prescribed folder names, file names or function names results in that
folder/file/program/function being discarded/unrecognized. This will only
reward you with 0 marks (for that particular question).
2. Hope you have all created a bitbucket account as per the invitation sent
to you guys (again). You will notice a shared repo already under your
username. This is where this lab assignment has to be finally uploaded.
3. Firstly create your own local repo titled lab-2. All your programs shall
be written within this repo.
4. Name each of your programs as per the question number. If the question
number is 1, then the program name should be 1.rkt.
5. Push the local lab-2 repository to the shared bitbucket repository.
6. Lab questions should be completed by the end of the lab session.
7. A set of practice questions have also been attached apart from the questions that are to be attempted in the lab. These may be attempted upon
completion and submission of those questions assigned for the lab time.

Lab Questions

Write the following functions in separate .rkt files as per the question numbers
indicated.

Q.N.

Function
name

Input

a,b,c
(3
natural
numbers)

swap

fibonacci

n (a natural
number)

Purpose/Description
Swap the numbers a,b,c
amongst themselves
ab
bc
ca
Note: Try not to use any extra variables

expSeries

x,n (2 natural numbers)

the

ex =

i!

and return the value of ex


Prints a (right-angled) number triangle of constantly increasing numbers.
The triangle should have n
lines.
There should be exactly i
numbers in the ith line.

numTri

n (a natural
number)

sumDigit

n (a natural
number)

Returns the sum of the digits


of the given natural number n

dec2BOH

n (a decimal
number)

Converts the decimal number


to binary, octal, hexadecimal
formats.

> (fibonacci 7)
0
1
1
2
3
5
8

> (expSeries 1 10)


2.718282

1+1+2 (Calculate i!=1,


th
Evaluate iP
term=1,
all
iterations=2)

> (numTri 5)
1
23
456
7 8 9 10
11 12 13 14 15

1.5+1.5 (1.5
marks each
for
each
loop)

> (sumDigit 263)


11

2+1 (Isolate
P
digits=2,
digits=1)

exponential

n
X
xi
i=0

> (dec2BOH 18)


Binary:10010
Octal: 22
Hexadecimal: 12
Total

Practice Questions (Optional)

These questions may be attempted once the first 5 have been submitted. Write
the following functions in separate .rkt files as per the question numbers indicated.

Marks

> (swap 5 10 7)
Before swap: a=5 b=10
2
c=7
After swap: a=7 b=5
c=10

Prints the first n elements of


the fibonacci series

Evaluates
function
3

Sample Implementation +
Output

2+2+2 (2 for
each conversion)
20

Q.N.

Function
name

Input

Purpose/Description

pascalTri

n (a natural
number)

Prints the pascals triangle


for n lines

palindrome

n (a natural
number)

Returns whether the given


number is palindromic or not.

tribonacci

n (a natural
number)

Prints the first n elements of


the tribonacci series

10a

bin2Dec

n (a binary
number)

Returns the decimal equivalent of the given binary number

10b

oct2Hex

n (an octal
number)

Returns the hexadecimal


equivalent of the given octal
number

11a

lcm

11b

hcf

a,b (2 natural numbers)


a,b (2 natural numbers)

Returns the LCM of the 2


numbers
Returns the HCF of the 2
numbers

All the best!

Sample Implementation +
Output
> (pascalTri 4)
1
121
12321
1234321
> (palindrome 1234)
#f
> (palindrome 25352)
#t
> (tribonacci 7)
0
1
1
2
4
7
13
> (bin2Dec 111)
7
> (oct2Hex #o34)
1C
> (oct2Hex 34)
1C
> (lcm 12 15)
60
> (hcf 12 20)
4

Vous aimerez peut-être aussi