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"

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!

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

Processing your response...

Discuss This Question: &nbsp

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

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

Processing your reply...

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

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

Processing your reply...

Ask a Question

Free Guide: Managing storage for virtual environments

Complete a brief survey to get a complimentary 70-page whitepaper featuring the best methods and solutions for your virtual environment, as well as hypervisor-specific management advice from TechTarget experts. Don’t miss out on this exclusive content!

Share this item with your network:

To follow this tag...

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