int Dot_Product( int A[], int B[], int size )
{
int i = 0;
int P[size];
while ( i <= size ) {
P[i] = A[i] * B[i];
}
return P;
}
Asked
Active
Viewed 68 times
-3

MAV
- 7,260
- 4
- 30
- 47

user3019432
- 21
- 1
- 3
-
6infinite loop????? I think I just went cross-eyed. You need a way out of your loop. – PaulG Nov 21 '13 at 21:38
-
This link should help you http://bit.ly/1fZBkPk – theAlias Nov 21 '13 at 21:39
-
1@PaulG You're not the only one :) – Gabriel L. Nov 21 '13 at 21:41
-
Also, it should be < not <=, the latter will cause you to go out of bounds. From what I remember of C (which I wouldn't recommend as a starting language anyways) indexes are 0 based, so 0 to size - 1 – PaulG Nov 21 '13 at 21:46
2 Answers
1
You can't return arrays in C. You can return pointers to arrays but then you have to worry about memory management. So often, people pass arrays INTO the function to be used for assignment.
However if you're really just trying to take the dot product, you shouldn't end up with an array, you should end up with a single value, so this should suffice:
int Dot_Product( int A[], int B[], int size )
{
int i = 0;
int product = 0;
while ( i < size ){
product += A[i] * B[i];
++i;
}
return product;
}

Taylor Brandstetter
- 3,523
- 15
- 24
0
C doesn't allow you to return arrays, you can pass a pointer to a function and with that pointer change the values of the array you declare in the main function.
void Dot_Product(int A[], int B[], int size, int *product) {
int i;
for(i=0; i<size; i++)
product[i]=A[i]*B[i];
}

JordanYankovich
- 26
- 2