Monday , 16 September 2019
Latest Posts
Home » Class 12 » Advice on How to write Algorithm in ISC Computer Practical

# Advice on How to write Algorithm in ISC Computer Practical

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 1:

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 2:

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.

## Infix Postfix Prefix Conversion – Correct Method (ISC Computer Science)

In this video we discuss about the correct way of converting an Infix Expression to Postfix and Prefix.

1. Sir, can we use split function for splitting the strings, separated by different punctuation marks.
Example, Hello! How are you? I hope everything is fine.
If so, then how?

• Hello Shilesh,

split() function splits a string into tokens (words) and returns the result in the form of a String array.
Example: If String s = “Guide For School”, then String w[]=s.split(” “); will save the words “Guide”, “For” and “School” in the array w[]

Just note: If there are 2 spaces in between any words or a fullstop followed by a space as in “Java is Fun. Said none”, the above code needs to be modified otherwise it will store null strings also in the array.
Just use this syntax: String word[]=s.split(“[. ]+”);
where ‘s’ is the sentence. ‘+’ denotes any possible combinations of the punctuators mentioned in the []. Similarly you can use other punctuation marks by putting them inside like this “[?!,;. ]+”

There are other alternatives as well, you can use StringTokenizer class.

Instead of these lines:

```String word[]=s.split("[.?! ]+"); // Saving the words in an array using split()
int c = word.length; // Finding the number of words```

write the following:

```StringTokenizer str = new StringTokenizer(s,".?! ");
int c = str.countTokens();
String word[] = new String[c];
for(int i=0; i<c; i++)
{
word[i] = str.nextToken();
}```

Regards,
guideforschool

2. I have 2 questions sir-
1. Is it important to write the algorithm before the program?
2. We have to write our name and roll number within the comment line at the beginning of the program or not?

• 1. Yes
2. Not compulsory, but you can write

3. System.out.print("Enter any sentence : ");
String s = br.readLine();
int l = s.length();
int pos=0;
String w;
for(int i=0;i<l;i++)
{
if(s.charAt(i)==’ ‘)
{
w = n.substring(pos,i);

/* Write the operation you want to perform with ‘w’ here.
* You can also send ‘w’ to any function by calling it here
*/

pos=i+1;
}
}
Sir , in the above code for extracting words from a sentence you used the statement “i<” in the for loop condition which I could not understand. So I request you to kindly explain me the purpose of using this statement.
Thank you.

• It was an html rendering issue. It is solved now.

4. It is a lovely article which you wrote. Thanks a lot sir. I will follow your method in my exam tommorow

5. Thank you so much sir. This is the best site ever!

6. Thank u sir very helpful

7. Sir please upload some questions on outputs of yheory paper.
Thank you for creating such a wonderful website for us students

8. Thanks a lot sir….could see the pains that you have taken to compile such valuable articles as i read it…..that will be really helpful to all the students across India……..

Sir and a question
do we have to document the written program?

And variable discription should be given as a table or will it do if I describe it along with its declaration as a part of documentation?

Sir please reply as soon as you can because 17 is our practical exam…..

• Thank you for your kind words.
Variable description should be given as a table.
Yes document the important steps.

• thanks a lot sir for taking time from your busy schedule to solve my query….

9. sir i am your fan
its really very helpful

10. I’d love just a few more algorithm examples but these are really helpful! Thanks so much!

11. is it needed to do variable listing in ISC theory paper of programs attempted.

• Yes

12. will i get full marks by writing this type of algo

• Yes.

13. how to run a program of inheritance in bluej?? plzzz help

• 14. sir,should we write comments in the algorithm if we are writing in pseudo code…

• 15. is it must we have to use tokeniser in the 3rd string program?

• No, you can do it without using StringTokenizer also.

16. is it necessary to write comments in the program? or is it okay if we just write the variable description? please reply asap sir.

• Do write the comments also. Just 7-8 comments will do.

• u mean just a few comments for the main logic of the program will do?

• 17. we don’t have to write flowchart , sir??

• 18. • The 3rd Instruction in the practical question paper is referred to as the documentation:
“3. Document the program using mnemonic names / comments, identifying and clearly describing the choice of data types and meaning of variables.”

In this you need to write comments in your program and after you have finished writing your program, write the Data Description Table for it which consists of mostly 3 columns – Identifier/Variable Name, Data Type and Purpose.

For example: The data description table for the below given code would be:

Code:

```void isPrime(int n)
{
int c = 0;
for(int i = 1; i<=n; i++)
{
if(n%i == 0)
c++;
}
if(c == 2)
System.out.println("The number is Prime");
else
System.out.println("The number is Not Prime");
}
```

Data Description:

Identifier Name Data Type Purpose
n int variable to store the number
c int variable to count the number of factors of n
i int loop control variable

Hope this helps.
Regards,
guideforschool

19. sir can u  post a simpler code for extracting a word from a sentence.pls.

• Yes Gopika, there are alternative codes for extracting words from a sentence. It is up to you to judge which is simpler and which is not. 🙂
Here is a code which extracts words from a sentence and saves it in a String variable w.

```System.out.print("Enter any sentence : ");
String s = br.readLine();
int l = s.length();
int pos=0;
String w;
for(int i=0;i<l;i++)
{
if(s.charAt(i)==' ')
{
w = n.substring(pos,i);

/* Write the operation you want to perform with 'w' here.
* You can also send 'w' to any function by calling it here
*/

pos=i+1;
}
}
```
• String a1=”Hello world this is jackass”
String[] a2=a1.split(” “); // if you know words are separated by space , it will put all it in array 😉

• Thank you Anna.
Yes, this is another way which we have used in some programs. It is the easiest way when you have to save the words of a sentence in an array.
Just note: If there are 2 spaces in between any words or a fullstop followed by a space as in “Java is Fun. Said none”, the above code needs to be modified otherwise it will store null strings also in the array.
Just use this syntax: String word[]=s.split(“[. ]+”); where ‘s’ is the sentence. Similarly you can use other punctuation marks by putting them inside like this “[?!,;. ]+”

20. Thank You 😉 Thank You very much, it helped a lot 😉

21. is it not okay if in fair itself we first write the program and then the algorithm? does the order matter?

• The correct order is to write algorithm first and then the program. Even in the question paper you are told to follow this order. Read the instructions given in ISC Practical Papers. Here is one: http://www.guideforschool.com/wp-content/uploads/2013/04/ISC-2013-Computer-Science-Paper-2-Practical.pdf

• the correct sequence is wat u r saying but leaving 1-2 pages we can start writing the program nd later write the algo based on our program…. ???  can we do this???

• Yes you can do this.

22. I always faced a problem in writing algorithms. But this post is really helpful. Thankyou.