Code:/*if i have*/
array1[2]={1,2};
array2[2]={3,4};
/*how do I do (1*3)+(2*4)=11?
I tried the one below, but sure it won't work*/
for(i=0;i<=2;i++)
{
total+=array1[i]*array2[i];
}
/*thanks for your help*/
Printable View
Code:/*if i have*/
array1[2]={1,2};
array2[2]={3,4};
/*how do I do (1*3)+(2*4)=11?
I tried the one below, but sure it won't work*/
for(i=0;i<=2;i++)
{
total+=array1[i]*array2[i];
}
/*thanks for your help*/
Did you intialize total to 0?
1) Initialise total to zero before the loop.
2) The end criterion needs to be i < 2, not i <= 2.
if it's i<2 then it won't go over i=2? because it's <2? Hmm?
Arrays are 0 indexed meaning they count up from 0..
0,1,2,3,4,5,6...
So the first 2 elements are 0 and 1, so your conditional for your for statement should be i < 2 (While 'i' is less than 2).
thanks, i was talking rubbish.