Sunday, May 10, 2020

Basic Problems (Level 1)

------------------------------------------------------------------------------------------------------------------

Objective:

In this article, the objective is to realize the following points with the help of very simple problems
  1. Problem Statement
  2. Analysis of Requirements
  3. Problem Specifications & Assumptions
  4. Design of Algorithm

Problem 01

Statement: Write an algorithm to find the sum of two numbers.

Analysis:
  1. We need two numbers as input. number means numeric value, i.e. having many alternative.
  2. The result that will be obtain after performing requisite operation(s) will be integer as integer+integer= integer.
 This analysis part is often done by our mind silently. We often fear about the term analysis but it is not so difficult. Analysis mainly requires to relate proper information in proper time & space. Regularly we use this analysis of the facts, may in the different areas rather than education.

You have to believe that, you have some knowledge to solve any problem but it may not be sufficient to solve the problem. So, you may need to gather some more knowledge to solve the problem. Remember, before deciding that, you cannot solve a problem, you must think to solve that problem for sufficient time, after a certain time you can surrender and go for outer support.

Specifications: So, what can be the specifications as per the analysis done?

Name of the Algorithm: Add_Two_Values
Inputs: X, Y are two integer quantity.
Output: Z is another integer as output to hold the result of (X+Y).

Now think, all the names used i.e. Add_Two_Values, X, Y & Z are subject to change by person to person. This is the sensed as Assumption. So, assumption is very important part in specification after the analysis.
 
The algorithm may be documented as follows:

Algorithm_01: Add_Two_Values (X, Y, Z)
Here,
X, Y are two integer value to be added must be given as input and,
Z is the result of (X+Y), must be produced using the following steps.
Steps:
  1. Z:= (X+Y)
  2. Return/ Stop/ End
Possibly you are thinking, "the author is a non-sense, giving knowledge of performing addition to us, as we are kindergarten fellows".

No, I am not giving you the sense of addition. This simple example is taken to clarify the notations of an algorithm, very basic part. If you know this all, you can skip some of the pages, if not found interesting due to your existing knowledge.
I prefer Return, rather than Stop or End, keeping in the mind of the fact that, ultimately we need to write programs. Return will help you there.

Problem 02

Statement: Write an algorithm to calculate (X+Y)/Z.

Algorithm_02.1: Calculate(X, Y, Z, R)
Inputs: X, Y, Z are three integer quantity.
Output: R will store the result of (X+Y)/Z.
Steps:
  1. t:= (X+Y)
  2. R:= (t/Z)
  3. Return
You are getting frustrated, as the author is giving you so critical mechanism of a simple problem. Why we should not perform (X+Y)/Z in step 1?....

In my opinion, Yes, the algorithm is a non-sense algorithm....

But, not because of step 1, rather due to step 2....

It is the division operation. Do you know what is the sense of divide by zero? It is not defined, i.e. undefined. Is it possible to perform division by zero operation by any machine in this world? No, there are no such machine. Then, what is the solution? The only solution is our thinking. See the steps below for the same problem,

Algorithm_02.2: Calculate(X, Y, Z, R)
Inputs: X, Y, Z are three integer quantity.
Output: R will store the result of (X+Y)/Z.
Steps:
1. t:= (X+Y)
2. IF ( Z Not_Equals_To 0) Then
        R:= (t/Z)
   End of IF
3. Return
Did you feel that, 
a very small change in an algorithm,
can solve a very big problem....
a very small change in an algorithm,
can create a very big problem....
If you have realize the fact explained above, never forget the example & the fact.

Now, tell me,
1) Do you perform (X+Y)/Z in step 1?
2) What will be the value of R, when Z=0?
Give your answer in the Comment box below.

So, again this algorithm is also non-sense. Why? as R is undefined (garbage value) by the algorithm.


What is the solution?

Algorithm_02.3: Calculate_Final(X, Y, Z, R, t)
Inputs: X, Y, Z are three integer quantity.
Output: R will store the result of (X+Y)/Z when t is 1. t will be 0 when z is 0, in this case Z is invalid.
Steps:
1. t:= (X+Y)                                     [ Calculating addition ]
2. IF ( Z Not_Equals_To 0) Then      [ Validity checking of Z ]
        R:= (t/Z)
        t:= 1                                        [ Indication of successful operation ]
    ELSE
        t:= 0                                        [ Indication of division by zero ]
   End of IF
3. Return

Again tell me, Which algorithm you will select for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02?
Give your answer in the Comment box below.

Observations:

  1. Analysis is not done only at the beginning of the development process of a program(or software), it will be effective in every steps of your life.
  2. Only observation of the mistakes/ wrong steps can give you a good algorithm.
  3. Faults in any algorithm is quite natural when it is being developing. Remember, after delivery of the software product, fault is very dangerous. So, what to do? Finding faults in an algorithm or in a program is known as the process Debugging. Proper debugging can gift you a good algorithm.
  4. Judgement of every steps is very very important, otherwise you will not be able to find any fault.
  5. Indentation makes the steps visible. Look the writing structure of step 2 in Algorithm_02.3, it is called indentation. It is used to clarify the relationship among the operations. In program writing it is also important due to same reason.
  6. Assumptions can be useful to validate the data value - this concept was introduced in the previous article. Here, in the Algorithm_02.3 t is used to validate the value of R.
  7. In Algorithm_02.3 bracket has been used to clarify respective operation to the reader.
  8. In Algorithm_02.3 t has been reused. 

Try Your Own

  1. Write an algorithm to find the highest value among 3 given values. 
  2. Write an algorithm to calculate the average of 3 given values.
  3. Write an algorithm to calculate (a+b)/(c+d).
  4. Write an algorithm to test a given value X is divisible by another given value Y or not. 
  5.  Write an algorithm to calculate a+b*c.
One last question. What is the characteristic of R & t in the Algorithm_02.3? please give the answer as comment.
Note, never relies on other answers. Think your own and put all answers in the comment. What everyone thinks, is not always true....

34 comments:

  1. Problem 02
    1) NO we can't perform (X+Y)/Z in step 1.
    2) When z = 0 then R has no value.
    --------------------------------
    Among the Algorithm_02.1 , Algorithm_02.2 and Algorithm_02.3 i am select the "Algorithm_02.3 " .
    ----------------------------------
    In the Algorithm_02.3 R and T are outputs.
    -----------------------------------
    Answered by : Akash Pakhira,(CMSA, Semester-IV)

    ReplyDelete
  2. 1. I do not (X+Y)/Z in one step.
    2. R will be undefined and it will store garbage value when Z=0.

    I will select Algorithm_02.3 for the problem 02 among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 .

    In the Algorithm_02.3 t is used to validate the value of R.

    -Ahin Subhra Halder ,CMSA,SEM 2

    ReplyDelete
  3. Q1) Do you perform (X+Y)/Z in step 1?
    Answer: We are not performing ( ( X + Y ) / Z ) in step 1.


    Q2) What will be the value of R, when Z=0?
    Answer: When Z=0, the value of R will be undefined according to the algorithm [ Algorithm_02.2: Calculate(X, Y, Z, R) ] .


    Q)Again tell me, Which algorithm you will select for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02?
    Answer: I will select Algorithm_02.3 against the problem 02.

    -Sayan Deb Roy , CMSA Semester-4

    ReplyDelete
  4. 1. No, I don't prefer (X+Y)/Z in step 1.
    2. The value of R can be any garbage value generated by the machine.

    I'll select Algorithm_02.3

    R and t are used to store result if t=1 then only R will return a value, else t=0 then division will not be performed because it will state that Z=0.
    Here R and t will be pointers because their value will change for other inputs.

    -Vibha Yadav (CMSA, Semester-IV)

    ReplyDelete
  5. Sir,
    1.I will not perform (X+Y)/Z in step 1, as if the value of Z is zero then the answer will be undefined.So the checking of Z must be done before dividing.
    2.The value of R will be undefined or result in infinity if Z=0(where t!=0)
    3.I will use Algorithm_02.3.
    4.In Algorithm_02.3 by checking the value of t we can confirm whether the operation is successful or not, i.e if(t==1) then Z is not 0 and thus R has proper result, else the value of R will be undefined as Z is 0.
    Sayan Poddar,CMSA,Semestr-4.

    ReplyDelete
  6. Answers to the questions -
    Answer to question 1 :-
    Yes, I have performed the operation ((X+Y)/Z) but after reading the article properly I understand why we should use the division operation in a different step.

    Answer to question 2 :-
    R will have a garbage value, because if Z=0 then it will not enter into the division operation because of the presence of a conditional statement which will prevent the operation to execute if Z=0.

    Answer to question 3 :-
    I have choose Algorithm_02.3 against the problem 02 because it is slightly big than the rest but it is more convenient than the rest.

    Answer to question 4 :-
    R will store the value after division of the sum of two integers.
    T will give the indication of successful operation of the algorithm, it will store 1 if the algorithm has successfully completed its operations and it will store 0 if algorithm is unsuccessful.

    I read it and I understand it. - Aditya Kumar Singh, CMSA, SEM- 2

    ReplyDelete
  7. Ans 1-
    I will not perform (X+Y)/Z in step 1 because we need to check if Z equals to zero or not.

    Ans 2-
    The R will store the garbage value if Z equals to zero because the operation in the if statement will not be executed.

    Ans 3-
    I will select Algorithm_02.3 to solve the problem 02 because it is perfect.

    Ans 4-
    R will store the result of the Algorithm_02.3 and t will firstly store (X+Y) and after this t will perform as an indicator to indicate if the operation is successfull or not.

    -SUNY DAS(CMSA,Sem-II)

    ReplyDelete
  8. Question 1: Do you perform (X+Y)/Z in step 1?
    Answer: Earlier I have performed this (x+y)/z in one step. But after reading this, i won't perform it altogether in step 1.
    Question 2:What will be the value of R, when Z=0?
    Answer: The value of R in algorithm 2.1 as well as in algorithm 2.2 will be garbage value when z =0. As the division by 0 will be undefined.

    Question 3:Which algorithm you will select for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02?
    Answer: I will select algorithm 2.3 to solve the problem 2.

    Question 4:What is the characteristic of R & t in the Algorithm_02.3?
    Answer:R will store the output result after the division. T is used as flag indicator to show whether the operation is successful.

    Aqsaa Nasir (CMSA,semester-IV)

    ReplyDelete
  9. Please do not send e-mail for any query/clarification related to any articles, use contact form for this purpose....

    ReplyDelete
  10. 1) No, I can't perform (X+Y)/Z in step 1.

    _______________________________________

    2) when Z=0 then R is undefined.

    ______________________________________
    3)i select Algorithm_02.3 against the problem 02.
    ________________________________________

    4)In the Algorithm_02.3 R&T both are output
    t store value of (X+Y)
    R store the value t/Z
    _____________________________________

    --protik Patra (CMSA,SEM-IV)

    ReplyDelete

  11. 1)I will not perform (X+Y)/Z in step 1 because if Z is equal zero then then R will be undefined.
    2)The R will hold garbage value if Z is equal to zero as the result will be undefined.
    3)I will prefer Algorithm_02.3 over others.
    4)Here in Algorithm_02.3 R holds the result and its value completely depend upon the input given.It can be either zero or the required value needed and 't' tells that whether the value of Z was zero or not and hence the operation of division is performed or not.
    - Sony Pathak,CMSA, Semester 4

    ReplyDelete
  12. Q1. I will never perform (X+Y)/Z in step 1 since, I don't want to obtain an undefined result.

    Q2. When Z= 0, R will certainly be a garbage value which is undefined.

    Q3. I will definitely select Algorithm_02.3 against Problem 02 because it will give me the result of my error when value of Z will be 0.

    Q4. Here, t is used to validate the value of R after division process.i.e., t is the main weight of the algorithm which will change with given further I/Ps & thus gives the property of a successful/unsuccessful algorithm.

    R stores the result after division process.

    -Sreemita Dey, CMSA, Semester 4

    ReplyDelete
  13. 1.No, I will not use (X+Y)/Z in Step 1.
    2.When Z=0,R holds an undefined or garbage value.
    3.Among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3, I would choose 'Algorithm_02.3'.
    4.In 'Algorithm_02.3','R' holds the value of the expression '(X+Y)/Z' and 't' hold confirmation value whether the above expression is being computed or not.

    Abhisekh Kumar Giri - Sem II CMSA

    ReplyDelete
  14. Ans1
    I will not perform (X+Y)/Z in step 1.

    Ans2
    When Z=0,the value of R will be undefined
    Ans3
    I will use Algorithm_02.3.
    Ans4
    Here, R and t are used to store result

    Saikat Banerjee(CMSA,sem-iv)

    ReplyDelete
  15. 1). I would have not performed (X+Y)/Z in Step 1. because I don't know if the value of Z is 0 or not, so first I need to check if Z is 0 or not.

    2).When Z is 0 , the value of R will be undefined (garbage value) by the Algorithm_02.2 .

    3).I will choose Algorithm_02.3 among the others , because the chance of any error occurring from this algorithm is the least.

    4).R and t both are output variables, where R stores the result while t validates if R has the result.

    -Sayan Neogy , CMSA, Semester-IV

    ReplyDelete
  16. 1)I will not perform (x+y)/z in step 1.
    2)The value of R will be undefined when z=0.
    I will prefer algorithm_02.3 to solve the problem 2.
    R and t are the two outputs .If t=1 R will return a value which means the division is performed else t=0 the division is not performed.
    -Shawan Paul,CMSA,SEMESTER-2.

    ReplyDelete
  17. 1. Usually, I will not perform ((X + Y) / Z) in step 1. If I use it and the value of Z = 0, the compiler may show an error(Like core dump...).

    2. Among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 , I will select the Algorithm_02.3.

    3. Characteristic of R and t :
    R(an integer as X, Y and Z are also integer) will hold the value of ((X + Y) / Z) but, we have to remember that Z can be 0. So, t will help us to decide that R is valid or invalid. t = 1 means R holds a valid number or t = 0 means R holds an invalid number.

    - Anojit Ghosh, CMSA, Semester - 2.

    ReplyDelete
  18. 1) I will not perform (x+y)/z in step 1.

    2) R will be undefined when z=0 and will be giving any garbage value.

    3) I will choose algorithm 02.3 from the problem 02 as it is more appropriate.

    4) R will be undefined as z=0 and t is used to store the value and again it has been used to indicate whether the output is positive or negative in regards of the algorithm.

    -Md Mahboob Ali,CMSA,Sem-IV.

    ReplyDelete
  19. 1> YES.

    2> The value of R is (-1).(set a default value if z = 0).

    3> I will select Algorithm_02.3 against the problem 02.

    4 > R will store the result of (X +Y) /Z and t will store the indication that the operation is performed successfully or not.

    - Payel Jana, B.Sc (H), Semister - IV, Syamaprasad College, India

    ReplyDelete
  20. 1)No,we do not perform (X+Y)/Z in one step because if Z is 0 the result will be undefined, hence checking of the divisor is compulsory.

    2)R will be undefined, when Z is 0.

    3)Algorithm_02.3 is most preferable for problem 2.

    4) R stores the result of the operation and t indicates weather the operation was successful i.e. (t=1), and (t=0) if the divisor is 0.

    Pabitra Kumar Dhal, CMSA, SEEM-IV

    ReplyDelete
  21. 1)
    Ans:- I don't perform (X+Y)/Z in step 1

    2)
    Ans:- when Z=0 then R is undefined

    3)
    Ans:- I select Algorithm_02.3 against the problem 02

    4)
    Ans:- In the Algorithm_02.3 R and t are output

    Protik patra (CMSA,sem-IV)

    ReplyDelete
  22. Q1) Do you perform (X+Y)/Z in step 1?
    Ans: If there is a checking for Z is 0 or not in step 1,then I am performing ( ( X + Y )/Z )in step 1 & if the checking is not there,then I am not performing this in step 1.

    Q2) What will be the value of R, when Z=0?
    Ans: According to the Algorithm_02.2, when Z=0, R provides us a garbage value.

    Q3)Again tell me, Which algorithm you will select for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02?
    Ans: I will select Algorithm_02.3 against the problem 02 because it is easy to understand.
    Sabyasachi Goswami,CMSA,Semester-2.

    ReplyDelete
  23. 1). I would have not performed (X+Y)/Z in Step 1. because I don't know if the value of Z is 0 or not, so first I need to check if Z is 0 or not.

    2).When Z is 0 , the value of R will be undefined (garbage value) by the Algorithm_02.2 .

    3).I will choose Algorithm_02.3 among the others , because the chance of any error occurring from this algorithm is the least.

    4).R and t both are output variables, where R stores the result while t validates if R has the result.

    -Sayan Neogy (CMSA, Semester-IV)

    ReplyDelete
  24. 1. In the problem "((x+y)/z)" I can't solve it in 1 step . Beacuse I have to care about the user input. So I have to check first that "z is 0 or not". As I know that any number which is divided by "0" is invalid.

    2. If "z=0" it's means the output which is R in this problem hold some gurbage value that is invalid.

    3. When I read the article step by step then I thought algorithm_02.2 is better then algorithm_02.1.
    But when I scroll down the page and found the algorithm_02.3 then I realised that this is the best analysis against the problem 02. Though it was little bit big but effective as well.

    4. I found "R" is the result variable and "t" is the local variable which is used to testing purpose in the algorithm_02.3.
    That's means f=1 if the operation is successfull
    And f=0 if the operation is invalid.

    - Bijoy Sarkar, CMSA, semister-II.

    ReplyDelete
  25. 1) No, i can't perform (X+Y)/Z in step 1.
    2) The value of R is infinite when Z=0.

    I will select Algorithm_02.3 against the problem 02.

    Both t and R are dependent(t depends on X and Y and R depends on t and Z) .

    -Priya Singh, CMSA, Sem-4

    ReplyDelete
  26. Answers:-
    1) No, I'll not perform (X+Y)/Z in step 1.
    2) When Z=0, the value of R will be undefined.
    3) I'll select the Algorithm_02.3 for the program against the problem 02.
    4) R will hold the result of the algorithm.
    At first t will hold the result of (X+Y). Then t will specify whether the operation is successful or not.

    -Preeti Mitra, CMSA, Semester-II

    ReplyDelete
  27. ANS(1) :Certainly not I will not perform (X+Y)/Z in step 1
    ANS(2) : Value of R will be UNDEFINED when Z = 0 at the Equation (X+Y)/Z.

    ANS(3) : Among Algorithm_02.1 , Algorithm_02.2 and Algorithm_02.3 i would like to select - Algorithm_02.3

    ANS(4) : t is identity of Operation and Result(R)..R is the output of the operation


    ~~RAHUL DEB BHANDARI // SEM - IV, SYAMAPRASAD COLLEGE)

    ReplyDelete
  28. Visit https://skmhomepage.blogspot.com/2020/05/information-centre.html

    ReplyDelete
  29. The answers of the above questions are:
    1. Before checking Z is zero or not we can not perform (X+Y)/Z in step 1. Because if Z is zero and if we perform (x+y)/Z then the result will be undefined.

    2. In Algorithm_02.2 when Z is zero then R will store the garbage value, because R would not initialize before by a proper value and the R will store the result when Z is not equal to zero.

    3. I will select Algorithm_02.3 for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02.

    4.In the Algorithm_02.3 the characteristic of R & t must be a floating point variable.

    I have read the content & realized.
    -Abhisek Midya, CMSA, Semester-2

    ReplyDelete
  30. 1)we not perform (X+Y)/Z in step 1, because at first we have to
    check if Z equals to zero or not.

    2)The value of R will garbage value, we can not execute if
    statement.



    I will select Algorithm_02.3 .

    -Vivek kumar yadav , CMSA , Semester-4

    ReplyDelete
  31. 1) Do you perform (X+Y)/Z in step 1?
    => No, we can't perform (x+y)/z at step 1.

    2) What will be the value of R, when Z=0?
    => R will hold garbage value when z is 0 (according to the Algorithm_02.2).

    3) Which algorithm you will select for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02?
    => I will select Algorithm_02.3 against the problem 02.

    4) What is the characteristic of R & t in the Algorithm_02.3?
    => R & t both are pointer to an integer where R will hold the result of operation (x+y)/z else it will hold a garbage value if z contains 0 and the latter will hold either 1 (indicating successful operation) or 0 (indicating the condition for division by zero and also for insuccessful operation).

    Roshan Kumar Sharma, CMSA, Sem-II

    ReplyDelete
  32. 1.) I will not perform (X+Y)/Z in step 1, because we need to check whether Z is equal to 0 or not.

    2.) The value of R, when Z=0, will be undefined i.e. garbage value will be given as output

    3.) Among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 I have choose Algorithm 02.3

    4.) R and t both are output variables.

    -Md Kobir Noman, CMSA, Semester-II

    ReplyDelete
  33. Some Comments Rejected among all posts with in the time 6pm....
    Reason: No Identity/ Many Comments/ All answers not given.
    Who are not seeing their comment, my request to them,
    1) read the e-mail....
    2) go through the comments showing for the structure
    3) Give all 4 answers at least in your single comment.

    Comment AFTER 6PM is not published....

    ReplyDelete
  34. 1) Do you perform (X+Y)/Z in step 1?
    Ans: We must not do that because in that case the algorithm will fail to show the result at the output, when Z=0 i.e. the result of the expression is "undefined".

    2) What will be the value of R, when Z=0?
    Ans: R will certainly hold garbage value because when Z = 0, the algorithm is not assigning any value to the variable R.

    3) Which algorithm you will select for the program among the Algorithm_02.1, Algorithm_02.2 and Algorithm_02.3 against the problem 02? 
    Ans: I will select Algorithm_02.3.

    4) What is the characteristic of R & t in the Algorithm_02.3? 
    Ans: In Algorithm_02.3,
    The algorithm computes the result of the expression - (X+Y)/Z
    If the result is undefined(i.e. for Z = 0), R holds garbage/unwanted value otherwise R will hold the calculated result of the given expression.
    And as defined in the specifications of the algorithm "X, Y, Z are three integer quantity." Thus, the result of the expression will also be integer.
    Therefore, R is obviously a variable of integer data type.

    In this algorithm, t has been used as a flag variable. If the result is undefined(R is holding garbage value) then t will be 0 otherwise t will hold 1. Thus, t is actually indicating the nature of the output of the algorithm.

    From
    Dwaipayan Bhoumick
    CMSA, Semester IV

    ReplyDelete