Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

java

Name: Anonymous 2007-04-02 21:47 ID:lENHJj++

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: Anonymous 2007-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: Anonymous 2007-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: Anonymous 2007-04-02 23:31 ID:HlJQ2d5/

What makes you think you have no problem with the average?

Name: Anonymous 2007-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.

You're on your own, kid.

Name: Anonymous 2007-04-03 1:59 ID:SHKEH8Sx

>>5
8)
8)
8)
8)
8)
8)
8)
8)
8)
8)
8)
8)

Name: Anonymous 2007-04-03 2:29 ID:E9Q9j2ej

>>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.

Name: Anonymous 2007-04-03 5:22 ID:zJVDTp4G

>>7
Can you elaborate on that?

Name: Anonymous 2007-04-03 5:46 ID:PCXC/5lf

>>6
Can you elaborate on that?

Name: Anonymous 2007-04-03 6:47 ID:jWCrT+sL

import Control.Monad
import Control.Monad.State
import System.Environment

data Shit = Shit Float Float Float Int [Float]
          deriving Show


getShit :: StateT Shit IO ()
getShit = do
    lift $ putStr "Enter a number: "
    str <- lift getLine
    when (str /= "q") (do

     (Shit a b c n 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 avg' = (foldr1 (+) (num:xs)) / fromIntegral n

     lift $ putStrLn $ "minimum is " ++ show min'
     lift $ putStrLn $ "maximum is " ++ show max'
     lift $ putStrLn $ "average is " ++ show avg'

     put $ Shit min' max' c (n + 1) (num:xs)

     getShit)

main :: IO ()
main = do
    putStr "Enter a number: "
    n <- liftM read getLine
    evalStateT getShit (Shit n n n 1 [n])


The worst Haskell code possible > the best Java code possible.

Name: Anonymous 2007-04-03 6:52 ID:jWCrT+sL

oops. Don't really need to keep track of the average

import Control.Monad.State

data Shit = Shit Float Float Int [Float]
          deriving Show


getShit :: StateT Shit IO ()
getShit = do
    lift $ putStr "Enter a number: "
    str <- lift getLine
    when (str /= "q") (do

     (Shit a b n 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 avg' = (foldr1 (+) (num:xs)) / fromIntegral n

     lift $ putStrLn $ "minimum is " ++ show min'
     lift $ putStrLn $ "maximum is " ++ show max'
     lift $ putStrLn $ "average is " ++ show avg'

     put $ Shit min' max' (n + 1) (num:xs)

     getShit)

main :: IO ()
main = do
    putStr "Enter a number: "
    n <- liftM read getLine
    evalStateT getShit (Shit n n 1 [n])

Name: Anonymous 2007-04-03 7:30 ID:PCXC/5lf

     let min' = min a num
     let max' = max b num
     let avg' = (sum (num:xs)) / fromIntegral n

Name: Anonymous 2007-04-03 8:52 ID:5eUA7rWw

>>10
>>11
= haskell failure fag

Name: Anonymous 2007-04-03 10:22 ID:E7bq3wsJ

Haskell fags

Name: Anonymous 2007-04-03 16:30 ID:jWCrT+sL

>>13,14

EVEN BETTER:

import Control.Monad.State

data Ex = Ex Float Float [Float]

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])

Name: Anonymous 2007-04-03 16:37 ID:zJVDTp4G

>>15
This sucks horribly. Do it better this time.

Name: Anonymous 2007-04-04 2:20 ID:5fPqZZ8j

>>15

>>12, motherfucker, did you read it?

Name: Anonymous 2007-04-04 4:44 ID:WvfTfg+6

import java.util.*;

public class Driver {
  public static void main (String[] args) {
    int iMin = Integer.MAX_VALUE, iMax = Integer.MIN_VALUE, iInput, iNum = 0, iTotal = 0;
    boolean quit = false;
    Scanner scan = new Scanner(System.in);

    do {
      try {
        System.out.println("Enter an integer");
        iInput = scan.nextInt();
        iNum++;
        iTotal += iInput;
        if (iInput < iMin)
          iMin = iInput;
        if (iInput > iMax)
          iMax = iInput;
      }
      catch (Exception InputMismatchException) {
        while (scan.hasNext())
          scan.next(); // flushes the shitty bad info
        System.out.println("Not an integer\nDid you want to stop (y/n)?");
        quit = scan.nextLine().toLowerCase().charAt(0) == 'y';
      }
    } while (!quit);
    if (iNum != 0) // divide by zero, lulz
      System.out.println("Min: " + iMin + "\nMax: " + iMax + "\nAverage: " + (double)iTotal / iNum);
    while (true)
      System.out.println("I love gay porn");
  }
}

Name: Anonymous 2007-04-04 4:46 ID:zjN12iZ1

>>18

I see what you did there...

Name: Anonymous 2007-04-04 9:26 ID:1Yz5rrI1

>>18
Fukken saved!
First time I see scanner, but sure as hell beats the other convoluted input methods.

Name: Anonymous 2007-04-04 9:54 ID:332T9hUp

Shouldn't be using a list, but it was faster to write and I'm lazy.


a = []
while True:
    try:
        a.append(int(raw_input()))
    except EOFError:
        break
    except ValueError:
        print "You're stupid"
print 'Min=%d Max=%d Avg=%d' % (min(a), max(a), sum(a) / len(a))

Name: Anonymous 2007-04-04 15:24 ID:w2zT73vI

>>20

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.

Name: Anonymous 2007-04-10 8:34 ID:hLakpL9v

Name: ​​​​​​​​​​ 2010-09-09 13:57

Name: Anonymous 2010-11-26 17:12

Name: Anonymous 2010-11-28 15:18

Name: Sgt.Kabu놋綶kiman劲ᘆ 2012-05-28 23:53

Bringing /prog/ back to its people
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List