This dot product function dose not multiply correctly. Or something... this has me really stumped..

I'm trying to create a dot product function for a larger project. provec0 and provec1 are the two output ints that are supposed to be the product of the four input ints.

The problem --
Console output
1
2
1976708154
32767

The wanted output is 10, 20 not 1976708154, 32767.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
using namespace std;

int vec0[] = {2,4};
int vec1[] = {3,2};
int vec0_s = sizeof(vec0)/sizeof(*vec0); // Number of elements in vec0
int vec1_s = sizeof(vec1)/sizeof(*vec1); // Number of elements in vec1

int* dotM(int vecA[], int vecB[]);

int main () {

	int *provec = dotM(vec0, vec1);
	cout << provec[0] << endl; // Prints *vecO
	cout << provec[1] << endl; // Prints *(vecO+1)

}

int* dotM(int vecA[], int vecB[]){ // Dot Product Function
	int* vecO;
	for(int a=0;a < vec0_s; a++){
		for(int b=0;b < vec1_s; b++){
			*(vecO+a) += vecA[a] * vecB[b];
		}
 	}
	return vecO;
}
Last edited on
The dot product (or inner product, or scalar product) of vectors (2,4) and (3,2) is the single number 14. Could you explain what you are actually trying to do.

And why do you need all those pointers? Just use the normal array elements.
Does this help?

http://en.cppreference.com/w/cpp/algorithm/inner_product


In function 'int* dotM(int*, int*)':
26:9: warning: 'vecO' is used uninitialized in this function [-Wuninitialized]
Last edited on
Topic archived. No new replies allowed.