Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Algorithm to find smallest element

  1. Jan 17, 2007 #1
    I'm working on an algorithm that finds the smallest element among a, b, and c.

    Here is what I have so far:

    Input: a,b,c
    Output: small; smallest element in the sequence a,b,c
    a = 2, b = 4, c = 3
    Small = a
    If b < small, then small = b
    If c < small, then small = c

    Is this right?
     
  2. jcsd
  3. Jan 17, 2007 #2

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    How could it be wrong?
     
  4. Jan 17, 2007 #3
    I'm just wondering if I worded it correct and it constitutes as an algorithm?
     
  5. Jan 17, 2007 #4

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    It's perfect.
     
  6. Jan 17, 2007 #5

    HallsofIvy

    User Avatar
    Science Advisor

    Well, except for one thing: According to the algorithm, it gets a, b, c as input and then immediately sets a= 2, b= 4, c= 3. No matter what a, b, c are, this algorithm outputs 2!
     
  7. Jan 17, 2007 #6

    Dick

    User Avatar
    Science Advisor
    Homework Helper

    Depends on where you think the problem statement ends and the algorithm starts. Maybe we need a new punctuation symbol here.
     
  8. Jan 17, 2007 #7
    I actually added the a = 2, b = 4, c = 3 portion as I thought that was what was needed in order to perform the algorithm
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook