Is this algo and pseudo for cylinder correct?

  • Thread starter Thread starter PainterGuy
  • Start date Start date
  • Tags Tags
    Cylinder
AI Thread Summary
The discussion revolves around the correctness of an algorithm and its corresponding pseudocode for calculating the volume of a cylinder based on user input for height and radius. The initial algorithm includes steps for reading inputs, calculating volume, and handling invalid input but lacks clarity on how to manage invalid entries effectively. Participants highlight that the pseudocode does not accurately reflect the algorithm's logic, particularly regarding the handling of invalid inputs. Key points include the need for a loop to repeatedly prompt for valid input until acceptable values are provided. Suggestions are made to revise the pseudocode to ensure that it correctly follows the algorithm's flow, with emphasis on using a more precise value for pi and structuring the input validation properly. The final pseudocode reflects these corrections, ensuring that the volume is only displayed after valid inputs are confirmed, thus addressing the confusion about the sequence of operations. Overall, the discussion emphasizes the importance of clear logic in both algorithms and pseudocode to prevent errors in programming.
PainterGuy
Messages
938
Reaction score
72
hello everyone,

is there someone who can please tell me whether my algoritham and pseudocode is correct? i will be very grateful.

cheers.

Algorithm:
Step 1: Read the height “l” and radius “r” of cylinder
Step 2: If “l” and/or “r” are zero or less than zero. Go to Step 5
Step 3: Volume V = (3.142)*r*r*l
Step 4: Print V
Step 5: Enter valid values of “l” and “r”
Step 6: Stop

Pseudocode:
Read the height “l” and radius “r” of cylinder
IF the height “l” and radius “r” of cylinder are not zero or less than zero THEN
Volume V = (3.142)*r*r*l
ELSE
Enter valid values of “l” and “r”
ENDIF
Print V
Stop
 
Technology news on Phys.org
painterguy said:
hello everyone,

is there someone who can please tell me whether my algoritham and pseudocode is correct? i will be very grateful.

cheers.

Algorithm:
Step 1: Read the height “l” and radius “r” of cylinder
Step 2: If “l” and/or “r” are zero or less than zero. Go to Step 5
Step 3: Volume V = (3.142)*r*r*l
Step 4: Print V
Step 5: Enter valid values of “l” and “r”
Step 6: Stop

Pseudocode:
Read the height “l” and radius “r” of cylinder
IF the height “l” and radius “r” of cylinder are not zero or less than zero THEN
Volume V = (3.142)*r*r*l
ELSE
Enter valid values of “l” and “r”
ENDIF
Print V
Stop

Seems mostly okay, except you don't seem to be very explicit about where/how you are reading the input (from a console of from a file?), and what to do if the inputs are invalid. If they are invalid, do you throw an error, and/or ask the console for corrected inputs?
 
There is no correct pseudocode. I don't see anything incorrect about it though. The algo in pseudocode is an accurate representation of the algorithm described. But, the handling of invalid input is ambiguos. Do you intend to only tell the user their input is invalid or get valid input? If the latter, your algo is wrong.
 
berkeman said:
Seems mostly okay, except you don't seem to be very explicit about where/how you are reading the input (from a console of from a file?), and what to do if the inputs are invalid. If they are invalid, do you throw an error, and/or ask the console for corrected inputs?

many thanks berkeman. much grateful for this quickly help.

i think at this stage i am not concerned where to read input from. it is up to the programmer.:smile: if the inputs are invalid then it will tell "Enter valid values of “l” and “r”". is there problem with this?:confused:

one thing more: in my pseudocode after ENDIF it says "Print V". but if the ELSE sequence has been performed then there will be no "V" hence printing of V can be performed. should not it be "Print V or "Enter Enter valid values of “l” and “r”"??

tell me please. many many thanks.

cheers:smile:
 
I would revise the algorithm as follows:

Step 1: Prompt the user to enter the height and radius of a cylinder.
Step 2: Read the height “l” and radius “r” of the cylinder
Step 3: If “l” <= 0 or “r” <= 0, then go to step 1. (Note that "or" includes the possibility of both inequalities being true.)
Step 4: Calculate V = (3.142)*r*r*l (I would use a more precise value for pi.)
Step 5: Display V
Step 6: Stop
 
Mark44 said:
I would revise the algorithm as follows:

Step 1: Prompt the user to enter the height and radius of a cylinder.
Step 2: Read the height “l” and radius “r” of the cylinder
Step 3: If “l” <= 0 or “r” <= 0, then go to step 1. (Note that "or" includes the possibility of both inequalities being true.)
Step 4: Calculate V = (3.142)*r*r*l (I would use a more precise value for pi.)
Step 5: Display V
Step 6: Stop

