COL100 Assignment 5 Solved

30.00 $

Category: Tags: , ,
Click Category Button to View Your Next Assignment | Homework

You'll get a download link with a: zip solution files instantly, after Payment

Securely Powered by: Secure Checkout

Description

5/5 - (2 votes)

IMPORTANT NOTE: This assignment is based on functions we want you to make functions that take inputs as arguments and return the result. We will check your code by calling the functions created by you along with checking the output thus please make sure to make functions. The naming, arguments and return value are specified in each question. You can do input/output inside or outside the function as long as it returns the correct value as well as printing it.

1 Calculator(5 marks)(In Lab Component)

This question uses functions to do the calculator questions done in the previous assignments. We had asked you to print the result of some mathematical operations in assignment 2 and 3. In this assignment you will be creating different functions for the same operations. We expect the following functions:

• Addition
• Subtraction
• Multiplication • Division
• Modulus

The name of the function must be the exact same as the operation i.e. for addition we want a function named addition(a,b) where a and b are integers. Since each of these is a binary function they will

take in two integer inputs and return the result. The return type is an integer.

Note: The function names must be in lowercase.

2 Factorial Using Recursion(5 marks) (In Lab Component)

Write a program to calculate the factorial of a number using recursion. You created a program that calculated the factorial of a number using loops and now you must create a function that recursively computes the factorial. Your function must be named factorial(a) where a is the integer argument whose factorial must be returned by the function. The return type is an integer.

Example 1:

INPUT: 11

OUTPUT: 1 1.00

EXPLANATION:
The factorial of 1 is equal to 1

Example 2:

INPUT: 15

OUTPUT: 1 120.00

EXPLANATION:
The factorial of 5 is equal to 120

Example 3:

INPUT: 1 10

OUTPUT:
1 3628800.00

EXPLANATION:
The factorial of 10 is equal to 3628800

Note: We will be checking your code for recursive functions thus calculating factorial using loops will be given a 0.

3 Padovan Sequence Using Recursion (10 marks)

Padovan sequence is similar to the Fibonacci sequence. In Padovan sequence P(n) = P(n−2)+P(n−3) where p(0), p(1), p(2) = 1 unlike Fibonacci sequence in which f(n) = f(n−1)+f(n−2) where f(0) = 0, f(1) = 1. We will calculate the nth number of the padovan sequence using recursion. You must create a function that recursively computes the nth number of the padovan sequence. Your function must be named padovan(a) and will have one integer argument called a and you will return the ath integer of

2

the padovan sequence. The return type and argument is an integer.

Example 1:

INPUT: 11

OUTPUT: 1 1.00

EXPLANATION:
The padovan of 1 is equal to 1

Example 2:

INPUT: 15

OUTPUT: 1 3.00

EXPLANATION:
The padovan of 5 is equal to p(5)=p(3)+p(2) where p(3)=p(1)+p(0)=2 and p(2)=1

Example 3:

INPUT: 1 10

OUTPUT: 1 12.00

EXPLANATION:
The padovan of 10 is p(10)=p(8)+p(7) where p(8)=7 and p(7)=5

Note: We will be checking your code for recursive functions thus calculating padovan using loops will be given a 0.

4 Number of words in a sentence(10 marks)

You will be given N sentences. Write a program to count the number of words in each sentence and print the number of words in the sentence which has minimum words from the given sentences. You have to create a function called numwords() that takes an array of strings(sentences) and returns the value of the number of words in the string(sentence) with minimum words. The first line of input will be the number of sentences given. Two words may be separated by any number of commas, spaces or period and a word contains any number (one or more than one) of alphabets i.e. a-z and A-Z. See examples for more clarification.

Note: The quotes are not part of the input and are just there to identify strings. For exam- ple they are needed in Example 2 to show spaces, we will not give quotes in actual input. Sentences may or may not be grammatically correct and the words may or may not have a meaning and will contain one or more than one letters.

Example 1:

INPUT:

3

4

"there is a buffalo"
"buffalo is male gender"
"cow is female animal"
"buffalo and cow are of same family"

1 2 3 4 5

OUTPUT: 1 4.00

EXPLANATION: “there is a buffalo”, “buffalo is male gender”,”cow is female animal” are the sentences with minimum four words, “buffalo and cow are of same family” have 7 words.

Example 2:

INPUT:

1 2 3 4

OUTPUT: 1 0.00

EXPLANATION: There are no words in “” and ” “, and 6 words in “This is computer science programming assignment”

Example 3:

INPUT:

1 2 3 4 5

OUTPUT: 1 2.00

EXPLANATION: “Id disagree” has two words – Id and disagree and is thus the sentence of the minimum words.

Example 4:

INPUT:

1 2 3

OUTPUT: 1 2.00

EXPLANATION: The first sentence has 4 words and the second sentence has 2.
Note: You have to return the number of words in the smallest sentence and not the smallest sentence.

3

“This is computer science programming assignment” “”
“”

4

"New Assignment of COL"
"The assignment is harder,"
"than the previous ones"
"Id disagree"

2

"A,a,a    A"
"b    b"

4

5 Figure out the pattern(15 marks)

Write a recursive program to print the pattern shown for any positive number N. Your task is to figure out how the pattern can be printed using recursion. You cannot use loops to print this pattern, any student found using loops will be given a 0. The name of the function created must be pattern(a) where a is the integer input for the function. The function returns the pattern as a string which has to be printed to the terminal. As in previous questions you may print it outside the function or inside.

NOTE: You DO NOT have to print to 2 decimal places here. Example 1:

INPUT: 11

OUTPUT: 1 111

EXPLANATION: The base case for the pattern is 111 for the number 1. Example 2:

INPUT: 12

OUTPUT: 1 211121112

EXPLANATION: Notice the repetition in the pattern – the pattern can be looked as 2, 111, 2, 111, 2. Example 3:

INPUT: 13

OUTPUT:
1 321112111232111211123

EXPLANATION: If you notice the pattern the pattern is 3, 211121112, 3, 211121112, 3 Example 4:
INPUT:

14 OUTPUT:

1 432111211123211121112343211121112321112111234
EXPLANATION: If you notice the pattern the pattern is 4 then 321112111232111211123 then 321112111232111211123

then 4.
Hint: What does the pattern “321112111232111211123” indicate in the 4th example?

5

  • A5-9nmau4.zip