Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with Array in fortran 95/2003

Status
Not open for further replies.

cbmurphy

Programmer
Oct 12, 2008
11
US
i am supposed to write a function that computes the dot product of 2 vectors where the dot product equals the sum of Xi and Yi when i goes from 0 to n-1. then i have to write a test program to test it. we had one day of instruction on arrays and the assignment is due the next day (tomorrow) and i have no clue where to even start. if anyone could just point me in the right direction, even without code as to what i need to include in the program i would appreciate it. thanks
 
here is my code so far, please tell me what i am doing wrong:
[code/]
program testVectors
implicit none

integer:: n
integer:: i = 0
real:: innerProd
real, dimension(n):: array1, array2
write(*,*)"Enter number of elements to each array: "
read(*,*)n
do i = 1, n
if(i == 1) then
write(*,*)"Please enter the first value of each array (array 1 and array 2): "
read(*,*)array1(1), array2(1)
else
write(*,*)"Please enter the next value of each array (array 1 and array 2): "
read(*,*)array1(i), array2(i)
end if
end do
write(*,*)"The dot product of the two vectors is: ", innerProd(array1, array2, n)
end program

real function innerProd(array1, array2, n)
implicit none
integer, intent(in):: n
real, intent(in), dimension(n):: array1, array2
integer:: i = 0
real:: newSum = 0.
do i = 1, n
newSum = array1(i) * array2(i)
innerProd = innerProd + newSum
end do
end function
[/code]

my error messages include a huge list weird words that all start with a and b so i wont post it, i think i am on the right track, just something is not quite right. appreciate any insight
 
now i have modified it to read a .dat file in the form:

# numberOfArrayValues
array1(1) array2(1)
array1(2) array2(2)
.
.
.
.
.
array1(n) array2(n)

and the code is as follows:
[code/]
program testVectors
implicit none

integer:: n = 0, i = 0
character:: dummy
read(*,*)dummy, n
real, dimension(n):: array1, array2
do i = 1, n
read(*,*)array1(i), array2(i)
end do
end program

real function innerProd(array1, array2, n)
implicit none
integer, intent(in):: n
real, intent(in), dimension(n):: array1, array2
integer:: i = 0
real:: newSum = 0.
do i = 1, n
newSum = array1(i) * array2(i)
innerProd = innerProd + newSum
end do
end function
Code:
i am still getting an error message when compiling saying:

 In file testVectors.f95:16

 real, dimension(n):: array1, array2
                                   1
 Error: Unexpected data declaration statement at (1)
 In file testVectors.f95:18

  read(*,*)array1(i), array2(i)
                1
 Error: Syntax error in READ statement at (1)

any comment on why this shouldnt work?
 
sorry, here:
[code/]
program testVectors
implicit none

integer:: n = 0, i = 0
character:: dummy
read(*,*)dummy, n
real, dimension(n):: array1, array2
do i = 1, n
read(*,*)array1(i), array2(i)
end do
end program

real function innerProd(array1, array2, n)
implicit none
integer, intent(in):: n
real, intent(in), dimension(n):: array1, array2
integer:: i = 0
real:: newSum = 0.
do i = 1, n
newSum = array1(i) * array2(i)
innerProd = innerProd + newSum
end do
end function
[/code]
 
please delete this thread, i got it working!!!
 
You could check it against dot_product which is a built in function in fortran for doing dot products.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top