patterncMinor
C inner product function without using array subscripting
Viewed 0 times
withoutarrayfunctionproductusinginnersubscripting
Problem
As part of a question designed to help us understand the relationship between pointers and arrays in C, I've been asked to write an inner product function that doesn't use any array subscripting. Here's what I came up with, but it looks like the kind of complicated 'clever' coding that we've traditionally been told NOT to write.
Any feedback on it, or how it could be done better / more efficiently would be much appreciated.
Edit - Inner product is simply summing the product of the indexes, so a[1,2,3] b[2,3,4] would be 12 + 23 + 3*4
Any feedback on it, or how it could be done better / more efficiently would be much appreciated.
int inner_product(const int *a, const int *b, int n) {
const int *p, *q;
int result = 0;
for(p = a, q = b; p < a + n, q < b + n; p++, q++) {
result += *p * *q;
}
return result;
}Edit - Inner product is simply summing the product of the indexes, so a[1,2,3] b[2,3,4] would be 12 + 23 + 3*4
Solution
I have a few comments on style. (By which I really mean, 100% opinion... :D)
One declaration per line
A lot of people strongly disagree with this, but I find it much easier to read code that has one declaration per line.
Is a lot clearer to me than:
Though really, the only time I've vehemently opposed to it is if both variables are not the same in terms of a pointer or value:
Is a harder to read and more prone to errors than:
n
I would name the
It's fairly apparent that
Possible implementation
I would probably write the function something like this:
Since the two pointers are incremented at the same time, there's no reason to check both.
This should be a tiny bit more efficient (well... probably not once the compiler does it's magic optimisations on the original), and I think it's a bit clearer.
One declaration per line
A lot of people strongly disagree with this, but I find it much easier to read code that has one declaration per line.
const int *p;
const int *q;Is a lot clearer to me than:
const int *p, *q;Though really, the only time I've vehemently opposed to it is if both variables are not the same in terms of a pointer or value:
const int *p, q;Is a harder to read and more prone to errors than:
const int *p;
const int q;n
I would name the
n paramter something more meaningful. I would consider calling it size or num.It's fairly apparent that
n is meant to be the number of elements, but two or three extra keystrokes seems worth the extra clarity.Possible implementation
I would probably write the function something like this:
int inner_product(const int *a, const int *b, int num)
{
const int* end = a + num;
int sum = 0;
while (a != end) {
sum += (*a) * (*b);
++a;
++b;
}
return sum;
}Since the two pointers are incremented at the same time, there's no reason to check both.
This should be a tiny bit more efficient (well... probably not once the compiler does it's magic optimisations on the original), and I think it's a bit clearer.
Code Snippets
const int *p;
const int *q;const int *p, *q;const int *p, q;const int *p;
const int q;int inner_product(const int *a, const int *b, int num)
{
const int* end = a + num;
int sum = 0;
while (a != end) {
sum += (*a) * (*b);
++a;
++b;
}
return sum;
}Context
StackExchange Code Review Q#14348, answer score: 4
Revisions (0)
No revisions yet.