2 Replies - 725 Views - Last Post: 04 February 2012 - 03:45 PM Rate Topic: -----

Topic Sponsor:

#1 hckyplyr606  Icon User is offline

  • New D.I.C Head

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

Trouble coming up with logic to find midpoint

Posted 01 February 2012 - 12:45 PM

Public Class frmInvoiceTotal

    Dim numberOfInvoices As Integer
    Dim totalOfInvoices As Decimal
    Dim invoiceAverage As Decimal
    Dim largestInvoice As Decimal
    Dim smallestInvoice As Decimal
    Dim midPoint As Decimal
    Dim newMin As Decimal
    Dim newMax As Decimal
    Dim currentMax As Decimal = 0
    Dim currentMin As Decimal = 9999999

    Private Sub btnCalculate_Click(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles btnCalculate.Click

        Dim subtotal As Decimal = CDec(txtSubtotal.Text)
        Dim discountPercent As Decimal = 0.25D
        Dim discountAmount As Decimal = Math.Round(subtotal * discountPercent, 2)
        Dim invoiceTotal As Decimal = subtotal - discountAmount

        txtSubtotal.Text = FormatCurrency(subtotal)
        txtDiscountPercent.Text = FormatPercent(discountPercent, 1)
        txtDiscountAmount.Text = FormatCurrency(discountAmount)
        txtTotal.Text = FormatCurrency(invoiceTotal)

        numberOfInvoices += 1
        totalOfInvoices += invoiceTotal
        invoiceAverage = totalOfInvoices / numberOfInvoices

        txtNumberOfInvoices.Text = numberOfInvoices.ToString
        txtTotalOfInvoices.Text = FormatCurrency(totalOfInvoices)
        txtInvoiceAverage.Text = FormatCurrency(invoiceAverage)

        newMin = Math.Min(currentMin, subtotal)
        txtSmallestInvoice.Text = FormatCurrency(newMin)
        currentMin = newMin

        newMax = Math.Max(currentMax, subtotal)
        txtLargestInvoice.Text = FormatCurrency(newMax)
        currentMax = newMax

        txtSubtotal.Select()
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object,
            ByVal e As System.EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub frmInvoiceTotal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnClearTotals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearTotals.Click

        numberOfInvoices = 0
        totalOfInvoices = 0
        invoiceAverage = 0
        newMin = 0
        newMax = 0

        txtNumberOfInvoices.Text = ""
        txtTotalOfInvoices.Text = ""
        txtInvoiceAverage.Text = ""
        txtLargestInvoice.Text = ""
        txtSmallestInvoice.Text = ""
        txtMidPoint.Text = ""


    End Sub

    Private Sub txtLargestInvoice_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLargestInvoice.TextChanged

    End Sub
End Class



This program takes invoices and discounts each one and gives the final price for each invoice. Then keeps track of how many invoices were entered as well as the average and total of each of the invoices. This is what I have so far and everything works fine except for the clear totals button which I can't get to reset the values I have to display the largest and smallest invoices. I can get them to clear out but not reset.

I need to add one more button to determine the midpoint between the largest invoice and the smallest invoice. I can't figure out the logic for this button. I have already been programming in java for about a year so I know about arrays and other data structures that I could potentially use here, but for this class in VB, we haven't gotten there yet and our professor does not want us to use any types of advanced data structures or loops.

Any help would be appreciated..I'm not asking for help with the actual code...just the logic behind it for right now.

Thanks,
Nick

Is This A Good Question/Topic? 0
  • +

Replies To: Trouble coming up with logic to find midpoint

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 211
  • View blog
  • Posts: 1,249
  • Joined: 26-March 09

Re: Trouble coming up with logic to find midpoint

Posted 04 February 2012 - 02:36 PM

Not sure if this is what you're asking for, but if you have a max and a min value and you need the mid point, then something like

midpoint = ((max-min)/2) + min

so max =20, min = 10

midpoint = ((20-10)/2) + 10 = (10 / 2) + 10 = 5 + 10 = 15

Not sure if that helps at all...
Was This Post Helpful? 0
  • +
  • -

#3 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 361
  • View blog
  • Posts: 1,020
  • Joined: 30-January 09

Re: Trouble coming up with logic to find midpoint

Posted 04 February 2012 - 03:45 PM

You can rationalise that down to:
midpoint = (max + min) / 2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1