Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Visual Basic (http://www.programmingforums.org/forum18.html)
-   -   Single Dimension Arrays (http://www.programmingforums.org/showthread.php?t=1174)

mgoku21 Nov 17th, 2004 4:35 AM

I am trying to figure out how to make a program for that will calculate and display the output using single dimensional arrays. For example, I want to input 5 of my test scores and output them into 4 labels. I am kind of stuck on figuring out how to manipulate an array to do the following things:
1. Output the highest value from array
2. Output the lowest value from array
3. Average of the four HIGHEST scores and output it
4. Last, print the scores back in their original order onto a label

Here's what I think I know so far but I've become kind of lost

:

[ Windows Form designer code ]
  Structure element
    Dim a As Decimal
  End Structure

  Dim a(4) As element

Then for my calculate button:
a(0).a = 0
    a(1).a = 0
    a(2).a = 0
    a(3).a = 0
    a(4).a = 0

    Dim index As Decimal
    Dim high = 0
    Dim low = 9999

    index =


Any advice or help will be appreciated. Thanks.

Pizentios Nov 17th, 2004 9:54 AM

Well, to output the lowest and highest array, use the UBound and LBound functions.

like this:
:

lblYOURLABEL.Caption = myarray(UBound(myarray)) 'for the last array.
lblYOURLABEL2.Caption = myarray(LBound(myarray)) 'for the first array.


however, that all depends on how you input the values into the array. the first should be the index of 0 and the last should be the index of 4.

for the Average, a loop and some if statments should take care of that.

for the last part, a loop will work.

Tim_Myth Nov 17th, 2004 4:31 PM

UBound and LBound determine the subscripts of the array, not the values conatined in the array. Dim an array with 10 elements (Dim MyArray(9)) and fill it with negative numbers. UBound(MyArray) will return 9 whiel LBound(MyArray) will return 0 (Zero).

To find the highest Score in your array:

:

' Dim the different variables we will need.
' If you want Decimals dim them as DECIMALS instead.
' It just depends on how accurate you need to be.
DIM X, Highest, Lowest, Total, Average as INTEGER

' Set Highest to the lowest score possible and Lowest to the highest score possible.
Highest = 0
Lowest = 100

'Loop through all your elements so we can compair them to other values.
For X = 0 TO UBound(a)
 Total = Total + a(x)
 IF a(X) < Lowest THEN Lowest = a(X)
 IF a(X) > Highest THEN Highest = a(X)
 lblScores = lblScore + "Score " + X + ": " + STR(a(X)) + vbNewLine
Next X

' We subtract the lowest value from our total so we can discard it.
' We could also divide the modified totl by UBound(a), but if your array doesn't
' start at 0, you'll get the wrong answer.
Average = (Total - Lowest) / (X - 1)

You now have your highest score (Highest), lowest score (Lowest), your average of the top four scores (Average), and a label containing all the the original scores in order (lblScore). Hope that helps.

Pizentios Nov 17th, 2004 4:41 PM

Quote:

UBound and LBound determine the subscripts of the array, not the values conatined in the array. Dim an array with 10 elements (Dim MyArray(9)) and fill it with negative numbers. UBound(MyArray) will return 9 whiel LBound(MyArray) will return 0 (Zero).

Yeah i know that, and that's what i was going for. that's why i had the code like this:

:

lblmylabel.Caption = myarray(Ubound(myarray))

which would return the highest subscript of the array, since ubound returns the subscript of the array, which is inside the array if you didn't notice.


BTW please use the CODE tags. That's what they're for. and i fixed em for you.

mgoku21 Nov 17th, 2004 5:18 PM

Thanks, I'm kind of stuck now. I have adjusted my code a bit thanks to your help but when I try to run it will not calculate the score. Here's what I got:

:

Dim X As Integer
  Dim Highest As Decimal
  Dim Lowest As Decimal
  Dim Total As Decimal
  Dim Average As Decimal
  Dim a(4) As Integer

'Calculate Button

Highest = 0
    Lowest = 9999

    For X = 0 To UBound(a)
      Total = Total + a(X)
      If a(X) < Lowest Then Lowest = a(X)
      If a(X) > Highest Then Highest = a(X)
      lblScores.Text = lblScores & "Score " & X & ": " & Str(a(X)) & vbNewLine
    Next X

    lblHighest.Text = FormatNumber(a(UBound(a))) 'for the last array.
    lblLowest.Text = FormatNumber(a(LBound(a))) 'for the first array.

    Average = (Total - Lowest) / (X - 1)


Am I missing something? Thanks.

Pizentios Nov 17th, 2004 6:25 PM

ok, there are two ways you could do this, one with a loop with input boxes or the second way with a loop, with out input boxes.

First Method:
:

dim testscores(4) as double 'Since you could have a 98.5 or somthing.
Dim x as integer
For x=0 to 4 Step 1
    testscores(x) = InputBox("Please enter score " & x & ":", "Test Scores", "0")
    if (testscores(x) = "") then
          'if the user clicks cancel InputBox Returns a "" and we can exit the loop.
          Exit For
    end if
Next x


Then do you calulations with the testscores() array.

Second Method:
You'd need to name all your textboxes like: textbox and make a control array...and then use the onclick event handler for a button to run this code.
:

Dim x as integer
Dim testscores(4) as double
For x=0 to 4 Step 1
      testscores(x) = textbox(x).value
Next x


then go on to your calulations. Hope this helps. and BTW this code hasn't been tested. But it should give you the idea, even if it doesn't work 100%.

As far as finding out the highest and lowest score you'd want to do that after the user has inputed all values. And you might want to do some checking to make sure the user doesn't enter any text as a score, otherwise things will blow up.

On a side note the code that Tim_Myth posted for checking for the lowest and highest values is flawed since if the user enters 50, 60, 30, 40, 90 as values in that order, your lowest and highest values will never be set. Even if the user enters 30,40,50,60,70 as inputs high and low values won't be set since:
:

IF a(X) < Lowest THEN Lowest = a(X)

a(X) will always be larger than the value that Lowest holds. Same thing for the line:
:

IF a(X) > Highest THEN Highest = a(X)
since a(x) will always be lower, since you can't have a grade above 100% or Below 0%

here's how i'd do it using the same vars as above Also you'd have to make testscores() a global varible, there's probally a way to pass it strait to the function, but i am to lazy to google for it right now:
:

Public Function HighestV() as Double
    Dim a,b,c,d,e as boolean
    Dim Highest As Double
    if (testscores(0) > testscores(1) AND testscores(0) > testscores(2) AND testscores(0) > testscores(3) AND testscores(4)) then
            '0 is the highest out of them all
            a = true
    else
            'it isn't the highest number.
            a = false 
    end if

    if (testscores(1) > testscores(0) AND testscores(1) > testscores(2) AND testscores(1) > testscores(3) AND testscores(1) > testscores(4)) then
            b = true
            'it's the highscore.
    else
            b = false
    end if

    if (testscores(2) > testscores(0) AND testscores(2) > testscores(1) AND testscores(2) > testscores(3) AND testscores(2) > testscores(4)) then
            c = true
            'it's the high score.
    else
            c = false
    end if

    if (testscores(3) > testscores(0) AND testscores(3) > testscores(1) AND testscores(3) > testscores(2) AND testscores(3) > testscores(4)) then
            d = true 'you get the idea.
    else
            d = false
    end if

    if (testscores(4) > testscores(0) AND testscores(4) > testscores(1) AND    testscores(4) > testscores(2) AND testscores(4) > testscores(3)) then
            e = true
    else
            e = false
    end if

    if (a = true) then
            Highest = testscores(0)
    elseif (b = true) then
            Highest = testscores(1)
    elseif (c = true) then
            Highest = testscores(2)
    elseif (d = true) then
          Highest = testscores(3)
    elseif (e = true) then
          Highest = testscores(4)
    end if

  return Highest
End Function

then to call this function from the buttons onclick event you'd have:
:

Highest = HighestV()

Then you'd just copy the above function and change the > to a < for the lowest value and re-name the function to LowestV or whatever you wanted. The code for checking for the highest value (the code just above) is really ugly and i know there is a better and more cleaner way to do it, but i don't have the time right now to think about it. I'll think some tongiht and then post if i come up with somthing better. Well good luck and hope you can make sense of my blabering on, since this post is huge.

Pizentios Nov 17th, 2004 7:07 PM

Hey, on my walk home from work, i thought of this:

:

Dim x, y as integer
Dim Highest as Double
For x=0 to 4 Step 1
    if (y = x) then
          y = y + 1
    end if
    if (testscores(x) > testscores(y)) then
          Highest = testscores(x)
    end
Next x


then dump highest into you label like this:

:

lblscoreHigh.Caption = Highest


I really don't know why i didn't think of this earlyer, but what can you do. BTW this code isn't tested.

tempest Nov 17th, 2004 7:47 PM

Highest and lowest can be done 100% chance of accurancy no matter what the numbers are like this...

:

Lowest = 0
Highest = 0

For X = 0 TO UBound(a)
  Total = Total + a(x)
  IF (a(X) < Lowest or X = 0) THEN Lowest = a(X)
  IF (a(X) > Highest or X = 0) THEN Highest = a(X)
  lblScores = lblScore + "Score " + X + ": " + STR(a(X)) + vbNewLine
Next X


mgoku21 Nov 17th, 2004 8:07 PM

I am having trouble trying to calculate the scores by using 5 text boxes and then displaying into 4 outputs. Is it really mandatory to use a control array? I think my problem is the way my a(0), a(1), etc. are declared...

:

Dim X As Integer
    Dim Highest As Decimal
    Dim Lowest As Decimal
    Dim Total As Decimal
    Dim Average As Decimal
    Dim a(4) As Decimal

    Highest = 0
    Lowest = 9999

    a(0) = CDec(txtScore1.Text)
    a(1) = CDec(txtScore2.Text)
    a(2) = CDec(txtScore3.Text)
    a(3) = CDec(txtScore4.Text)
    a(4) = CDec(txtScore5.Text)

    For X = 0 To UBound(a)
      Total = Total + a(X)
      If a(X) < Lowest Then Lowest = a(X)
      If a(X) > Highest Then Highest = a(X)
      lblScore.Text = lblScore.Text & "Score: " & Str(a(X)) & vbNewLine
    Next X

    lblHighest.Text = FormatNumber(a(UBound(a))) 'for the last array.
    lblLowest.Text = FormatNumber(a(LBound(a))) 'for the first array.

    Average = (Total - Lowest) / (X - 1)


This only works when you input numbers for the Xs in that a(X) = CDec(txtScore.Text). And when you do that, the first and last numbers are always lowest and highest respectively. What can I do to fix this without drastically changing the code? Thanks.

Pizentios Nov 17th, 2004 8:51 PM

Try my second High low deal there, maybe it'll work.


All times are GMT -5. The time now is 1:10 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC