Functions and Recursions – Arithmetic – Problem #6
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
You are to read in a positive integer n (n <= 20) and read n distinct positive integers into an array A. After that you are to recursively reverse the digits of each number of A and store them a separate array B. You will then sort both the arrays recursively, the original one (A) in ascending and one with reversed digits (B) in descending order. Then you will find themedian of the combined list of elements of both arrays (that is A and B). For this you will write appropriate recursive functions for (a) reversing a digit (b) sorting in ascending / descending order, with an argument for whether it is to be in ascending or descending order, (c) finding the median of 2 sorted arrays. You will print all intermediate parts appropriately. Save the file as A11_<Roll Number>.c (example A11_21AG10002.c). Build, Run and Test it for the given data as well as your own data. Then upload the .c file for the Assignment. [20 Marks: 5 marks for intermediate submission, 15 marks for final submission] In particular, you will do the following: 1. Read in a positive integer n and print it. Create a new line. Read in n positive integers in an array A and print them. 2. Reverse the digits of each array element of A to create a new number and store these in another array B. You will write a recursive function revdig(n) that reads an integer and returns a number with digits reversed (eg. 785 returns 587, 890 returns 98, 5 returns 5). You will call function revdig in the main program to reverse all the elements of A and store them in B. Print the elements of the array B. 3. Write a recursive sorting function rsort(….) with suitable arguments that you feel appropriate that takes an array and sorts it in ascending or descending order based on a binary variable called mode which is one of the parameters of rso You will call function rsort appropriately twice in the main program, once to sort A in ascending order (mode = 0) and the to sort B in descending order (mode = 1) and print these arrays A and B after sorting. 4. Write a recursive function rmed(….) with suitable arguments that you feel appropriate to find the median of all the elements of A and B. Call it from the main program and print the final median. [If you use non-recursive functions for any part where recursive functions are to be used, then the maximum you can get for that part will 25% of the marks allotted for it. If you do not write any function for some part, you will get a 0 for that part.] [Do not call any Library Functions other than scanf, printf. You may use your own user defined functions.] Test Data: Test it with your own data