 Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki Blog Search Turing Chat Room Members
Average Height Program Arrays?       Author Message
codergirl Posted: Wed May 09, 2018 9:08 pm   Post subject: Average Height Program Arrays?

I'm creating a program that gets the user to input a bunch of heights, finds the average height, then outputs the heights that are above this average height.

I know how to do the average, but outputting the heights above the average height is just not working.

I don't know if it's the order, or what...?

If you could help me out it would be greatly appreciated! (bits will come your way )

Thanks!

CODE:

var range:int:=0
var numbers: flexible array 1..range of int

put "How many heights do you want to enter?"
get range
put "Enter your", range, "height"
for counter: 1..range
new numbers, counter
get numbers (counter)
end for

var total: int:=0
for counter: 1..range

total:=total+numbers (counter)

if numbers (counter) > (total/range) then

put numbers (counter)

end if

end for

put "the mean is", total/range    Srlancelot39  Posted: Thu May 10, 2018 11:02 am   Post subject: Re: Average Height Program Arrays?

 code: var range:int:=0 var numbers: flexible array 1..range of int put "How many heights do you want to enter?" get range put "Enter your", range, "height" for counter: 1..range     new numbers, counter     get numbers (counter) end for var total: int:=0 for counter: 1..range     total:=total+numbers (counter)     if numbers (counter) > (total/range) then         put numbers (counter)     end if end for put "the mean is", total/range

You are comparing the heights to the average before you are finished calculating it. As a result, only the last height is compared to the completed average.

EDIT:
Hint: As far as I can predict right now, you could fix the issue without adding any lines of code. You have 2 lines that should move to another location... codergirl Posted: Thu May 10, 2018 7:37 pm   Post subject: Re: Average Height Program Arrays?

Would you be able to clarify? I tried to move the 2 that are logically supposed to move...but nothing works     Srlancelot39  Posted: Fri May 11, 2018 9:13 am   Post subject: Re: Average Height Program Arrays?

Which 2 lines did you move? Post any updated code that you have.

In order to build the total, you need a for loop. Once this is done, you need a second for loop to perform the comparisons.

You currently have two for loops, but the first is being used for getting the heights, which is appropriate, however, you could calculate the total as well while getting the heights here. This way, your heights total is ready for averaging before it is needed for comparison. You could use three for loops, but it would not be as efficient.

The key point though is that you cannot calculate the total and average while making the comparisons, since you would be comparing each height against a different average. The first height is currently being compared to itself, and only the last height is being compared to the final average. codergirl Posted: Sat May 12, 2018 9:28 pm   Post subject: Re: Average Height Program Arrays?

I got it to work, thanks so much for your help! Have some bits  Srlancelot39  Posted: Mon May 14, 2018 7:15 am   Post subject: Re: Average Height Program Arrays?

Good to hear, and thanks!

I'm assuming your solution looked something like this:

 Turing: var total: int:=0 var range:int:=0 var numbers: flexible array 1..range of int put "How many heights do you want to enter?" get range put "Enter your", range, "height" for counter: 1..range     new numbers, counter     get numbers (counter)     total:=total+numbers (counter) end for for counter: 1..range     if numbers (counter) > (total/range) then         put numbers (counter)     end if end for put "the mean is", total/range codergirl Posted: Tue May 15, 2018 9:19 pm   Post subject: Re: Average Height Program Arrays?

I believe so, yes! I just had to move a few lines like you suggested and that did the trick!

How do you get the code in the white box? I want to be more proper when posting code from now on...   Srlancelot39  Posted: Wed May 16, 2018 12:21 pm   Post subject: RE:Average Height Program Arrays?

You can use the Code tag, or the Syntax tag. When using the Syntax tag, put the name of the language in the quotes provided.

Almost all tags work like so:         