Computer Science Canada

outputing the highest and the lowest numbers.. help! asap!

Author:  Ange [ Wed Sep 29, 2004 10:10 pm ]
Post subject:  outputing the highest and the lowest numbers.. help! asap!

Hi! Sorry, I'm a totally n00b but i really need help. I'm really behind in my class and they've moved on to something totally new. I'm still stuck in the back trying to figure out how turing works. Anyways, one of the example questions that the book gave me didnt have an answer to it cause it said answers varied... so i was wondering if you could help me understand this concept on receiving a list and having the turing program output the highest number and the lowest number.

so, as an example.. a guy is trying to selling a car, and his selling cost is $2000.00, but he's willing to go to $1500.00. so he gets a couple of offers and for some unknown reason, since im guessing he just couldn't read the list and figure out which guy would pay him the most, he wanted to set a program that could enter the offers he received. basically, the program is what im supposed to create... which should output:

- the highest offer AND the name of the person who offered it
- the lowest offer and the name of the person who offered it
- the number of orders that he'd accept (like at and over $1500.00)
- the number of orders that he wouldn't accept (under $1500.00)
- the average offer price

so i get the "input a name" and "input a value" part..

so something like:
put "Enter the person's name."
get name
put "Enter the amount that the person is offering."
get offerprice

but how on earth can you make turing tell you who has the highest/lowest price AND their name? and what's the code for finding the average?

please help me... im so lost. ><
thank you!!!

Ange

Author:  wtd [ Wed Sep 29, 2004 10:28 pm ]
Post subject: 

You'll want to create a record type, where each record stores the name of the person making the offer, and the price they're willing to pay.

Then you'll want a function which iterates through them and finds the record with the maximum price. Since Turing doesn't seem to do so well with recursion and list operations, I'd suggest an imperative approach.

Basically, set the first offer's price to be your starting point, then iterate over the rest of the array of records. If the current offer's price is greater than the previous high price, make the current high price the current offer.

code:
winningOffer = firstOffer
for offer in offers
   if offer.price > winningOffer.price then
      winningOffer = offer
   end if
end for


Just replace the > with a < to get the minimum offer.

The Turing tutorials section provides excellent tutorials on Records, Arrays, and Loops (specifically For loops).

Author:  Ange [ Wed Sep 29, 2004 10:59 pm ]
Post subject: 

first off, thank you soooo much for replying to my post... but sorry!! you're going to have to dumb it down.. A LOT. i am a total IDIOT when it comes to computer programming. bear with me. >< please explain again? haha.. sorry again!!

Ange

Author:  wtd [ Wed Sep 29, 2004 11:21 pm ]
Post subject: 

A "record" takes two or more individual chunks of information and groups them together to describe something more complex. In this case it's an offer a customer is making on a car. An offer has a name attached to it (the name of the customer). That's a string. It also has a price, which for simplicity sake is an integer.

In psuedocode (that may or may not be valid Turing code):

code:
type Offer : record
   name : string
   price : int
end record


Using a record like this means that we can keep two connected pieces of information connected in a way that isn't easy to break.

Now, to create an offer, we just do something like:

code:
var bobsOffer : Offer
bobsOffer.name := "Bob Smith"
bobsOffer.price := 1800


But we have lots of offers to sort through, so the best way to store lots of things of the same type, that are all related to the same problem is an array.

code:
var offers : array 1 .. 10 of Offer


That assumes we have ten offers. It might be more than that, or it might be less.

Now, we need a way to find the maximum price offered. The basic problem we're faced with is how to find the largest number in an array?

Well, putting aside the obvious recursive solution that isn't well suited to Turing, the simplest solution goes something like:

Create a variable to store the maximum number. Initialize that variable to the first number. Now, go through the rest of the numbers one by one. Compare each to the max number. If it's bigger than the max, make that number the new max.

code:
var numbers : array 1 .. 5 of int := init(3, 2, 1, 7, 6)

var max : int := numbers(1)

for i : 2 .. 5
   if numbers(i) > max then
      max := numbers(i)
   end if
end for


Now, it's easy to adapt that to this problem. Instead of going through numbers directly, though, we're going through records which have a number as part of them.

code:
var offers : array 1 .. 10 of Offer

% set all of the offers' names and prices

var maxOffer : Offer := offers(i)

for i : 2 .. 10
   if offers(i).price > maxOffer.price then
      maxOffer := offers(i)
   end if
end for


Now you have the record with the highest price, which means you also have the name of the person who made that offer!

code:
maxOffer.name


The same process just works with a less than operator to get the minimum offer.

Author:  Ange [ Thu Sep 30, 2004 1:08 am ]
Post subject: 

