i have to create a loop, in the loop i have to take the input values (from keyboard) and then output min/max and average of the values entered. I have no problem with the average, but the only way i can find how to figure the min/max is through a text file example, which doesnt help me when it has to be through keyboard. help pls
8)
Name:
Anonymous2007-04-02 21:55 ID:MQBXsRHj
take the keyboard input and save it to a text file. then you're on familiar ground, and can tackle this problem with your obvious skills.
Name:
Anonymous2007-04-02 22:35 ID:PYB0/I0s
Dude, don't forget that you'll have to parse the keyboard input and convert it from an unsigned integer stream to ASCII characters (you should be very familiar with ASCII character codes), then you'll have to format it for printing to standard out, then you can perform operations on them (after casting them to long double ints), then print them to standard out as a binary stream and th
OH WAIT. Find out how many values you need to average, then store that many values in an array of that size and iterate over the array and find out that way. Also, you should maybe RTFT before you come here for help. We are the destroyers of the ignorant.
Name:
Anonymous2007-04-02 23:31 ID:HlJQ2d5/
What makes you think you have no problem with the average?
Name:
Anonymous2007-04-03 1:22 ID:GEWp483F
8)
I was gonna tell you exactly what you need to get started, but this retarded smiley just kills it for me.
>>4
I assume he's just going to add them up, then divide by the total afterwards.
>>1>>3
Dumbass, you don't need to store the values at all.
OP, define four variables: max, min, total, n. Read one line of input, set max=min=total={value} and n=1. Then start looping. Read a value; if it's greater then max, set max; if it's less than min, set min; then add it to total and n++.
When you reach the end of the input, output max, min, and total/n.
instance Show Ex where
show (Ex min max list) =
"minimum is: " ++ show min ++ "\n" ++
"maximum is: " ++ show max ++ "\n" ++
"average is: " ++ show avg ++ "\n"
where avg = foldr1 (+) list /
fromIntegral (length list)
process :: StateT Ex IO ()
process = do
lift $ putStr "Enter another number: "
str <- lift getLine
when (str /= "q") (do
(Ex a b xs) <- get
let num = read str
let min' = if num < a then num else a
let max' = if num > b then num else b
let new' = Ex min' max' (num:xs)
lift $ putStr $ show new'
put new'
process)
main :: IO ()
main = do
putStr "Enter a number: "
n <- liftM read getLine
evalStateT process (Ex n n [n])
Debug it, because I compiled/ran it after I wrote it and it didn't work :3. I think the hasNext() next() should be hasNextLine nextLine(), but that's just a guess. You're welcome.