I tried several times how to write a algorithm and a pseudo code for a program of finding the largest value among 3 user input integers? . I couldn't make it properly. Can i be helped?
-
what you tried..add something.Alien– Alien2018-07-31 05:22:26 +00:00Commented Jul 31, 2018 at 5:22
-
@Alien i assumed that user inputs are x,y,z then if x>y then again checked the condition x>z if it is true, x is the largest. While x>y , z>y and then there is an anther condition if z>x then z is the largest number. Like wise i tried. It's hard to check conditions likewise.Kavindu Gayantha– Kavindu Gayantha2018-07-31 05:31:44 +00:00Commented Jul 31, 2018 at 5:31
-
Edit your question and add own pseudocode - seems you realize what to do but doubt in details.MBo– MBo2018-07-31 08:48:25 +00:00Commented Jul 31, 2018 at 8:48
-
Why do you think that finding the largest out of three integers is different than finding the largest out of, say, eleven integers?ChatterOne– ChatterOne2018-07-31 09:01:41 +00:00Commented Jul 31, 2018 at 9:01
Add a comment
|
2 Answers
Pseudocode for maximum of 3 integers-
print max(max(first_integer,second_integer),third_integer)
5 Comments
Kavindu Gayantha
I think i didn't get this. Not clear enough to understand to me
mighty_dev
What was hard though?
Kavindu Gayantha
i didn't get what happens with this code. max(max(first_integer, second_integer), third_integer)
mighty_dev
max(a,b) finds maximum between a and bKavindu Gayantha
Aha. . I got it now. Thankz
So you have three numbers, x, y, and z. You want the largest one. So here are some rules:
- If x > y, then the largest can't be y; it must be x or z. So check to see if x > z.
- If x < y, then the largest can't be x; it must be y or z. So check to see if y > z.
That results in the code:
if (x > y)
if (x > z)
largest = x;
else
largest = z;
else // y >= x
if (y > z)
largest = y;
else
largest = z;
If you have a max function that returns the maximum of two numbers, then you can simplify that code:
largest = max(x, y);
largest = max(largest, z);
Which can further be optimized to largest = max(max(x, y), z);
1 Comment
Kavindu Gayantha
Ok. Thankz. it has been explained well. i will try to think like this way.