lol.. i think maybe cause im dead tired and absolutely brainfried.. but... i HONESTLY do not get it.. haha.. i tried everything you said.. and when i put it all together.. it screwed up.. >< ack. i've been at this for 4 hours.. do you mind showing me the entire code once? i believe it will be on the exam.. which is on friday.. please please please and thank you!!

Ange

Author:  wtd [ Thu Sep 30, 2004 1:37 am ]
Post subject: 

What parts of it do you understand? If you don't understand it, showing you the code won't do any good.

Author:  Ange [ Thu Sep 30, 2004 1:52 am ]
Post subject: 

Well, I have everything with the variables of inputing the names and the offers.. however, when asked to output the highest value WITH the name.. or the lowest.. i've been trying to do what you've shown me.. and it's not working.. currently the computer lab has closed for the night and i have no turing program on my pc to try anything out on.. i think im getting confused with applying the concepts to the program.. i've never learned in class about arrays or records.. all i know is formatting, "put", "get", "if", "else", "elsif".. etc.. thats about it. so some of the things you've said before, i have no idea how to incorporate it into a program.. maybe it's the order of the commands, i dont know.. im not sure what I'm doing... I've never done computer programming in my life before.. and am very very confused...

Ange

Author:  wtd [ Thu Sep 30, 2004 2:14 am ]
Post subject: 

Utterly untested, because I don't know Turing or have the environment available. Smile

code:
type Offer : record
   name : string
   price : int
end record

var maxNumberOfOffers : int := 10
var numberOfOffers : int := 0

var offers : array 1 .. 10 of Offer

for i : 1 .. maxNumberOfOffers
   var answer : string
   
   put "Input an offer: "
   get offers(i).price
   put "And your name is? "
   get offers(i).name

   numberOfOffers += 1

   if numberOfOffers < maxNumberOfOffers then
      put "Input another offer?"
      getch(answer)

      exit when answer = "n" or answer = "Y"
   else
      exit
   end if
end for

var maxOffer : Offer := offers(1)
var minOffer : Offer := offers(1)

for i : 2 .. numberOfOffers
   if offers(i).price > maxOffer.price then
      maxOffer := offers(i)
   end if
end for

for i : 2 .. numberOfOffers
   if offers(i).price < minOffer.price then
      minOffer := offers(i)
   end if
end for

put "The minimum offer was ", minOffer.price, " from ", minOffer.name
put "The maximum offer was ", maxOffer.price, " from ", maxOffer.name

Author:  Ange [ Thu Sep 30, 2004 8:27 am ]
Post subject: 

thank you thank you thank you.. i will test it again at the comp lab todayyy... yay. here's hoping i wont fail the exam! thanks again!!

Ange

Author:  Ange [ Thu Sep 30, 2004 2:19 pm ]
Post subject: 

Okay.. so far.. this is what I have.. and i keep screwing up.. but the one you gave me.. i can't use... what do you think I should do? I can now separate the two, one in the high category, the other in the low category.. but it doesnt distinguish which one is the lowest or which is the highest.

code:

var offerprice : int
var person : string
var countperson : int
var numoffer : int
var lowestoffer : int
var highestoffer : int
var counter : int
var c : real
var minoffer : int
var maxoffer : int
var lowprice : int
var totalamount : int
var average : int
var number : int

counter := 0
highestoffer := 0
lowestoffer := 0
minoffer := 0
maxoffer := 0
average := 0
totalamount := 0

    put "How many people have offers?"
    get number

loop
    exit when counter = number
   
    put "Enter the person's name."
    get person
    put "Enter the offered amount."
    get offerprice
   
    totalamount := offerprice + offerprice
   
    if offerprice < 1500 then
        lowestoffer := offerprice
        minoffer := minoffer +1
   
    elsif offerprice > 1500 then
        highestoffer := offerprice
        maxoffer := maxoffer + 1
       
end if

    average := totalamount
    counter := counter + 1

end loop
put "The highest offer was from ", offerprice, " ."
put "The lowest offer was from ", lowprice, " ."
put "The number of unacceptable offers were ", minoffer, " in total."
put "The number of acceptable offers were ", maxoffer, " in total."

Author:  wtd [ Thu Sep 30, 2004 2:26 pm ]
Post subject: 

A quick hint: when you know how many times your loop is going to loop, you can use a for loop.

code:
var numberOfOffers : int := 3
var counter : int := 0
loop
   exit when counter = numberOfOffers
   % ...
   counter += 1
   % the previous line is equivalent to:
   % counter := counter + 1
end loop


Is equivalent to:

code:
for counter : 0 .. 2
   % ...
end for


Much cleaner.

Author:  wtd [ Thu Sep 30, 2004 2:29 pm ]
Post subject: 

Second tip:

code:
if offerprice < 1500 then
   lowestoffer := offerprice
   minoffer := minoffer +1
