How to write an algorithm – Step 3

By now you should know how to find out the data, process, conditions and loops from a given problem statement. As a beginner this much should be enough for you to be able to write a complete algorithm. If you can find out all this information from the given problem statement you should be able to write algorithms for majority of the problem statements.

In this tutorial you will learn how to write a full algorithm which involves all these four components. We will be using the problem statement which we discussed in the previous tutorial. Below is the same problem statement copied from the previous tutorial

Rosy was given a file with marks of 10 students in a class. Each student appeared for 5 exams and scored marks. Rosy has to write an algorithm to read the marks of all these students from the data file, calculate the average marks, award the grade to each student and finally enter the average marks and final grade in the data file. If the student’s average is below 75 then he / she will be given a D grade, if the average is between 75 and 80 then he / she will be given a C grade, if the average is between 80 (including 80) and 85 then he / she will be given a B grade and if the average is above 85 (excluding 85) then he / she will be given an A grade.

Now that you already know the data, processes, conditions and loops in this statement let’s directly get into writing the algorithm

Start

Step1 : Open the data file with student marks records and set file pointer to first student record
Step2 : Create a variable StudentCounter and set it to 1
Step3 : Create five variables M1, M2, M3, M4, M5 to store student marks for each subject
Step4 : Create a variable Grade to store student grade
Step5 : Read student marks and store them in M1, M2, M3, M4, M5
Step6 : Create a variable Avg to store average marks
Step7 : Set Avg = (M1 + M2 + M3 + M4 + M5) / 5
Step8 : Check if Avg < 75
            If true then set Grade = D
            If not true then check if 75 <= Avg < 80
            If true then set Grade = C
            If not true then check if 80 <= Avg <= 85
            If true then set Grade = B
            If not true check if Avg > 85
            If true then set Grade = A
Step9 : Store the value of Avg and Grade in the data file
Step10 : Set StudentCounter = StudentCounter + 1
Step11 : Check if StudentCounter > 10
    If true then Goto End
Step12 : Increment the file pointer to point to next student record
Step13 : Goto Step5

End

In this algorithm notice how we go from Step13 back to Step5 and we keep doing this till the StudentCounter value does not become greater than 10. This repetition of a process based on some condition is called a loop.

Quick Links

E-Magazines

@

Total Followers
Study Group Created
Study Group Joined
Following Teacher
Following Organization
Blog Articles Added
Questions Asked
Questions Answered
Jobs Posted
Total Members in Group
Questions asked by members
Tasks added in this Group

Please wait..

Ok

Login to Open ESchool OR Create your account    Login   SignUp