Pointer Dot products

PHOTO EMBED

Tue Jun 11 2024 06:34:32 GMT+0000 (Coordinated Universal Time)

Saved by @meanaspotato #c #function

#include <stdio.h>

float dot_product(float v1[3], float v2[3]) //add this
{
    return (v1[0] * v2[0]) + (v1[1] * v2[1]) + (v1[2] * v2[2]);
}

int main(void)
{
	float vec_a[3];
	float vec_b[3];

	vec_a[0] = 1.5f;
	vec_a[1] = 2.5f;
	vec_a[2] = 3.5f;
	vec_b[0] = 4.0f;
	vec_b[1] = 5.0f;
	vec_b[2] = 6.0f;

	printf("%f\n", dot_product(vec_a, vec_b));

	return 0;
}
content_copyCOPY

The objective of this exercise is to help students grasp array manipulation in C, specifically, the concept of computing the "dot product" of two vectors. The exercise requires the declaration of two 3-element arrays and the implementation of a function that calculates their dot product. Requirements Array Declaration Declare two arrays of floats, each with a dimension of 3. The first array should be named vec_a and should be initialized with the elements [1.5f, 2.5f, 3.5f]. The second array should be named vec_b and should be initialized with the elements [4.0f, 5.0f, 6.0f]. Function Declaration and Definition Declare and define a function named dot_product that: Takes two 3-element arrays of type float as arguments. Returns the dot product of these arrays as a float. Should not produce any side effects. The formula for the dot product is given by dot product=(x1×x2)+(y1×y2)+(z1×z2)dot product=(x1×x2)+(y1×y2)+(z1×z2). Main Function In the main() function: Declare and initialize the two float arrays, vec_a and vec_b, as specified. Call the dot_product function with vec_a and vec_b as arguments. Print the result returned from the dot_product function in the format 39.500000.