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