#include #include void print_vector(const double* vector, int n) { putchar('<'); int i = 0; for ( ; i < n - 1; ++i ) printf("%.1lf, ", vector[i]); printf("%.1lf>", vector[i]); } double dot_product(const double v1[], const double v2[], int n) { double result = 0.0; for ( int i = 0; i < n; ++i ) result += v1[i] * v2[i]; return result; } // Optional: do not declare local variables in body void print_vector2(const double* vector, int n) { putchar('<'); while ( --n > 0 ) printf("%.1lf, ", *vector++); printf("%.1lf>", *vector); } // Example: calculate dot product using pointer arithmetic without [] operator double dot_product2(const double* v1, const double* v2, int n) { double result = 0.0; while ( n-- ) result += *v1++ * *v2++; return result; } // Calculates dot product of two vectors of real numbers given by argument // An even quantity of real numbers are required, the program slices them in // two arrays. If program is called with only one number by argument, nothing // is done. With an odd quantity of numbers, the last one is ignored int main(int argc, char* argv[]) { if ( argc <= 2 ) return 0; double values[argc - 1]; for ( int i = 1; i < argc; ++i ) values[i - 1] = atof( argv[i] ); int n = (argc - 1) / 2; // if n was argc / 2, program could crash print_vector(values, n); printf(" * "); print_vector(values + n, n); printf(" = %.2lf\n", dot_product(values, values + n, n) ); return 0; }