Name : Ashutosh Raghuwanshi
Enrollment Number : 031795362   (MCA-1 Batch-B)
Course Code : CS-02
Course Title : Introduction to Software
Assignment Number : MCA(1)-02/Project/03

Assignment:

Question 1: Write shell programs for the following:

  1. To search for a number in a given list of numbers.
  2. To reverse a seven digit number and find the sum of all digits.
  3. To sort a given list of numbers in descending order.
  4. To count the number of positive and negative numbers in a given list.
  5. To check whether the given number is a perfect number or not.

Solution:


Flowchart for Searching

Introduction

 All these programs are more or less iterative by nature. So, let's first look at the iterative paradigm of programming. We will take the search program as our example for this purpose. For searching an element from an arbitrary list definitely we have to go to each element and match it with the desired element so it means to say that we are going to do same task iteratively. In such cases we don't repeat the parts of our programs; instead we put them in a loop so that the same piece of code can be repeated multiple times. This may be better represented by a flowchart. Look at the accompanying figure; here you can clearly see a loop between box-7 and box-4. The control is getting transferred back to box-4 on a given condition so that the given piece of code can be repeated until there is nothing left in the list to be matched with the number. We can also break the loop as shown in box-6. There must be a condition for getting out of the loop so that the program remains finite.


Scripts

 So! Here are the shell scripts. These are written as to be compatible with System V 3.2 (as specified in the syllabus) but since it is a very old version and hard to find so I just tested them on Linux's ash (a Bourne shell clone) and bash (Bourne Again SHell), and they are working fine. I have used read command to get data input instead of command-line parameters because it is more interactive and user friendly. Though it increased a bit of complexity but that would also increase if I had to support any number of parameters (more than just $1 - $9), so it is worth considering. One more thing that I would like to tell you here is that I have used a better mechanism for specifying the shell. Instead of using '#' (for 'C' Shell) and ':' (for Bourne Shell), I