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!
Problem creating Minimum and Maximum Function
Page 1 of 14 Replies - 145 Views - Last Post: 07 December 2012 - 10:03 PM
Replies To: Problem creating Minimum and Maximum Function
#2
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.
#3
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!
#4
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.
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
#5
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
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote







|