0% found this document useful (0 votes)
93 views7 pages

Assignment IV - Recursion: Question 2 and 4, You Can Assume The Input Is Always Correct (You Are Not Required To Test It)

This document provides instructions for Assignment IV on recursion. It includes: 1. An introduction stating the assignment will test comprehension of recursion and ability to implement recursive procedures. 2. Coding requirements prohibiting loops and use of additional arrays except for type conversion. Functions must match prototypes exactly. 3. Clarification on required input verification, stating only logical checks are needed unless specified. 4. Four questions testing recursion and recursive functions working with binary numbers, strings, merging strings, and finding shortest paths in matrices. Detailed requirements and restrictions are provided for each question.

Uploaded by

S.k. Hussain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views7 pages

Assignment IV - Recursion: Question 2 and 4, You Can Assume The Input Is Always Correct (You Are Not Required To Test It)

This document provides instructions for Assignment IV on recursion. It includes: 1. An introduction stating the assignment will test comprehension of recursion and ability to implement recursive procedures. 2. Coding requirements prohibiting loops and use of additional arrays except for type conversion. Functions must match prototypes exactly. 3. Clarification on required input verification, stating only logical checks are needed unless specified. 4. Four questions testing recursion and recursive functions working with binary numbers, strings, merging strings, and finding shortest paths in matrices. Detailed requirements and restrictions are provided for each question.

Uploaded by

S.k. Hussain
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Assignment IV Recursion

I.

Introduction
This Assignment will test your comprehension of the recursion concept, as well as your ability to implement it by programming a few simple procedures in a recursive manner.

II.

Coding requirements
Since this is an assignment designed to test your ability to use recursion, using iterative methods will be prohibited. Therefore, no loops are allowed. o Your code should not contain any loop commands such as for or while (also in main function). You are not allowed to use any library functions, except of printf, scanf, getchar, puts and gets. You are not allowed to use pointers and static variables. You are not allowed to create additional sArrays, with the exception of sArrays intended to convert input between types. Once youve called the functions described explicitly in this assignment, you are no longer allowed to change these sArrays. Each question that describes a function prototype. You must implement your answer in a function with exactly the same prototype as the one described in the question. Failure to do so will cause the testing script to fail for your code, resulting in lower grades. BESIDE Question 2 and 4, You can assume the input is always correct (you are not required to test it) o Input sArrays will never contain more than 28 elements. Your output should be exact to the one created by the sample executable supplied with this assignment. Main function: o The main function, like all other code in this assignment, cant use loops. Thus, you might need recursive procedures that handle inputs. o Main function can use input / output handling library functions (also true for specific additional procedures you may write for that purpose). o All return values from the required functions should be printed to output outside these functions.

III.

INPUT VERIFICATION

Since there were a lot of question about that, I will explain in details what you are supposed to check in the user input: 1. Philosophy: a. The program is not Stupid Proof software!! If you should expect a number, you will get a number not a character! You can safely use %d when you think that what you need! b. The only verification are logic verification ie if you are required to ask the size of an array and you know that it is allowed to have less than 15 characters so you must check that the size smaller!! c. You wont be tested on things that were not required d. As soon as you find an incorrect input, you should print an error message according the exe file and restart the menu! e. If you have any doubts, write on the forum and I answer 3 times a day 2. As detailed requirement as possible per Question: a. XOR: i. You arent required to perform any check: The length will always be <= 8 The number entered will be a binary number {0,1}. ii. Although you do not need to check, your software shouldnt get crazy if the number entered is 150000. It will just have to give a garbage output! b. FindWord: i. You are required to perform any check: ii. You will get 2 strings that are smaller than 256 and just perform the task! c. Merge: i. You should verify that the characters entered in both strings are letters ONLY ii. You must check that the result string length is smaller than 256 including \0. d. ShorthestPath: i. Only logical verification such as: Matrix size Starting Point vs Ending point and such ii. Simply think and use your instinct following the Philosophy of this homework! The GOAL is for you to know how to create a recursive algorithm, write recursive functions and debug them!!

IV.

Question I (20%)
Function Prototype: int iBinaryXor(int iFirstNumber[], int iSecondNumber[], int iLength) Input: iFirstNumber, iISecondNumber sArrays with elements from {0,1}. iLength the length (number of elements) of both of the sArrays. 0<length<8. Required Output: (return value) iIFirstNumber xor iISecondNumber in decimal basis. This question resolves around binary numbers. For those of you who arent familiar with the binary basis, you can read a little in: http://en.wikipedia.org/wiki/Binary_numeral_system An sArray represents a binary number in the following manner: each element in the sArray represents a bit in the number. The element in position [0] is the most significant bit (MSB), and in position [length-1] is the least significant bit (LSB). Example: Let iFirstNumber be {1, 0, 0, 1, 0}. iFirstNumber represents the binary number 100102 which is 1810 (XY means X in Y basis). We could convert it to decimal basis in the following manner: 100102 = 20*0 + 21*1 + 22*0 + 23*0 + 24*1 = 2 + 16 = 1810. The XOR operation is defined as follows for 2 bits: Value of X Value of Y Value of X xor Y 0 0 0 0 1 1 1 0 1 1 1 0 XOR for 2 binary numbers is defined as follows: if X and Y are both binary numbers, Then the ith bit in (X xor Y) is (ith bit of X xor ith bit of Y). Example: iFirstNumber={1,0,0,1}, iSecondNumber ={1, 1, 0, 0}, length = 4. iFirstNumber represents 10012, iSecondNumber represents 11002. Their XOR is 01012. iBinaryXorreceives 2 sArrays representing a binary number each. It should calculate the XOR of these 2 numbers, convert it to decimal and return this value. Example: Continuing previous example, the function should return and print 5, since 510 = 01012. Notice: The sArrays are of same Length (given as a parameter) that is smaller than 9 (ie max 8!!). Thus, the 2 binary numbers represented have the same number of bits. Restrictions: you are not allowed to change sArrays in the iBinaryXorFunction; you are allowed to use any additional functions inside iBinaryXorfunction but you can use only two other recursive functions outside (such as to set up the arrays ).

V.

Question II (25%)
#define True 1 #define False 0 Function Prototype: int iFindWord(char sArr[], char sWord[],int iLength[1], int iArrIndex,int iWordIndex); Input: sArr, sWord strings. 256>Length[sArr] >= Length[word] >= 2. Required Output (return value): Define if the sWord is included in the sArr return True or False We say sArr contains sWord if there is a sub-string (a part of sArr, starting at index i and ending at index j) in which all the letters of sWord appear in the same order. Notice: Length[Sub-String] >= Length[word] we allow other characters to appear between the appearances of letters of the word. Example: sArr = do no good, sWord= dog. The Sub-String do no g contains the sWorddog since the letters of dog appear in same order in it. Example: sArr = good is done, sWord= dog. The sArray does not contain the word, since although all the letters of the sWordappear in the sArray, they dont appear in correct order. We say a letter in the sub-string Matches a letter in sWordif it is the same. A match is the assignment of every letter of the sWordto a specific letter in the sub-string. Example: Consider again = do no good, sWord= dog. The Sub-String do no g can be matched in 2 ways: 1. do no g 2. do no g We say that a given match legally matches sWordif the following holds: All letters of sWord appear in order in the sub-string. The sum of distances between the matched letters and the start of the string is minimal. Example: Consider once more = do no good, sWord= dog. We look again at the 2 possible matches we can do with the sub-string do no g: 1. do no g this is a legal match: (accumulated distance = 8) 2. do no g this is a illegal match: since the accumulated distance from the start of the string (10) is higher than the example above. The function should find the length of the shortest sub-string of sArr that legally matches the sWord(There can more than one with shortest length). If no legal match exists, the return value is FASLE in other hand if there is a match,te return value is TRUE and the iLength array is updated according the above definition. Restrictions: you are not allowed to change sArrays; you are not allowed to use additional (helper) functions inside the iFindWord function but can add outside it no more than one recursive function!

VI.

Question III (25%)


#define True 1 #define False 0 Function Prototype: int iMergeStrings(char str1[], int index1,char str2[],int index2, char result[], int index3) Input: str1, str2,result 2 Input strings index1, index2, index3 3 integers that can be used as you desire Required Output: (return value) True or False Result - the string that contains the merging of the 2 input strings The function has two purpose: 1. Define if both input strings are composed only of letters 2. Define if each string is in a raising order in term of ascii value 3. Merge both strings into the result string such as the result string is in an raising order By raising order we define that the ascii value of str[i]<=str[i+1]. If 1 and 2 are correct the function should return True otherwise False. If the function return True, the result string should be printed from outside the function otherwise a notification will be printed to the user and there is no importance to the result string. The maximal result string length should be 256 including \0 - you will need then to verify that the merge length is inferior to 256. In the case the length is bigger, the function will return False. Restrictions: you are not allowed to change str1 or str2; you are not allowed to use any additional (helper) functions inside the iMergeStrings function. You can use outside the function ONLY 1 outside function to Get the User Input. You cannot use any function from any library beside what is allowed (see Coding requirement section)

VII.

Question IV(30%)
This question is based on a very popular issue that is deal in many different fields such as image processing, civil engineering and also asked by many human being on earth. What is the shortest path? The goal of this question is to get you familiar with 2D arrays and also learn more about the recursion process. Problem Definition: The user will be proposed to enter the 2D array that can symbolize a city or an image for example. The program will determine the shortest path from a starting point to an ending point. Input: 2D Array Starting Point Ending Point Required Output: Shortest Path Total Path Distance To move from the starting point to the ending point, the next steps are allowed in this exact order: 1. rightward 2. diagonally to the down-right 3. straight down 4. diagonally to the down-left 5. leftward Hence starting in a position (row=i, column=j), the algorithm consider the next position (i,j+1), (i+1,j+1), (i+1,j), (i+1,j-1) or (i,j-1) provided that the target position is within the grid boundaries. Notice: The maximal array size will be 18x18 The path is the sequence of positions that has been visited, including the starting and ending positions. Positions in the grid have associated distances (>0 and <100), which are specified in the distance matrix. The same sample cannot be taken in consideration twice in a specific path. The Path Distance is the sum of distances of all the samples visited on the path. Example:

1 2 2 1

2 4 2 1

3 5 1 5

2 1 6 4

For example, the underlined numbers in the figure above illustrate a path (0,2)>(1,2)->(2,1)->(3,0) that starts at sample (0,2) and ends at sample (3,0) with total distance of 11. Note that this is not the shortest path that starts at (0,2) and ends at (3,0). The shortest path in this case is (0,2)->(1,3)->(2,2)->(3,1)->(3,0) with total distance of 7 . It is shown in the following figure:

1 2 2 1

2 4 2 1

3 5 1 5

2 1 6 4

Restrictions: you are allowed to add a maximum of five recursive helper functions as . No regular functions will be accepted! You are not allowed to use static variables! As soon as you get a wrong input, you are requested to stop the operation and restart the Menu!!

You might also like