4 Replies - 780 Views - Last Post: 07 December 2012 - 10:03 PM Rate Topic: -----

#1 Prino33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-November 12

Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 08:45 PM

I'm trying to figure a way to write a Function or If statements that picks the maximum value out of 4 numbers. I also have to do one for the minimum of 4 numbers. I found a way in which I could do it with comparing 3 numbers but cant figure out how to do it with 4.

Any help is greatly appreiciated!
Is This A Good Question/Topic? 0
  • +

Replies To: Problem creating Minimum and Maximum Function

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 9574
  • View blog
  • Posts: 36,262
  • Joined: 12-June 08

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 08:47 PM

Why not use a for loop and a two integer variables? Go through each entry in the array and compare the value if it is bigger than the current biggest or smaller than the current smallest.. if either - replace accordingly.
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 08:58 PM

Use a List(Of Integer) then you have .Min and .Max on that object and your done!
Was This Post Helpful? 0
  • +
  • -

#4 Prino33  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-November 12

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 09:45 PM

Both sound great. I'm just having trouble figuring out how to implment either method into my code. Could either of you provide an example about how one could be done?

Thanks!


P.S. I addded the code I'm try to refactor below. Our teacher prdovided us with an example we just have to rewrite to work with our problem. Anyways, I dont know if you can decipher the code but im trying to refactor the code to work with a 4 value min/max equation.

Option Strict On
Option Explicit On
Imports System.Convert
Imports Microsoft.VisualBasic.ControlChars

Public Class frmStats

    Private StatsData() As frmMain.sQuarterlySales = frmMain.SalesmansQuarterlySales


    Private Sub btnShowAverage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowAverage.Click
        Dim pdecAverageSale As Decimal
        Dim pdecTotalSales As Decimal

        For pintCount As Integer = 0 To StatsData.GetUpperBound(0)
            pdecTotalSales += StatsData(pintCount).Quarter1Sales + StatsData(pintCount).Quarter2Sales + StatsData(pintCount).Quarter3Sales + StatsData(pintCount).Quarter4Sales
        Next

        pdecAverageSale = pdecTotalSales / 4

        lblDisplayReport.Text = "The average sales amount is: " & pdecAverageSale.ToString("c")
    End Sub



    Private Sub btnShowMinimumPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowMinimumPurchase.Click
        Dim pdecMinPurchase As Decimal
        Dim pdecPurchase As Decimal
        Dim pintCount As Integer
        Dim pintFound As Integer = 0

        pdecMinPurchase = StatsData(0).prodAmount * StatsData(0).prodPrice

        For pintCount = 1 To StatsData.GetUpperBound(0)
            pdecPurchase = StatsData(pintCount).prodAmount * StatsData(pintCount).prodPrice
            If pdecPurchase < pdecMinPurchase Then
                pdecMinPurchase = pdecPurchase
                pintFound = pintCount
            End If
        Next

        lblDisplayReport.Text = "The minimum purchase amount is: " & pdecMinPurchase.ToString("c") & " at location " & pintFound.ToString
    End Sub

    Private Sub btnShowMaxPurchase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowMaxPurchase.Click
        Dim pdecMaxPurchase As Decimal
        Dim pdecPurchase As Decimal
        Dim pintCount As Integer
        Dim pintFound As Integer = 0

        pdecMaxPurchase = 0

        For pintCount = 1 To StatsData.GetUpperBound(0)
            pdecPurchase = StatsData(pintCount).prodAmount * StatsData(pintCount).prodPrice
            If pdecPurchase > pdecMaxPurchase Then
                pdecMaxPurchase = pdecPurchase
                pintFound = pintCount
            End If
        Next

        lblDisplayReport.Text = "The minimum purchase amount is: " & pdecMaxPurchase.ToString("c") & " at location " & pintFound.ToString
    End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1067
  • View blog
  • Posts: 4,174
  • Joined: 02-July 08

Re: Problem creating Minimum and Maximum Function

Posted 07 December 2012 - 10:03 PM

Dim numbers As New List(Of Decimal)
numbers.Add(10.1)
numbers.Add(1.1)
numbers.Add(2.9)
Dim min As Decimal = numbers.Min
Dim max As Decimal = numbers.Max
Dim ave As Decimal = numbers.Ave

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1