Help with VBScripting – Beginner

Tags:
VBScript
Visual Basic
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"

Answer Wiki

Thanks. We'll let you know when a new response is added.

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 <b>see</b> 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 <b><i>WScript.Echo avgtemp</i></b> 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 <b>7.75 Degrees</b>. 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!

Discuss This Question:  

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following