Arrays (Task)

Download Report

Transcript Arrays (Task)

Lab 4:
Array variables, input from file (Finding Max. element, vectors)
1. Type in the given numbers using Notepad and save them in the numbers.txt file : 2 5 -3 15 7 -6 12 1
Define an array, where in the elements a(1), a(2), .. you can input at most
10 Integers, then open for Input the numbers.txt fájlt, and read the
Sub MaxNumber()
numbers from the file to the array a.
Dim a%(10), n%, k%, kmax%, max%
Open "numbers.txt" For Input As #1
You don’t know usually the number of data in
k=0
a txt file, so use the EOF function, whose
Do While Not EOF(1)
value will be True at the EndOf File marker.
k=k+1
After finishing data input, continue your program
Input #1, a(k)
by finding the index (kmax) of the (first) biggest
Cells(k, 1) = a(k)
element of the array.
Loop
Close #1
As output the program should write the value of kmax into the row of the (first)
biggest element (column C).
Open "vectors.txt" For Input As #1
2. Create the vectors.txt file as you see below.
Input #1, n, com
Write
a
program,
that
reads
the
first
data
of
the
file
Cells(1, 1) = com
3
vectors.txt
into
n
(an
Integer
type
variable,
the
dimension
For k = 1 To n
Vector a:
of
the
two
vectors)
into
com
(a
String
type
variable)
the
Input #1, a(k): Cells(1, k + 1) = a(k)
1 -2 2
text
of
the
second
and
fourth
row
of
the
file,
thereafter
Next
k
Vector b:
the coordinates of the two vectors into the Double type

3 0 4
arrays a and b.
Close #1
Function Scalar(n%, x#(), y#()) As Double
Create the Function given here right for evaluation of the dot product of
Dim sum#, j%
two vectors, then call the Function in the main program three times:
sum = 0
For j = 1 To n
Scalar(n,a,b) calling gives the dot product
sum = sum + x(j) * y(j)
of the vectors a and b ,
Next j
Sqr(Scalar(n,a,a)) és Sqr(Scalar(n,b,b))
Scalar = sum
callings give the lenght of the vectors a
End Function
and b .
The cosine of the angle between the two vectors is given by the
dot product, divided by the product of the two lengths.