Is this algo and pseudo for cylinder correct?

  • Thread starter PainterGuy
  • Start date
  • Tags
    Cylinder
In summary, the algorithm and pseudocode provided describe a process for calculating the volume of a cylinder given its height and radius. It involves reading the input, checking for validity, and calculating the volume if the input is valid. If the input is not valid, the program prompts the user to enter valid values. The pseudocode uses an if-else statement to handle invalid input, and then proceeds to print the calculated volume. It also includes a step to stop the program.
  • #1
PainterGuy
940
70
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
  • #2
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?
 
  • #3
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.
 
  • #4
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:
 
  • #5
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
 
  • #6
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
 
  • #7
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.
 
  • #8
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
 
  • #9
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.
 

Related to Is this algo and pseudo for cylinder correct?

What is an algorithm for a cylinder?

An algorithm for a cylinder is a set of step-by-step instructions for solving a problem related to cylinders. It typically involves mathematical calculations and logical operations.

What is pseudo code for a cylinder?

Pseudo code for a cylinder is a simplified and structured representation of an algorithm for a cylinder. It is not an actual programming language, but rather a way to express the logic of an algorithm in plain English or another natural language.

How do you know if an algorithm for a cylinder is correct?

An algorithm for a cylinder is considered correct if it produces the desired output for all possible inputs and if it follows the principles of good algorithm design, such as being efficient, understandable, and maintainable.

Can an algorithm for a cylinder be incorrect?

Yes, an algorithm for a cylinder can be incorrect if it produces incorrect results for certain inputs or if it violates the principles of good algorithm design. In such cases, the algorithm needs to be revised and improved.

Why is it important to use algorithms and pseudo code for cylinders?

Using algorithms and pseudo code for cylinders helps to solve complex problems in a systematic and efficient manner. It also allows for easier communication and collaboration among scientists and researchers, as well as making it easier to review and improve upon existing algorithms.

Similar threads

Replies
1
Views
879
  • Programming and Computer Science
Replies
8
Views
1K
  • Special and General Relativity
Replies
11
Views
610
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
7
Views
2K
  • Introductory Physics Homework Help
Replies
1
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Sci-Fi Writing and World Building
Replies
9
Views
2K
  • Introductory Physics Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
9
Views
1K
Back
Top