Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 How to output specific information only using arrays
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tristanbryce7




PostPosted: Tue Nov 13, 2012 4:22 pm   Post subject: How to output specific information only using arrays

Hello, I have a problem , In the following program, I get the information for 10 students and their heights of a data base, and the program calculates the average height number .
Now my problem is, how do I get the problem to after it gets the 10 Peoples data, only make it output the names of the peoples whose height is higher than the average, i m thinking you would need to
use a loop and put in it somewhere
if height > average, but I have no clue what to do after that, Any one know how to do it ? I am new to arrays so I am kind of confused.


Turing:



var name : array 1 .. 100 of string (20)
var height : array 1 .. 100 of int %No size indicator b/c it is not a string
var average : real
var totalheight : int := 0
var fileNo : int
var x : int := 0
setscreen ("text")
open : fileNo, "Height Data.t", get
put "Name" : 20, "Height (cm)"
put ""
loop
    get : fileNo, skip %Skips over blank lines in data
    exit when eof (fileNo)
    x := x + 1
    %All arrays need an index
    get : fileNo, name (x), height (x)
    totalheight := totalheight + height (x)
    put name (x) : 20, height (x)
end loop
average := totalheight / x
put ""
put "The average height is ", average : 0 : 1, " cm!"
put ""
put "People with heights above the average are.."





Turing:




Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Tue Nov 13, 2012 4:28 pm   Post subject: RE:How to output specific information only using arrays

If you are confused about arrays, you should revisit some of the tutorials available from the Turing Walkthrough.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Tue Nov 13, 2012 6:44 pm   Post subject: RE:How to output specific information only using arrays

Quote:
if height > average, but I have no clue what to do after that


Well, what do you want the computer to do when height > average?
tristanbryce7




PostPosted: Tue Nov 13, 2012 8:41 pm   Post subject: Re: How to output specific information only using arrays

I am pretty clear on what we 've learned in arrays so far(which is not much lol), just not how to that specific option, and I want the program to only output the heights that are above the average
I tried putting it in a loop as

(Sorry, I do not have the Syntax for showing Turing Memorised)
loop

if height > average then
put name

end loop

something along those lines, It didnt go to well
Insectoid




PostPosted: Tue Nov 13, 2012 9:07 pm   Post subject: RE:How to output specific information only using arrays

You know how to use a counter variable, as shown in your first post. Use that.

Quote:
if height > average


height is an array, remember? You can't compare it like that. You have to compare one specific element of the array using the array syntax, which you also know to be "height (x)" where x is an integer. name is also an array, so it follows the same rules. You're almost there, you just need to add the proper array syntax and a counter variable (or use a for loop, which is actually the better solution).
Tony




PostPosted: Tue Nov 13, 2012 9:10 pm   Post subject: RE:How to output specific information only using arrays

I would suggest that you start with printing all of the heights recorded in your array, and worry about filtering it (above average or not) later.

How would you print every height?
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: