2.11BSD/share/learn/C/L42.1a
#print
Write a function
inprod(a,b,n)
that computes the inner product of two integer vectors
a and b which are n items long. Name the file "inprod.c"
and compile and test it; then type ready.
You may assume that the result and all intermediate
values fit in a 16-bit integer, not usually a safe assumption.
#once #create tzaqc.c
main()
{
int x[100], y[100];
int k;
for(k=0; k<100; k++)
{
x[k] = k%10;
y[k] = (k*k)%3;
}
if (inprod(x,y,100) != xprod(x,y,100)) return(1);
return(0);
}
xprod(x,y,n)
int *x, *y;
{
int k, sum;
for(sum=k=0; k<n; k++)
sum=+ *x++ * *y++;
return(sum);
}
#user
cc tzaqc.c inprod.o
a.out
#succeed
/* one way */
inprod(a, b, n)
int *a, *b;
{
int s;
s = 0;
while (n--)
s += *a++ * *b++;
/* none of the spaces in the line above are necessary but
would you really want to read
s+=*a++**b++;
and try to parse it? Even clearer than what I have,
but slower, would be
for(i=0; i<n; i++)
s += a[i]*b[i];
*/
return(s);
}
#log
#next
43.1a 10