Question

  Asked: Nov 10 2007   6:43 PM GMT
  Asked by: Chottenstine


Help with VBScripting - Beginner


Visual Basic, VBScript

Attached is the script I'm working on for an assignment. I cannot get the count to work for the number of temps equal to the avgtemp.

for each member in temps
if member = avgtemp then count=count + 1
next

What am I doing wrong?
--------------------------------------------------------
Option Explicit
Dim temps(), numtemps, j, mess, avgtemp, member, count, abvfreeze, belfreeze, str
numtemps = InputBox("How many temps will you be working with?")
ReDim temps(numtemps-1)
For j = 0 To numtemps-1
temps(j) = inputbox("enter a valid temperature")
next
for j = 0 to numtemps-1
avgtemp = avgtemp+temps(j)/numtemps
next
for each member in temps
if member > 32 then abvfreeze=abvfreeze + 1
next
for each member in temps
if member < 32 then belfreeze=belfreeze + 1
next
for each member in temps
if member = avgtemp then count=count + 1
next
mess = "The temperatures are: "
for each member in temps
mess = mess & member & ","
next
mess = left(mess,len(mess)-1) & vbcrlf & "The average temperature is: " & avgtemp & vbcrlf
mess = mess & "The number of temperatures above freezing is " & abvfreeze & vbcrlf
mess = mess & "The number of temperatures below freezing is " & belfreeze & vbcrlf
mess = mess & "The number of temperatures equal to the average is " & count
msgbox mess,,"The Amazing Results"

Subscribe to Alerts! Get questions and answers delivered to your Inbox.


E-mail me updates on this question



   SUBSCRIBE

hidden modal window

Answer Wiki (Improve, edit or add to this answer)


 RATE THIS ANSWER
0
Click to Vote:
  •   0
  •  0



Looks like you never got an answer... I almost didn't answer, but thought maybe someone else might need this answer some time. Better late than never I guess!

I don't see anything you're doing wrong in the script, but I did clean it up a bit. You had the for/next loops that were identicle, so I rolled them into one for/next loop.


Here is what I came up with:
_____________________ snip _______________________________
Option Explicit
Dim temps(), numtemps, j, mess, avgtemp, member, count, abvfreeze, belfreeze, str
numtemps = InputBox("How many temps will you be working with?")
ReDim temps(numtemps-1)
For j = 0 To numtemps-1
temps(j) = inputbox("enter a valid temperature")
next
for j = 0 to numtemps-1
avgtemp = avgtemp+temps(j)/numtemps
Next
WScript.Echo avgtemp

for each member in temps
if member > 32 then abvfreeze=abvfreeze + 1
If member < 32 then belfreeze=belfreeze + 1
if member = avgtemp then count=count + 1
Next

mess = "The temperatures are: "
For each member in temps
mess = mess & member & ","
Next
mess = left(mess,len(mess)-1) & vbcrlf & "The average temperature is: " & avgtemp & vbcrlf
mess = mess & "The number of temperatures above freezing is " & abvfreeze & vbcrlf
mess = mess & "The number of temperatures below freezing is " & belfreeze & vbcrlf
mess = mess & "The number of temperatures equal to the average is " & count
msgbox mess,,"The Amazing Results"
_____________________ snip _______________________________

You'll notice I put a line in like this WScript.Echo avgtemp this will echo at the command line (with cscript) and msgbox in windows (wscript) and it puts out the average temp as you have calculated it. I'm not certain the calculation is accurate over a long series of varied numbers, but in the few I tested it worked. You are calculating the avgtemp one temp at a time but dividing by the numberoftemps multiple times. It could be right... I just looked odd and I didn't regression test it enough to see if it figured incorrectly at all.

Typically, the average of 3 numbers X Y and Z is (X+Y+Z)/3 what you've coded is (0+X)/3+((0+X)/3+Y/3)+(((0+X)/3+((0+X)/3+Y/3))+Z/3)--- notice the division multiple times? Like I said, It could be 6 of one and a half dozen of another--- it just didn't seem clear to me.

The one thing I can say for sure is that you are comparing the entries of temperatures you made to the average of those temps. There will be very few cases where that works out! here is why...

for 3 entries of 10 degrees and one entry of 1 degree the average is (10+10+10+1)/4, mathmatically that works out to 7.75 Degrees. None of the entries will match that average.

So.... the good news is your script probably worked perfectly if the math calculation for the average was correct!
  • AddThis Social Bookmark Button

Browse more Questions and Answers on Development.

Looking for relevant Development Whitepapers? Visit the SearchWinDevelopment.com Research Library.


Discuss This Answer


You must be logged-in to discuss a question. Log-in/Register