many thanks Mark44.:smile: i am still little but confused about pseudo.:confused: help me please.

if radius and height are greater than zero, V will be calculated and "ENDIF" will be encountered. the next step is "Print V", which is okay as long as V has been calculated. if V has NOT been calculated, it will display "Enter valid values of “l” and “r”" and "ENDIF" will be encountered. but the next step is "Print V" which is meaningless now because no V is calculated.

i think i don't understand the sequence of IF in pseudo.:redface: tell me please. many thanks for helping me with this out.

cheers:smile:



Pseudocode:
Read the height “l” and radius “r” of cylinder
IF the height “l” and radius “r” of cylinder are not zero or less than zero THEN
Volume V = (3.142)*r*r*l
ELSE
Enter valid values of “l” and “r”
ENDIF
Print V
Stop
 
painterguy said:
many thanks Mark44.:smile: i am still little but confused about pseudo.:confused: help me please.

if radius and height are greater than zero, V will be calculated and "ENDIF" will be encountered. the next step is "Print V", which is okay as long as V has been calculated. if V has NOT been calculated, it will display "Enter valid values of “l” and “r”" and "ENDIF" will be encountered. but the next step is "Print V" which is meaningless now because no V is calculated.

i think i don't understand the sequence of IF in pseudo.:redface: tell me please. many thanks for helping me with this out.

Hi painterguy. The "trick" is in Mark's step 3. If the input is invalid (i.e. either or both of the values is <= to 0), it jumps back to where it will ask for valid input from the user (step 1) and read it in (step 2). It will keep asking, reading and checking until it gets usable values.

Once it does get valid input, step 3 will be FALSE and so the program goes on to step 4, knowing it has valid input to deal with.

Normally, this would be implemented with a while or do while type loop. Although goto is generally bad programming practice, to implement the algorithm totally literally, step by step, you would need a label and a goto statement. But I would look at turning that logic into a while or do while loop. Think "do ... while input invalid", if that helps.
 
Grep said:
Hi painterguy. The "trick" is in Mark's step 3. If the input is invalid (i.e. either or both of the values is <= to 0), it jumps back to where it will ask for valid input from the user (step 1) and read it in (step 2). It will keep asking, reading and checking until it gets usable values.

Once it does get valid input, step 3 will be FALSE and so the program goes on to step 4, knowing it has valid input to deal with.

Normally, this would be implemented with a while or do while type loop. Although goto is generally bad programming practice, to implement the algorithm totally literally, step by step, you would need a label and a goto statement. But I would look at turning that logic into a while or do while loop. Think "do ... while input invalid", if that helps.

hello Grep,:wink:

that means my psedo was correct? but the highlighted lines should read the same because the "ELSE" take you back to first step which reads "Read the height “l” and radius “r” of cylinder". what does "ELSE" part in IF section of pseudo really mean?

many thanks for helping me here. you are very kind.:smile:

cheers

Pseudocode:
Read the height “l” and radius “r” of cylinder
IF the height “l” and radius “r” of cylinder are not zero or less than zero THEN
Volume V = (3.142)*r*r*l
ELSE
Enter valid values of “l” and “r”
ENDIF
Print V
Stop
 
Let's look at your algorithm first.
Suppose the user enters valid values for l and r in step 1. Then the if condition in step 2 is false, so no branch to step 5 occurs. In step 3, V is calculated, and in step 4, this value is displayed.
Step 5 now asks the user to enter valid values for l and r, which is pointless because the user already entered valid values back in step 1.

Let's look at another scenario. Suppose that in step 1 the user enters values for l or r that are invalid. The if condition in step 2 is now true, so a branch to step 5 occurs. In step 5, the user is asked to enter valid values for l and r. The next step, step 6, causes the program to stop. The user might have entered values that were valid, but nothing was done with them.
Code:
Step 1: Read the height “l” and radius “r” of cylinder
Step 2: If “l” and/or “r” are zero or less than zero. Go to Step 5
Step 3: Volume V = (3.142)*r*r*l
Step 4: Print V
Step 5: Enter valid values of “l” and “r”
Step 6: Stop

Let's take a look at the pseudocode now. One problem is that it does not describe the same actions as your algorithm. In my experience it's unusual for both an algorithm and pseudocode to be written, but apparently your instructor wants both. In any case, the algorithm and pseudocode should describe the same actions.

If the user enters valid values (i.e., positive) for both l and r, the if condition is true, and V is calculated. The next statement that executes causes the value for V to be displayed. After that, the program stops. This is pretty much the right behavior.

