Vous êtes sur la page 1sur 2

Imports System.

Math
Module Module1
Sub Main()

'*********************************************************
'GENG1002 Midterm Test
'C3206008
'Hussain, Faizan
'*********************************************************

'Variables
Dim n As Integer 'Number of points
Dim header As String 'header
Const nmax = 56 'Maximum size of array
Dim x(nmax - 1) As Double 'x coordinate
Dim y(nmax - 1) As Double 'y coordinate
Dim filename As String 'Input file name
Dim ISRS As Double
Dim i As Integer 'For loop index

'Ask user to enter name of data file


Console.WriteLine("Please enter name of file:")
filename = Console.ReadLine

'Open input file spcified by user


FileOpen(20, filename, OpenMode.Input, OpenAccess.Read)
Input(20, header) 'Reads header
Input(20, n) 'Reads number of points

'Reads the coordinates of n points


For i = 0 To n - 1
Input(20, x(i)) 'Reads x coordinate
Input(20, y(i)) 'Reads y coordinate
'Checks if any x coordinate 0 if true then shuts down
If x(i) = 0 Then
Console.WriteLine("x = 0 therefore invalid")
Console.ReadLine()
Exit Sub
End If
Next
FileClose(20) 'Closes the file

'Call function Find_ISRS to find ISRS


ISRS = Find_ISRS(n, x, y)

'Opens output file


FileOpen(20, "results.dat", OpenMode.Output, OpenAccess.Write)

'Writes the header and the ISRS to an output file named "results.dat
WriteLine(20, header)
WriteLine(20, "The value of ISRS=")
WriteLine(20, ISRS)

'Writes the header and the ISRS to the screen


Console.WriteLine(header)
Console.WriteLine("The value of ISRS=")
Console.WriteLine(ISRS)
FileClose(20) 'Close the file
Console.ReadKey()

End Sub
Function Find_ISRS(ByVal n As Integer, ByVal x() As Double, ByVal y() As Double)
'Inputs n, x, y
'Outputs ISRS

Dim topsum As Double = 0 'Top half of formula


Dim bottomsum As Double = 0 'Bottom half of formula

For i = 1 To n - 1
topsum = topsum + (x(i) - x(i - 1)) ^ 2
Next
For i = 0 To n - 1
bottomsum = bottomsum + Sqrt(y(i) / x(i))
Next
Return Sqrt(topsum) / bottomsum ' Uses topsum and bottomsum to find ISRS
End Function

End Module

Console Screen

Output screen

Vous aimerez peut-être aussi