Below is an assignment that i did but i didnt do good on it. It is/was never meant to be ran the teacher was just asking for the code to be written based off of her A-D. Can you please let me know what is right and wrong with this.

This is using VBScript

The parts of this ? are all continuations of each other. U may assume
code written for the later portions of the ? uses the results of the previous
parts of the ?.

A. Write a line of code to create an array called aScores that will have 10 values.

Dim aScores(9)

B. Set up a loop that will allow the user to enter 10 values into consecutive
locations in the array aScores.

ReDim aScores(iCount)

For t = 1 to 10 vInput = InputBox("Enter Score: " & cstr(t), "Question: " & cstr(t)) If isNumeric(vInput) Then
aScores(t) = cInt(vInput)
Else
aScores(t) = 0
End If

C. Assume that a variable named ScoreAverage exists and has been assigned the
value of 0 (zero). Set up a loop and any additional statements necessary to
calculate the average of the values in aScores. The average should be assigned to
ScoreAverage.

ScoreAverage = 0

ScoreAverage = ScoreAverage + aScores(t)

Next ScoreAverage = cint(ScoreAverage/10)

D. Assume that a variable called HighCount exists and has been assigned the value of
0 (zero). Set up a loop to count the number of values in the array that exceed the
average. That number should be assigned to HighCount.


HighCount = 0

For t = 1 to iCount ScoreAverage = ScoreAverage + aScores(t)

If aScores(q) > iAvg Then
ScoreAverage = ScoreAverage + 1
End If

Dani AI

Generated

Quick expert review of the posted solution and what to change.

Main issues (as already flagged): off‑by‑one indexing, missing/ unmatched For/Next pairs, unnecessary or incorrectly used ReDim, and several undeclared/typo variables (e.g. iCount, q, iAvg). Since this is VBScript, arrays created for ten slots are indexed 0..9; use LBound/UBound instead of hardcoding 0..9 or 1..10 to avoid boundary mistakes.

Practical fixes for each part:

  • A — keep a ten‑element array (indices 0..9). Prefer an explicit declaration so intent is clear.
  • B — iterate from LBound(...) to UBound(...). For each iteration call InputBox, test the result with IsNumeric (and Trim if needed), convert to a numeric type only when valid, and assign a default (zero) when not. Do not call ReDim unless the program must change the array size; if resizing and preserving existing values is required, use ReDim Preserve and set the new upper bound carefully.
  • C — accumulate a total inside the input loop or in a separate loop, then compute the average after the loop finishes. For a floating average use something like ScoreAverage = total / (UBound(aScores) - LBound(aScores) + 1) so division by zero and integer truncation are avoided.
  • D — loop the same safe bounds and increment HighCount for each element strictly greater than ScoreAverage. Initialize HighCount = 0 before the loop.

Troubleshooting tips: enable Option Explicit and Dim every variable to catch typos, match every For with a Next, and test with boundary cases (all zeros, nonnumeric inputs, canceling the InputBox). If a "Subscript out of range" error appears, the loop bounds or the array declaration are the likely culprits.

Recommended Answers

All 2 Replies

Just a quick critique. By item:
A. Good. Just be aware that this will be indexed as aScores(0) through aScores(9)
B. No need to redim if it isn't changing the array size.
You have a "FOR" with no "NEXT".
You cycle through from 1 to 10. You should be cycling through from 0 to 9 (see above).
C. You have a "NEXT" with no "FOR". Sum the scores inside the "FOR/NEXT" construct, then divide after the loop is done.
D. You have a "FOR" with no "NEXT".
You cycle from 1 to iCount. That variable has never been set to a value.

Nevermind, VBScript. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.