All about flooble | fun stuff | Get a free chatterbox | Free JavaScript | Avatars    
perplexus dot info

Home > Algorithms
Finding the maximum (Posted on 2004-04-21) Difficulty: 3 of 5
Using only standard math operators (+, -, *, /) and functions (absolute value, square root, powers, and so on), give a function that calculates MAX(A,B,C).

Alternate version, for programmers only: using any language (BASIC, C, Excel, whatever) find the maximum of A, B, and C, but be careful when writing the program, because you cannot use the "" key, for it's broken (thus writing things like IF A>B is impossible...) and you cannot use the "M" key either, for it's also broken (...and writing H=MAX(A,B) is also not possible.)

See The Solution Submitted by e.g.    
Rating: 4.0000 (5 votes)

Comments: ( Back to comment list | You must be logged in to post comments.)
Solution Super-Corrected VisualBasic Program (conforms to the problem requirements) | Comment 12 of 16 |
This time I avoided the "</>" symbols by using "=" instead. I assume we're still allowed to use the "=" symbol. My program works for all positive and negative numbers, and zero, with 0 to any number of decimal places. (It even works for 666 if I hardcode that value in the program).
 
Imports System
Imports System.Runtime.InteropServices
Imports System.Math
Module Module1
 
    Sub Main()
        Randomize()
        Dim strGoAgain As String
        strGoAgain = "Y"
        While strGoAgain = "Y"
            Mainline()
            Console.WriteLine("Want to try again ?")
            strGoAgain = UCase(Console.ReadLine())
        End While
    End Sub
 
    Sub Mainline()
        Dim dblNumberA As Double
        Dim strSign As String
        Dim dblNumberB As Double
        Dim dblNumberC As Double
        Dim dblWork1 As Double
        Dim dblWork2 As Double
        Dim dblWork3 As Double
        Dim dblWholeA As Double
        Dim dblWholeB As Double
        Dim dblWholeC As Double
        Dim dblSmallerA As Double
        Dim dblsmallerB As Double
        Dim dblSmallerC As Double
        Dim dblOldNumber1 As Double
        Dim dblOldNumber2 As Double
        Dim intPower As Integer
        Dim strGoodA As String
        Dim strGoodB As String
        Dim strGoodC As String
        Dim intFactor As Integer
        Dim RoundDown As Double
        Dim vartemp As Double
        Dim intPowerOf10 As Integer
        Dim strTimeOfDay As String
        Dim dblDiffAB As Double
        Dim dblDiffAC As Double
        Dim dblDiffBC As Double
        '
        Console.WriteLine("  ")
        '
        dblNumberA = 0
        While dblNumberA = 0
            Console.WriteLine("Please enter a nonzero whole number")
            dblNumberA = Int(Console.ReadLine())
        End While
        intPowerOf10 = 99999
        While intPowerOf10 > 99998
            Console.WriteLine("Please enter a whole number P < 99999 " & _
            "to complete the formula: ")
            Console.WriteLine("First number = " & Str(dblNumberA) & _
            " * 10 ^ P ")
            intPowerOf10 = Int(Console.ReadLine())
        End While
        dblNumberA *= 10 ^ intPowerOf10
        '
        dblNumberB = 0
        While dblNumberB = 0
            Console.WriteLine("Please enter another nonzero whole number")
            dblNumberB = Int(Console.ReadLine())
        End While
        intPowerOf10 = 99999
        While intPowerOf10 > 99998
            Console.WriteLine("Please enter a whole number P < 99999 " & _
            "to complete the formula: ")
            Console.WriteLine("First number = " & Str(dblNumberB) & _
            " * 10 ^ P ")
            intPowerOf10 = Int(Console.ReadLine())
        End While
        dblNumberB *= 10 ^ intPowerOf10
        dblNumberC = 0
        While dblNumberC = 0
            Console.WriteLine("Please enter a nonzero whole number")
            dblNumberC = Int(Console.ReadLine())
        End While
        intPowerOf10 = 99999
        While intPowerOf10 > 99998
            Console.WriteLine("Please enter a whole number P < 99999 " & _
            "to complete the formula: ")
            Console.WriteLine("First number = " & Str(dblNumberC) & _
            " * 10 ^ P ")
            intPowerOf10 = Int(Console.ReadLine())
        End While
        dblNumberC *= 10 ^ intPowerOf10
        '
        strTimeOfDay = TimeOfDay
        Console.WriteLine("Starting time of compare: " & TimeOfDay)
        Console.WriteLine("  ")
        '
        '
        intPower = 0
        dblWholeA = (dblNumberA * (10 ^ intPower)) / _
          (10 ^ 0)
        dblWholeB = (dblNumberB * (10 ^ intPower)) / _
          (10 ^ 0)
        dblWholeC = (dblNumberC * (10 ^ intPower)) / _
          (10 ^ 0)
        '
        strGoodA = "0"
        strGoodB = "0"
        strGoodC = "0"
        intFactor = 0
        While (dblWholeA - Fix(dblWholeA) <> 0) And _
            (dblWholeB - Fix(dblWholeB) <> 0) And _
            (dblWholeC - Fix(dblWholeC) <> 0)
            intPower += 1
            dblWholeA = ((dblNumberA * (10 ^ intPower))) / _
          (10 ^ 0)
            dblWholeB = ((dblNumberB * (10 ^ intPower))) / _
          (10 ^ 0)
            dblWholeC = ((dblNumberC * (10 ^ intPower))) / _
          (10 ^ 0)
        End While
        '
        dblWork1 = dblWholeA
        dblWork2 = dblWholeB
        dblOldNumber1 = dblNumberA
        dblOldNumber2 = dblNumberB
        CompareTwoNumbers(dblWork1, dblWork2, dblWork3, _
        dblOldNumber1, dblOldNumber2)
        dblWork1 = dblWork3
        dblWork2 = dblWholeC
        dblOldNumber2 = dblNumberC
        CompareTwoNumbers(dblWork1, dblWork2, dblWork3, _
        dblOldNumber1, dblOldNumber2)
        Console.WriteLine("For numbers " & _
        Str(dblNumberA) & " " & Str(dblNumberB) & " " & _
        Str(dblNumberC))
        Console.WriteLine(Str(dblOldNumber1) & " is the Max")
        Console.WriteLine("  ")
        Console.WriteLine("Ending time of compare: " & TimeOfDay)
        Console.WriteLine("  ")
    End Sub
    Sub CompareTwoNumbers(ByRef dblWork1, ByRef dblWork2, ByRef dblWork3, _
    ByRef dblOldNumber1, ByVal dblOldNumber2)
        Dim dblSave1 As Double
        Dim dblSave2 As Double
        Dim dblLoopCount As Double
        Dim dblFixNumber1 As Double
        Dim dblFixNumber2 As Double
        dblSave1 = dblWork1
        dblSave2 = dblWork2
        If Double.IsInfinity(1 / (dblSave1 + Abs(dblSave1))) And _
        Not Double.IsInfinity(1 / (dblSave2 + Abs(dblSave2))) Then
            dblWork3 = dblSave2
            dblOldNumber1 = dblOldNumber2
            Exit Sub
        ElseIf Not Double.IsInfinity(1 / (dblSave1 + Abs(dblSave1))) And _
        Double.IsInfinity(1 / (dblSave2 + Abs(dblSave2))) Then
            dblWork3 = dblSave1
            Exit Sub
        ElseIf Double.IsInfinity(1 / (dblSave1 + Abs(dblSave1))) And _
        Double.IsInfinity(1 / (dblSave2 + Abs(dblSave2))) Then
            dblSave1 *= -1
            dblSave2 *= -1
        End If
        dblLoopCount = dblSave1 + dblSave2 + 10
        For Index1 As Double = 0 To dblLoopCount
            If Double.IsInfinity(1 / (dblSave1 + Abs(dblSave1))) Then
                If Double.IsInfinity(1 / (dblWork1 + Abs(dblWork1))) Then
                    dblWork3 = dblWork1
                    Exit Sub
                Else
                    dblWork3 = dblWork2
                    dblOldNumber1 = dblOldNumber2
                    Exit Sub
                End If
            End If
            If Double.IsInfinity(1 / (dblSave2 + Abs(dblSave2))) Then
                If Double.IsInfinity(1 / (dblWork1 + Abs(dblWork1))) Then
                    dblWork3 = dblWork2
                    dblOldNumber1 = dblOldNumber2
                    Exit Sub
                Else
                    dblWork3 = dblWork1
                    Exit Sub
                End If
            End If
            dblFixNumber1 = (Log(dblSave1) / Log(10)) + 1
            dblFixNumber1 = Fix(dblFixNumber1)
            If dblFixNumber1 = 1 Then
                dblSave1 -= 1
            Else
                dblSave1 = Int((dblSave1 + 0.99) / 2)
            End If
            dblFixNumber2 = (Log(dblSave2) / Log(10)) + 1
            dblFixNumber2 = Fix(dblFixNumber2)
            If dblFixNumber2 = 1 Then
                dblSave2 -= 1
            Else
                dblSave2 = Int((dblSave2 + 0.99) / 2)
            End If
        Next
    End Sub
End Module

Edited on April 23, 2004, 2:42 pm
  Posted by Penny on 2004-04-22 18:16:02

Please log in:
Login:
Password:
Remember me:
Sign up! | Forgot password


Search:
Search body:
Forums (0)
Newest Problems
Random Problem
FAQ | About This Site
Site Statistics
New Comments (4)
Unsolved Problems
Top Rated Problems
This month's top
Most Commented On

Chatterbox:
Copyright © 2002 - 2017 by Animus Pactum Consulting. All rights reserved. Privacy Information