However, if the user enters invalid values (nonpositive) for l or r, the if condition is false, so the else clause executes, asking the user to enter valid values for l and r. The next statement that executes is the one that causes a value for V to be displayed. This is not correct, since V has not been calculated. After that, the program stops.

Code:
Pseudocode:
Read the height “l” and radius “r” of cylinder
IF the height “l” and radius “r” of cylinder are not zero or less than zero THEN
Volume V = (3.142)*r*r*l
ELSE
Enter valid values of “l” and “r”
ENDIF
Print V
Stop
 
  • #10
painterguy said:
that means my psedo was correct? but the highlighted lines should read the same because the "ELSE" take you back to first step which reads "Read the height “l” and radius “r” of cylinder". what does "ELSE" part in IF section of pseudo really mean?
No, that's not the way it works. A statement with if and else causes one of two alternatives to be chosen. Neither alternative causes a statement before the if - else structure to be executed.
 
  • #11
Mark44 said:
Let's take a look at the pseudocode now. One problem is that it does not describe the same actions as your algorithm. In my experience it's unusual for both an algorithm and pseudocode to be written, but apparently your instructor wants both. In any case, the algorithm and pseudocode should describe the same actions.

If the user enters valid values (i.e., positive) for both l and r, the if condition is true, and V is calculated. The next statement that executes causes the value for V to be displayed. After that, the program stops. This is pretty much the right behavior.

However, if the user enters invalid values (nonpositive) for l or r, the if condition is false, so the else clause executes, asking the user to enter valid values for l and r. The next statement that executes is the one that causes a value for V to be displayed. This is not correct, since V has not been calculated. After that, the program stops.

Code:
Pseudocode:
Read the height “l” and radius “r” of cylinder
IF the height “l” and radius “r” of cylinder are not zero or less than zero THEN
Volume V = (3.142)*r*r*l
ELSE
Enter valid values of “l” and “r”
ENDIF
Print V
Stop

hello Mark44,:smile:

many thanks for pointing errors out in both algorithm and pseudocode. can you help me please to correct this pseudocode? i have tried to no success. i will be much grateful if you can lead me to correct pseudocode.

cheers:smile:
 
  • #12
OK, here's the algorithm I gave in post #5, slightly changed (better approximation for pi).
Code:
Step 1: Prompt the user to enter the height and radius of a cylinder
Step 2: Read the height “l” and radius “r” of the cylinder
Step 3: If “l” <= 0 or “r” <= 0, then go to step 1. (Note that "or" includes the possibility of both inequalities being true.)
Step 4: Calculate V = 3.14159*r*r*l 
Step 5: Display V
Step 6: Stop

Here's the pseudocode based on the algorithm above.
Code:
Start:  Prompt the user to enter the height and radius of a cylinder
        Read l
        Read r
        if ( (l <= 0) OR (r <= 0)) 
           goto Start
        else 
           V =  3.14159*r*r*l 
        Display V

I didn't add any pseudocode for Stop, since many languages (including C and C++) don't have a keyword for Stop.
 
  • #13
Mark44 said:
Here's the pseudocode based on the algorithm above.
Code:
Start:  Prompt the user to enter the height and radius of a cylinder
        Read l
        Read r
        if ( (l <= 0) OR (r <= 0)) 
           goto Start
        else 
           V =  3.14159*r*r*l 
        Display V

I didn't add any pseudocode for Stop, since many languages (including C and C++) don't have a keyword for Stop.

many, many thanks Mark44. :smile:
you people here are very nice. i don't know y i didn't come to this forum before!:cry:

because i am not writing pseudocode for direct conversion into in any programming language so i can put "Stop" as a safety precaution at end. but i was thinking where i will put ENDIF. below "Display V"?:confused:

cheers :smile:
 
  • #14
You could put the endif statement above the statement that displays V, like this:
Code:
Start:  Prompt the user to enter the height and radius of a cylinder
        Read l
        Read r
        if ( (l <= 0) OR (r <= 0)) 
           goto Start
        else 
           V =  3.14159*r*r*l 
        end if
        Display V

... or you could do it this way:
Code:
Start:  Prompt the user to enter the height and radius of a cylinder
        Read l
        Read r
        if ( (l <= 0) OR (r <= 0)) 
           goto Start
        else 
           V =  3.14159*r*r*l 
           Display V
        end if
Normally it makes a difference where it goes, but with this program structure, either way does the same thing. In the first pseudocode, the only way that the "display V" statement executes is when the else clause is the one that is chosen. Since the "display V" statement follows the whole if-else-end if structure, the "display V" statement executes next. Putting the "display V" statement inside the else clause doesn't change anything.
 

Similar threads

Back
Top