Instructions
Create a VBScript script (w2_firstname_lastname.vbs) that takes 3 parameters (numbers)
1) Print the product of three numbers
2) Print the sum of the three numbers
3) Print the average of the three numbers
Optionally, you can save the results into a file \”Results.txt\” using the redirection operator \”>\” see sample run below.
4) Make sure to include comment block (flowerbox) in your code.
5) Sample run:-
C:\\entd261>cscript.exe w2_sammy_abaza.vbs 2 3 5 > results.txt
Submit your week 2 work in w2_firstname_lastname.txt (Please save the file as a text file and upload the text file here for final review.)
Requirements
Points
Comment block (flowerbox) with Instructions on how to run the code with examples.
20
Code documentation and comments.
10
Assignment code including creating command line parameter
70
TOTAL POINTS
100
Next Question
1. There are various selection structures that can be used in programming. What are the different relational operators used in selection structures? Provide an example of an if-then-else statement that can be used in a script that performs a system administration task.
ANSWER
Here\’s a VBScript code that takes three parameters (numbers), calculates their product, sum, and average, and optionally saves the results to a file \”Results.txt\” using the redirection operator:
“`vbscript
\’**********************************************************************
\’ Script: w2_firstname_lastname.vbs
\’ Description: Calculate product, sum, and average of three numbers.
\’ Usage: cscript.exe w2_firstname_lastname.vbs
\’ Example: cscript.exe w2_firstname_lastname.vbs 2 3 5 > Results.txt
\’**********************************************************************
\’ Check the number of command-line arguments
If WScript.Arguments.Count < 3 Then
WScript.Echo \"Usage: cscript.exe w2_firstname_lastname.vbs
WScript.Quit
End If
\’ Get the command-line arguments as numbers
num1 = WScript.Arguments(0)
num2 = WScript.Arguments(1)
num3 = WScript.Arguments(2)
\’ Calculate product, sum, and average
product = num1 * num2 * num3
sum = num1 + num2 + num3
average = (num1 + num2 + num3) / 3
\’ Display the results
WScript.Echo \”Product of the three numbers: \” & product
WScript.Echo \”Sum of the three numbers: \” & sum
WScript.Echo \”Average of the three numbers: \” & average
“`
Save this script as \”w2_firstname_lastname.vbs\” and run it as shown in your sample run example:
“`
cscript.exe w2_firstname_lastname.vbs 2 3 5 > Results.txt
“`
This script will calculate the product, sum, and average of the three numbers and print the results. If you use the redirection operator (\”>\”), it will also save the results to a file named \”Results.txt.\”