I have been getting many queries requesting me to upload tutorials on how to write algorithms for a program in the ISC Computer Science Practical Examination.
Teaching how to write algorithms by writing an article on it is not that effective, but I decided to put down algorithms of two different programs which would serve as examples on how you should proceed with writing algorithms of your program.
Note: There are various ways in which you can write algorithms. I am putting down the easiest form of writing algorithm. This style is completely acceptable in ISC Computer Science Practical Examination and really easy to understand. All you need to do in order to understand the algorithms and write them, is by having in front of yourself, the program whose algorithm is given below. Go through the program and the algorithm and you will get an idea on how to write algorithms.
After all, writing algorithms isn’t a mammoth task as we see it to be. They are just logical steps written in order in an easy to understand language which when put together in a programming language will result in the solution.
Also Note: Before writing your program in your answer sheet, you need to write the algorithm for it. Now what I would suggest you to do is, when you get your answer sheet, solve the program you have selected using pencil at the end of your paper (last 2-3 pages). Write “Rough” above the pages.
Once you are done with writing the program in pencil, begin writing your answers in fair from the front page. Begin by writing the algorithm by seeing the rough program you wrote at the back.
Then Write the program in fair using pen, just after your algorithm. Then erase what you wrote in pencil at the last pages of your answer sheet.
I know you have to write the program twice but it has its own advantages like:
1) You can easily write the algorithm by seeing the program
2) Your final program in fair will be neat as you will just be copying it from the back pages.
3) If you started a program with pencil and after 10 minutes you realize that you should choose other program, you can do so as you haven’t written anything in pen yet.
Example of Algorithm of a program which contains only main() method
First see this program: http://www.guideforschool.com/1260759-java-program-to-print-circular-spiral-matrix/
Step 1 : Start of algotithm
Step 2 : Input the size of circular matrix and store it in integer variable ‘n’
Step 3 : Create an integer square array of size ‘n*n’ which will be the circular matrix
Step 4 : Declare and initialize variables k = 0 (for filling the matrix), c1 = 0 (for storing index of first column), c2 = n-1 (for storing index of last column), r1 = 0 (for storing index of first row), r2 = n-1 (for storing index of last row)
Step 5 : Start a while loop till k<=n*n and repeat steps 6 to 10
Step 6 :
(a) Start a for loop from i = c1 to c2, where ‘i’ increases by 1 every time and perform step (b)
(b) Store the natural numbers in the first row using A[r1][i] = k++
Step 7 :
(a) Start a for loop from j = r1+1 to r2, where ‘j’ increases by 1 every time and perform step (b)
(b) Store the natural numbers in the last column using A[j][c2] = k++
Step 8 :
(a) Start a for loop from i = c2-1 to c1, where ‘i’ decreases by 1 every time and perform step (b)
(b) Store the natural numbers in the last row using A[r2][i] = k++
Step 9 :
(a) Start a for loop from j = r2-1 to r1+1, where ‘j’ decreases by 1 every time and perform step (b)
(b) Store the natural numbers in the first column using A[j][c1] = k++
Step 10 : Update the variables c1, c2, r1 and r2
Step 11 : Display the circular matrix A[ ]
Step 12 : End of Algorithm
Example of Algorithm of a program which contains other methods along with the main() method
First see this program: http://www.guideforschool.com/358-isc-2013-question-3-practical-paper-solved/
Note: In such cases, start with first writing the algorithm for the main() method and then the algorithm for the other methods.
Algorithm for main() method :
Step 1 : Start of algotithm
Step 2 : Input the sentence and store it in a String variable ‘s’
Step 3 : Convert the sentence into upper case
Step 4 : Create a StringTokenizer object ‘str’ to extract tokens (words) from the sentence using space and other the punctuation marks namely ‘.’, ‘?’, ‘!’
Step 5 : Count the number of tokens (words) and store it in an integer variable ‘c’. Also create a String array word[ ] of size ‘c’
Step 6 : Start a for loop from i = 0 to less than ‘c’ and store the tokens of the sentence into the word [ ] array
Step 7 : Declare an integer variable ‘count’ and initialize it with 0
Step 8 : Start a for loop from i = 0 to less than ‘c’ and repeat step 9
Step 9 : Call the function isPalin() as : ob.isPalin(word[i]). If the returned value is ‘true’ then increase the count variable and print the word.
Step 10 : If count of palindromic words is not equal to zero, then print the value stored in the variable ‘count’
Step 11 : End of algorithm for main() method
Algorithm for function boolean isPalin(String s) :
Step 1 : Start of algorithm for function isPalin()
Step 2 : Find the length of the String ‘s and store it in an integer variable ‘l’
Step 3 : Declare and initialize a String variable rev=”” for storing the reverse of the String ‘s’
Step 4 : Start a reverse for loop from i = l-1 to 0 and repeat step 5
Step 5 : Extract characters from the end of the original string and add them to the variable ‘rev’
Step 6 : If the reverse word obtained (rev) is equal to the original String (s), then return true, otherwise return false.
Step 7 : End of algorithm for the function isPalin().
Similarly if you have more functions, then just write their algorithms in a similar fashion one after the other.
This is how you can write algorithms in your ISC Practical Examination. So, as you see, this is nothing but step wise solution to the given program in English language. So just relax! It isn’t a big deal to write algorithms now. You can easily get a 10/10 in the algorithm part now. Smile.