elsif offerprice > 1500 then
   highestoffer := offerprice
   maxoffer := maxoffer + 1
end if


Doesn't check to see what happens if offerprice is exactly 1500.

Author:  Ange [ Thu Sep 30, 2004 2:40 pm ]
Post subject: 

hmm but the main point of the program is that the person doesnt know how many offers he will receive. he's supposed to get the offer, then just input it in with everything else, and the output should read the highest offer with the name and the lowest offer with the name.

i still have trouble with the name.. how do you do that?

Author:  wtd [ Thu Sep 30, 2004 2:55 pm ]
Post subject: 

Well, you can also do something like:

code:
var numOffers : int

get numOffers

for counter : i .. numOffers
   % ...
end for


If you can use records...

code:
type Offer : record
   name : string
   price : int
end record


Then something like:

code:
type Offer : record
   name : string
   price : int
end record

var bestOffer, worstOffer : Offer
var numOffers, totalPriceOffered : int := 0

% setup the initial values for the best and worst offers
bestOffer.price := 0
worstOffer.price := 0

put "How many offers? "
get numOffers

for counter : 1 .. numOffers
   var currentOffer : Offer

   put "Your name? "
   get currentOffer.name

   put "You're willing to pay? "
   get currentOffer.price

   if currentOffer.price > bestOffer.price then
      bestOffer := currentOffer
   end if

   if currentOffer.price < worstOffer.price then
      worstOffer := currentOffer
   end if

   totalPriceOffered += currentOffer.price
end for

var averagePriceOffered : int := totalPriceOffered / numOffers

Author:  Ange [ Thu Sep 30, 2004 3:09 pm ]
Post subject: 

I can get the number of offers, no problem... but i used the highest and the lowest...

code:

     if offerprice <= 1500 then
        lowestoffer := offerprice
        minoffer := minoffer +1
   
    elsif offerprice > 1500 then
        highestoffer := offerprice
        maxoffer := maxoffer + 1


but, they don't work.. like, turing will read it as the final offerprice for each of them.. if i typed in 123, 234, 345, 456... turing will read that 456 is the lowest number.. as well as the highest... how do i correct this?

Author:  wtd [ Thu Sep 30, 2004 3:26 pm ]
Post subject: 

Probably because you're doing the wrong thing over and over again and expecting it to magically work. Some people call that "insanity". Smile

You're comparing it to a static number of 1500. That makes no sense. By those standards, as long as the numbers are less than 1500, the last number will always be considered the minimum.

The basic algorithm for finding the max and min is basically:

Start out with the first number, or a reasonable default such as zero as your minimum oir maximum, then compare it to the next number. If the next number is bigger than the max, then it's your new maximum. Just continue doing this until you get to the end of the numbers.

Do the same thing for finding the minimum, but check to see if each number is less than the current minimum, and if it is, make that number your new minimum. Lather, rinse, repeat.

Author:  the_short1 [ Thu Sep 30, 2004 6:32 pm ]
Post subject: 

wtd... that last post was very good... + 10 bits... (maybe u can make a tutorial for that! u have it very ncie and simply put.... just add in a little code to show what ur talking about...)

Author:  wtd [ Thu Sep 30, 2004 6:39 pm ]
Post subject: 

If I were to do so, I'd rather put it in General Programming and provide samples in numerous languages in one place, since it's a general kind of programming problem.

Would it be kosher then to put links to it in other Tutotials forums?

Author:  miracle [ Sun Oct 03, 2004 3:32 pm ]
Post subject: 

Edited... different quesiton now O.o

edited again O.o

Quote:
type Offer : record
name : string
price : int
end record

var bestOffer, worstOffer : Offer
var numOffers, totalPriceOffered : int := 0

% setup the initial values for the best and worst offers
bestOffer.price := 0
worstOffer.price := 0

put "How many offers? "
get numOffers

for counter : 1 .. numOffers
var currentOffer : Offer

put "Your name? "
get currentOffer.name

put "You're willing to pay? "
get currentOffer.price

if currentOffer.price > bestOffer.price then
bestOffer := currentOffer
end if

if currentOffer.price < worstOffer.price then
worstOffer := currentOffer
end if

totalPriceOffered += currentOffer.price
end for

var averagePriceOffered : int := totalPriceOffered / numOffers


i tried to run that, and it worked but the lowest amount and highest amount is the same, and i been trying to figure out how to fix it but still got no clue...

------------------
i got the name thing to work now =D but still the lowest is no good =/
plz help Sad

Author:  miracle [ Sun Oct 03, 2004 7:09 pm ]
Post subject: 

um never mind, i fixed everything, but thanks for the help wtd, most apperciated ^^

blah...

edit...

the program only works when i start off with a largeer number on the first turn....

some1 help Laughing


: