Jump to content

Hey guys, so my little sister is starting a computer science class in school and needs help with an algorithm that they have asked her to create. she was away from school for the day of the assignment due to a dentists appointment so she wasnt there for the lesson and i myself have not played with pseudo code enough to know how to do whats being asked although i have a basic code created already.

 

The question is this, devise an algorithm to compute the average, the maximum, and the minimum of a list of numbers. construct a flow chart to describe your algorithm. your algorithm should accept the list of numbers as input and print the results in a user friendly way. do not make any assumptions about how many numbers the list contains.

 

The code i have written for personal uses perviously

Pseudo Code:
 
Write "please enter 5 numbers"
Read n1,n2,n3,n4,n5
 
Write "The average is"
Set avg to (n1+n2+n3+n4+n5)/5
Write avg
 
If(n1 < n2)
  Set max to n2
Else
  Set max to n1
If(n3 > max)
  Set max to n3
If(n4 > max)
  Set max to n4
If(n5 > max)
Set max to n5
Write "The max is"
Write max
 
If(n1 > n2)
  Set min to n2
Else
  Set min to n1
If(n3 < min)
  Set min to n3
If(n4 < min)
  Set min to n4
If(n5 < min)
Set min to n5
Write "The min is"
Write min
 
So this code is limited to 5 numbers which isnt really what she needs for school so if anyone could either alter it or at least help me out i would really appreciate it
Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/
Share on other sites

Link to post
Share on other sites

I would just have it so it asks for the amount of input n, then creates a for loop like this

for(i = 0; i < n; i++)    if(x > y)        min = y;

You can probably figure out the rest.

not really haha im pretty bad with pseudo actually :/ i wrote mine almost 4 years ago when i was still in school and havent used it since so i kinda forgot it all lol

Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734302
Share on other sites

Link to post
Share on other sites

For input that doesn't have the amount pre-determined you want a loop that doesn't need a pre-determined end. 'While' loops are conventionally used in such a way. Note here the use of a separate variable to count the number of items we have. Also note that I use array notation here since it is straightforward but depending on the target language you'll possibly need another way to store data. This loop also assumes only positive numbers.

int num [];int count = 0;while (num > 0){    num[count]  = input;    count = count + 1;}

Now that you have the inputs you can go through a for loop as Likeaturtle suggested for your min/max/average. A couple of loops would go a long way to controlling those runaway if statements.

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734346
Share on other sites

Link to post
Share on other sites

not really haha im pretty bad with pseudo actually :/ i wrote mine almost 4 years ago when i was still in school and havent used it since so i kinda forgot it all lol

Pseudo code can be written many ways, convert code statements to english statements. 

Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734347
Share on other sites

Link to post
Share on other sites

For input that doesn't have the amount pre-determined you want a loop that doesn't need a pre-determined end. 'While' loops are conventionally used in such a way. Note here the use of a separate variable to count the number of items we have. Also note that I use array notation here since it is straightforward but depending on the target language you'll possibly need another way to store data. This loop also assumes only positive numbers.

int num [];int count = 0;while (num > 0){    num[count]  = input;    count = count + 1;}

Now that you have the inputs you can go through a for loop as Likeaturtle suggested for your min/max/average. A couple of loops would go a long way to controlling those runaway if statements.

Thank you much we are working it out from there and its going quite well :) guess i should brush up on pseudo before she comes asking me for help again

Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734432
Share on other sites

Link to post
Share on other sites

Thank you much

Your welcome.  :)

My rig: 2600k(4.2 GHz) w/ Cooler Master hyper 212+, Gigabyte Z68-UD3H-B3, Powercolor 7870 xt(1100/1500) w/AIO mod,

8GB DDR3 1600, 120GB Kingston HyperX 3K SSD, 1TB Seagate, Antec earthwatts 430, NZXT H2

Verified max overclock, just for kicks: http://valid.canardpc.com/show_oc.php?id=2609399

Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734476
Share on other sites

Link to post
Share on other sites

Pretty simple but, I don't know how complex your psuedo code needs to be.

 

write "Input 5 numbers"

read n1,n2,n3,n4,n5

avg = (n1+n2+n3+n4+n5)/5

min=n1

max=n1

if(min>n2) min=n2

if(max<n2) max=n2

if(min>n3) min=n3

if(max<n3) max=n3

if(min>n4) min=n4

if(max<n4) max=n4

if(min>n5) min=n5

if(max<n5) max=n5

write "The avg,min and max are: " avg "," min " and " max

 

It would be easier if we could index the numbers in some sort of a loop. (if they were an array or if we could tokenize the input, but that is out of your/her skillset so no need)

Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734645
Share on other sites

Link to post
Share on other sites

Pseudo code can take a lot of different forms (syntax) from almost pure english to nearly compilable in some language.

 

Since the question says that your algorithm already receives the list of numbers and that you don't know how many there are I don't think you need to read the numbers from the user interface or name those numbers (n1, n2, etc.).

 

I would do something along the lines of:

procedure algo(numberList)  //here are declared/initialized any variables you may need  min := 0  foreach n in numberList    //code to check min, max, ...    if n < min      min := n  //calculate avg and print everything
Link to comment
https://linustechtips.com/topic/54637-need-pseudo-code-help/#findComment-734693
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×