Tuesday, September 3, 2013

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE part-3

2.3 Iteration Control Structures

Assignment 2.3.1 Pseudo code - Iterational pattern (1)


Objective: Write pseudo code for iterational pattern problems.

Problem 1 :

The problem given in Assignment 2.1.2 is extended to check whether 
the Number of Hours accepted from the user is less than or equal to 0. If so display an error 

message. Reenter the data the Number of Hours until valid data is entered.




Step 1: Write the following Pseudo code

CALC_GROSS_PAY
1. input No_of_Hours, Pay_Rate
2. while(------------) do
3. display "---------------------"
4. -------------------------------------
5. end while
6. Gross_Pay =  No_of_Hours * Pay_Rate
7. display " The gross pay is equal to : ", Gross_Pay
8. end

Step 2 : Fill in the missing (------) parts in the Pseudo code
We need to put a condition in the parenthesis of while ( ) loop, which could test the validity of the data input by the user for the number of hours. We need to keep the loop running displaying a message as long as the condition in the parenthesis remains true. Therefore the complete form the above pseudocode is given as :



CALC_GROSS_PAY
1. input No_of_Hours, Pay_Rate
2. while(No_of_Hours < = 0) do
3. display "Invalid Entry, Enter Valid Number of hours. "
4. input No_of_Hours
5. end while
6. Gross_Pay =  No_of_Hours * Pay_Rate
7. display " The gross pay is equal to : ", Gross_Pay
8. end


Problem 2 :
Write a pseudo code to calculate the Loan Balance, where a person borrows an amount A and in return he/she agrees to make N payments per year, each of amount P. While the person is repaying the loan, interest will accumulate at an annual percentage rate of R, and this interest will be compounded N times a year (along with each payment). Therefore, the person must continue paying these installments of amount P until the original amount and any accumulated interest is repaid.

NOTE: The formula to calculate the amount that the person needs to repay after T years is :

Balance amount after T years = 

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE

2.2 Selection Control Structures


Assignment 2.2.1: Pseudo code - Selectional pattern (1)


Objective: Write pseudo code for selectional pattern problems

Problem Description: The problem given in Assignment 2.1.2 should be extended to check whether the Number of Hours accepted from the user is less than or equal to 0. If so an appropriate error message should be displayed.

The problem given in assignment 2.1.2 is here.
The pseudo-code with required changes can be written again as :


Calculate-Gross-Pay

1. input No_of_Hours, Pay_Rate

2. if  No_of_Hours < = 0 then
3. Display " The Number of hours are invalid."
4. else
5. Gross_Pay = No_of_Hours * Pay_Rate
6. Display "Gross Pay is : ", Gross_Pay
7. end



Assignment 2.2.2: Selectional pattern (2)

Objective: To develop pseudo code writing skills for a given problem using certain logic.

Case study: An institution offers several programs. Each program has several courses. There 
are students who have opted for these courses. There are Educators who offer these courses.



Problem 1:
At a given point of time we need to allocate a course to an educator for a given
period. Before allocating a course to an educator the following conditions are to be checked.
1. Enough number of students have registered for the course
AND
2. The course is added into the educator’s skill set
AND
3. Educator’s calendar is free for the given period
AND
4. Educator is not on leave for the given period
And
5. Educator has not yet completed his delivery targets Or There is no other alternative

SOLUTION :

COURSE_ALLOCATION
1. input course
2. input num_of_students
3. input educator's_skill_set, spare_time, is_on_leave, delivered_targets, allocation
4. if ( (num_of_students > min_number_students) && (educator's_skill_set = course) && (spare_time = true ) && (is_on_leave = false) && (delivered_targets = true ) ) then
5. set allocation = true
6. end if
7. display "Course is allocated. "
8. end


Problem 2: For a student, following is the course qualifying criteria.
1. Student has registered for the course
2. Attended at-least 80% of the classes
or attended at-least 60% of the classes and submitted medically unfit certificate
3. Submitted all the assignments
4. Completed project
5. Scored 80% marks in the test or the retest

QUALIFYING_STUDENT
1. input classes_attended, min_score
2. input course_reg, medical_certificate, submitted_assignments, completed_project
3. if ( ( course_reg = true ) &&(classes_attended >= 80 || (classes_attended >= 60 && medical_certificate = true ) ) && (submitted_assignments = true ) && (completed_project = true) && ( min_score >= 80)) then
4. set course_qualification = true
5. end if
6. display "Congratulations, course has been qualified. "
7. end 



Assignment 2.2.3: Selectional pattern (3)


Objective: To use the problem solving techniques learnt so far in a real life application.

Problem 1: Take marks of a trainee in a module as input and calculate the grade and
grade points. The grade table is given below:




SOLUTION :

CALC_GRADE
1. input marks_obtained
2. if ( marks_obtained >= 80 ) then
3. grade_point = 5
4. grade = A
5. elseif ( marks_obtained >= 73 && marks_obtained <=79 ) then
6. grade_point = 4.5
7.  grade = B+
8. elseif ( marks_obtained >= 65 && marks_obtained <=72 ) then
9. grade_point = 4
10. grade = B
11. elseif ( marks_obtained >= 55 && marks_obtained <=64 ) then
12. grade_point = 3
13. grade = C
14. elseif ( marks_obtained >= 0 && marks_obtained <=54 ) then
15. grade_point = 2
16. grade = D
17. end if
18. display " The grade points obtained are equal to: ", grade_point
19. display " The grade awarded is : ", grade
20. end


Problem 2 :
Take marks of a trainee in all generic modules (listed below)as input and calculate the
Generic GPA. The method for calculating Generic GPA is as follows:


Grade points for marks are given in previous problem and credit points are as follows:





SOLUTION :

CALC_GPA
1. input marks_obtained
2. if ( marks_obtained >= 80 ) then
3. grade_point = 5
4. grade = A
5. elseif ( marks_obtained >= 73 && marks_obtained <=79 ) then
6. grade_point = 4.5
7.  grade = B+
8. elseif ( marks_obtained >= 65 && marks_obtained <=72 ) then
9. grade_point = 4
10. grade = B
11. elseif ( marks_obtained >= 55 && marks_obtained <=64 ) then
12. grade_point = 3
13. grade = C
14. elseif ( marks_obtained >= 0 && marks_obtained <=54 ) then
15. grade_point = 2
16. grade = D
17. end if
18 GPA = ( marks_obtained * grade_point ) / marks_obtained
19. display " The grade points are : ", grade_point
20. display " The GPA obtained : ", GPA
21. end

PSEUDO-CODES IN PROGRAMMING

WHAT ARE PSEUDO-CODES ?

The pseudo- code is step by step representation written in plain english of an algorithm, which can be perceived as the initial stage of formal coding. Instead of directly digging upon the proper code, it is preferred to write pseudocode to get an essence of the logic applied in the algorithm.

Some of the conventions, keywords etc. followed while writing a pseudo-code are as follows :
1. 'input' keyword is used to take the input from the user.
2. 'display' keyword is used to show the result.
3. Mathematical expressions are used as we do normally.
4. For decision making constructs, 'if' , 'then', 'else' , 'else if' keywords are used.
5. When some statements are to be repeated for a known number of times, 'for', 'to', 'do', 'end for' keywords are used.
6. When some statements are to be repeated as long as some condition remains true then , 'while', 'end while' keywords are used.


TYPES OF PSEUDO-CODES

1. SEQUENTIAL PATTERN ( the statements are executed in the same order as it is written)

2. SELECTIONAL PATTERN ( they involve decision making situations )

3. ITERATIONAL PATTERN ( they involve repitition of the statements )


Monday, September 2, 2013

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE

2.1 Flowchart and Pseudo Code


Assignment 2.1.2: Pseudo code- Sequential pattern


Objective: Writing pseudo code for sequence pattern problems

Problem Description: The finance department of a company wants to calculate the gross pay of an employee in the company. The number of hours worked by the employee and the pay rate should be accepted from the user and the gross pay should be calculated as the below formula.

Calculate-Gross-Pay
1. input No_of_Hours, Pay_Rate
2. Gross_Pay = No_of_Hours * Pay_Rate
3. Display "Gross Pay is : ", Gross_Pay

The above given set of statements represents a pseudo code, which can be perceived as the initial stage of formal coding. It represents a step by step approach to solve any problem in plain english, instead of formal syntax used in any particular programming language. 

There are few conventions which are followed while writing a pseudo code as :
1. use of keyword 'input', whenever data is retrieved from the user
2. use of keyword 'display', whenever result is to be displayed
3. Mathematical expressions are used as it is, as we normally use them.
4. if, then, else, end if keywords are used for decision making situations
5. for, to , do , end for keywords are used when  certain steps are to repeated for a known number of times.
6.while, end while keywords are used when some of the steps are to repeated as long as certain condition remains true.


Assignment 2.1.3: Understanding Variables & Constants


Problem 1 :

Write a pseudo code to take the height of the building in variable 
“Building_Height” and display the same.





SOLUTION :

DISP_HEIGHT
1. input Building_Height
2.display "The height of the building is : ", Buildiing_height
3. end


Problem 2 :
Write a pseudo code to take the constant value 3.142 in a variable “pi” and
display the same.

SOLUTION :

DISP_PI
1. input pi = 3.142
2. display "The value assigned to pi is : " pi
3. end


Assignment 2.1.4: Understanding Operators


Problem 1 : 
Write a pseudo code to take two numbers as input in variables a and b and display
i. Product of the two numbers a and b
ii. Sum of the two numbers a and b
iii. Values of a-b and b-a
iv. Quotient and remainder for a/b and b/a

SOLUTION :

i ) 
PROD_OF_TWO_NUM
1. input a, b
2. Product = a * b
3. Display " The product of 'a' and 'b' is: ", Product
4. end

ii)
SUM_OF_TWO_NUM
1. input a, b
2. Sum = a  + b
3. Display " The sum of 'a' and 'b' is : ", Sum
4. end

iii)
DIFFERENCE_BETWEEN
1. input a, b
2. Diff_A_and_B = a - b
3. Diff_B_and_A = b-a
4. Display "the difference of 'a' and 'b' is : ", Diff_A_and_B
5. Display "the difference of 'a' and 'b' is : ", Diff_B_and_A
6. end

iv) 
QUOTIENT_REM
1. input a, b
2. Quo_A_by_B = a / b
3. Rem_A_by_B = a % b
4. Quo_B_by_A = b / a
5. Rem_B_by_A = b % a
6. Display "the quotient of 'a' by 'b' is : ", Quo_A_by_B
7. Display "the quotient of 'b' by 'a' is : ", Quo_B_by_A
8. Display "the remainder from 'a' by 'b' is : ", Rem_A_by_B
9. Display "the remainder from 'b' by 'a' is : ", Rem_B_by_A
10. end


Problem 2:

Write a pseudo code to take the height and width of a rectangle in variables

height and width respectively and display the rectangle's area and perimeter.

SOLUTION :

RECTANGLE_AREA_PERIMETER
1. input height, width
2. Area = height * width
3. Perimeter = 2*(height +width)
4. Display "The area of rectangle is : ", Area
5. Display "The perimeter of rectangle is : ", Perimeter
6. end


Problem 3:
Write a pseudo code to take the radius of a circle in variable radius and display

its area and perimeter.

SOLUTION :

CIRCLE_AREA_PERIMETER
1. input radius
2. Area = 22/7(radius * radius)
3. Perimeter = 2 *( 22/7) * radius
4. Display "The area of circle is : ", Area
5. Display "The perimeter of circle is : ", Perimeter
6. end


Problem 4:
Write a pseudo code to take the starting value, common difference and the
number of terms of an arithmetic progression in variables a, d and n respectively and display its nth term and sum of n terms.

SOLUTION :

ARITHMETIC_PROGRESSION
1. input a, d, n
2. Nth_term = a+((n-1)*d)
3. Sum_of_terms = (n/2)*((2*a)+((n-1)*d))
4. Display " The nth term is : ", Nth_term
5. Display "The sum of terms is : ", Sum_of_terms
6. end


Problem 5:
Write a pseudo code to take the radius of the sphere in variable radius and

display the volume of the sphere.

SOLUTION :

VOLUME_OF_SPHERE
1. input radius
2. Volume = (4/3)*3.14*(radius*radius*radius)
3. Display " The volume of the sphere is  : ", Volume
4. end




FLOWCHARTS IN PROGRAMMING

WHAT ARE FLOWCHARTS ?

Flowcharts are graphical means of representing the logic to solve any computational problem.
It is a pictorial way and hence give more intense understanding of the solution for the problem at hand.It consist of some symbols which forms its tools to make a complete structure for any problem.

These symbols are : 
 
Flowchart Symbols


An example to show a perfect flowchart :


Flowchart to check whether a number is prime or not


Wednesday, August 28, 2013

TOPIC 2: LOGIC BUILDING USING PSEUDO CODE

2.1 Flowchart and Pseudo Code

Assignment 2.1.1: Problem Solving through flowchart

Problem Description: Draw a flowchart for the following problems:

1. Add first N even numbers

SOLUTION : 


flowchart to show addition of first 'n' even numbers


2.   Add first n odd numbers

SOLUTION :


Flowchart showing addition of first 'n' odd numbers


3.  Find maximum of three numbers

SOLUTION :


Flowchart showing maximum of three numbers


4. Determine whether a year s leap year or not

SOLUTION :


Flowchart showing whether a year is leap year or not

5. Input a number and check whether it is prime or not

SOLUTION :


Flowchart showing whether a number is prime or not


6. Input a number and check whether it can be expressed as some positive integer power of 2. For example 2,4,8,16,32… etc. can expressed as some positive integer power of 2 like 2^1,2^2,2^3,2^4, and 2^5……..

SOLUTION : 


Flowchart expressing a number as power of 2

Monday, August 26, 2013

INFOSYS FOUNDATION TRAINING PROGRAM (FTP) SOLUTION FOR LAB-GUIDE ASSIGNMENT PROBLEM SOLVING AND LOGIC BUILDING- part-2

TOPIC 1 : COMPUTATIONAL PROBLEM SOLVING

1.1 COMPUTATIONAL PROBLEM SOLVING AND ALGORITHM

ASSIGNMENT 1.1.1 : PROBLEM SOLVING EXERCISES ( part-b )


1. In an objective type programming contest, following are the rules

a. It will have multiple rounds.

b. In each round, every participant will get a set of 10 question out of which
one question is marked as star question (The toughest one).
c. The participant will get the next set, only if 50% of the questions are
marked correctly in the previous round or he attempted the star question
correctly. Otherwise he will be out of the contest.
It is found that in each round 50% of the participants are not able to attempt 50%
of the questions correctly but one out of those 50% participants, attempts the
star question correctly.
If only 4 participants are left for the 5th round what was the number of
participants in the first round?

SOLUTION :

Let the students appeared for 1st round be = x

Students appearing for the 2nd round :
Since half of them made for the 2nd round, which constitute = x / 2
and 1 candidate who attempted star question
So, total students who made for the 2nd round are = x/ 2 + 1 = (x + 2)/ 2

Students appearing for the 3rd round :
Since half of the previous round students made for the 3rd round, which constitute = ( (x + 2)/ 2 )/2
and 1 candidate who attempted star question
So, total students who made for the 2nd round are =  ( (x + 2)/ 4 ) + 1 = (x + 6)/ 4

Students appearing for 4th round :
Since half of the previous round students made for the 3rd round, which constitute = ( (x + 6)/ 4 )/2
and 1 candidate who attempted star question
So, total students who made for the 2nd round are =  ( (x + 6)/ 8 ) + 1 = (x + 14)/ 8

Students appearing for 5th round :
Since half of the previous round students made for the 3rd round, which constitute = ( (x + 14)/ 8 )/2
and 1 candidate who attempted star question
So, total students who made for the 2nd round are =  ( (x + 14)/ 16 ) + 1 = (x + 30)/ 16


Since it is given that the number of students appeared for the 5th round are =  4

This implies the expression we got for the 5th round i.e. (x + 30)/ 16 becomes = 4
i.e.                                                   (x + 30)/ 16 = 4
                                                         (x + 30 ) = 64
                                                            x = 64 - 30
                                                               x = 34

Since we assumed ' x ' to be the number of students present during the 1st round, which came out to be = 34. It means there were total of 34 students present in the 1st round.


2. A farmer had a lazy son. The farmer wanted his son to work in the farm. So he offered that the son will work for 50 days, for each day the son works, he will get 10 rupees, for each day he will not work, he need to pay back 15 rupees to his father. At the end of 50 days, when the son counted his income, it turned out that he had not got anything. How many days did the son actually worked?

SOLUTION :

Let the son worked for =  x days
money earned for x days = 10x

No. of days on which he didn't work = (50 - x)
Money he lost for days he didn't work = 15 ( 50 - x )

It is given in that the money he earned balances the money he paid back, which means :

                                                      10x = 15 ( 50 - x )
                                                       10x = 750 - 15x
                                                            25x = 750
                                                           x = 750/25
                                                              x = 30
Since we assumed ' x ' to be the number of days for which son worked, which came out to be = 30 days. Therefore, Son worked for 30 days.