![]() |
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 ]Any advice or help will be appreciated. Thanks. |
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.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. |
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. |
Quote:
:
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. |
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 IntegerAm I missing something? Thanks. |
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.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 integerthen 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)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:
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. |
Hey, on my walk home from work, i thought of this:
:
Dim x, y as integerthen dump highest into you label like this: :
lblscoreHigh.Caption = HighestI really don't know why i didn't think of this earlyer, but what can you do. BTW this code isn't tested. |
Highest and lowest can be done 100% chance of accurancy no matter what the numbers are like this...
:
Lowest = 0 |
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 IntegerThis 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. |
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