Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Algorithm, pseudo code

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?

like image 947
Kavindu Gayantha Avatar asked Nov 22 '25 21:11

Kavindu Gayantha


2 Answers

Pseudocode for maximum of 3 integers-

print max(max(first_integer,second_integer),third_integer)
like image 183
nice_dev Avatar answered Nov 25 '25 09:11

nice_dev


So you have three numbers, x, y, and z. You want the largest one. So here are some rules:

  1. If x > y, then the largest can't be y; it must be x or z. So check to see if x > z.
  2. 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);

like image 30
Jim Mischel Avatar answered Nov 25 '25 11:11

Jim Mischel