Lab 7
Lab 7
In this Lab, you will solve the problems that were asked in the Lab Test. Since you haver all seen
the questions and discussed among yourselves (-:) ), the deadline for the same is Friday 16th
(6PM). You can do the Labs (outside the designated hours) and come to the Lab and meet the
TAs. This week's Lab will not have marks for attendance -- it will only be for practice and doubt
clearing (if any). The solutions for the Lab will be posted at 7PM on the 16th and hence there is
no deadline extension or late submission.
1. Write a function [myReverse.m] which will reverse a vector containing a non empty set of
single digit numbers.
Input: [1 2 3 6 ]
Output: [6 3 2 1]
[1 mark]
2. Write a script/function called [myMountain.m] that prompts the user for a word or phrase,
then prints out that phrase as a text mountain, as follows [the triangle shape is mandatory]:
>> myMountain.m
Enter your text: mountain
[2 marks]
3. A number can always be represented as a sum of squares of other numbers. Note that 1 is a
square and we can always break any number as (1*1 + 1*1 + 1*1 + …). Write a recursive
function which takes a number n and returns the minimum number of squares that sum to n.
Input: n = 100
Output: 1
Explanation:
100 can be written as 102. Note that 100 can also be written as 52 + 52 + 52
+ 52, but this representation requires 4 squares.
Input: n = 6
Output: 3 [12 +12+ 12]
[4 marks]
4. Write a function [mySumDigit.m] which will return the sum of digits of a number.
Input: 2356
Output: 16
[1 mark]
5. Write a script function [mySumOddEven.m] that first asks the user to enter an integer
between 1 and 200. You may assume that the user will only enter valid input numbers for this
Lab 7
Please solve all problems given in the Lab test
task. If the number is an odd number, the program should sum up all the odd numbers from
1 to that number. If the number is an even number, the program should sum up all the even
numbers from 0 to that number.
>> mySumOddEven
Please enter an integer (between 1 and 200): 5
The number you entered is 5, which is an odd number.
So we sum up all the odd numbers from 1 to 5, and the sum is 9.
>> mySumOddEven.
Please enter an integer (between 1 and 200): 20
The number you entered is 20, which is an even number.
So we sum up all the even numbers from 0 to 20, and the sum is 110.
[2 marks]
6. You can come to the LHC rooms using stairs. Clearly for each floor, you will be able to do it
in some number of “Steps”. You can hop either 1 step, 2 steps, or 3 steps at a time [Step].
Implement a recursive method to count how many possible ways you can run [or walk J] up
the stairs and also return the minimum and maximum number of Steps [combination of 1 step,
2 steps and 3 steps] that is required.
[4 marks]
7. Write a function [myMatrixElemSum.m] takes a matrix as an input and returns the sum of all
the elements of the matrix.
11 2
Input:
3 5
Output: 21
[1 marks]
8. Write a script/function that takes a non-negative integer as a parameter and returns the binary
representation of that number (as a string of 0's and 1's).
[2 marks]
9. Given a “2 x n” board and tiles of size “2 x 1”, count the number of ways to tile the given
board using the 2 x 1 tiles. A tile can either be placed horizontally i.e., as a 1 x 2 tile or
vertically i.e., as 2 x 1 tile.
Input: n = 4
Output: 5
Explanation:
For a 2 x 4 board, there are 5 ways
Input: n = 3
Output: 3
Explanation:
Lab 7
Please solve all problems given in the Lab test