Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Please write this in language of C. Do not need to be extremely complicated 1. [

ID: 3593644 • Letter: P

Question

Please write this in language of C. Do not need to be extremely complicated

1. [81 Consider representing vectors with n floating point components using arrays. Develop a library for vector operations, that includes the following functions * A function print_vector() to print the vector components, on a single line, separated by commas The function prototype has to be * void print vector(double array[], int size); Parameter size represents the size of parameter array and the vector dimension. *A function add_vectors() to add two vectors of the same size, with prototype: » void add_vectors( double vector1l, double vector2[], double vector3[], int n); vector3 should store the sum of vector1 and vector2. You may assume that all three arrays have the size equal to n, which equals the vector dimension. (In other words, assume that the calling function ensures that the arrays passed in satisfy this condition. A function scalar_prod() that returns the scalar product of two vectors of the same dimension. You may assume that the passed in arrays have the same size A function norm2(), which returns the L2 norm of a vector. The L2 norm is defined as the square root of the scalar product of the vector with itself. Function norm2() should call function scalar_prod() * * Write a program to test this library. You are allowed to use math library functions Attention: When you pass an array (which is not a string) to a function, you also need to pass to the function the size of the arrav Note: Consider vectors x=(x(0), x(1), , x(n-1)) and y=(y(0), y(1), , y(n-1)) The sum of x and y is the vector z-(z(0),z(1), ..., z(n-1)), where z(i)-x(i)+y(i) for every 0

Explanation / Answer

//Let's start implementing the functions one by one:

/*1. We need to print the vector components on a single line which are separated by commas.

We are given with array and the size of the array.

Let's implement this

*/

void print_vector(double array[], int size) {

//Write a simple for loop which would iterate over the given array for "size" times.

for (int i = 0; i < size; ++i)

{

printf("%d", array[i]);

//This would print the vector components but we need a comma between every component.

//Since we would print a comma, but not after the last element so we are checking for the last element and not priiting the comma for that

if (i != size - 1)

{

printf("%s", ",");

}

}

}

/*2. We need to add the vector components from two vectors of given size n and then store them in a third vector.

Let's implement this

*/

void add_vector(double vector1[], double vector2[], double vector3[], int n){

//Since the size of all the vectors is same we would use only one for loop.

for (int i = 0; i < n; ++i)

{

//First get the sum of components from vector1 and vector2 on position i

int sum = vector1[i] + vector2[i];

//Now set this sum to the corresponding element on vector3.

vector3[i] = sum;

}

}

/*3. We need to find the scalar prod of two vectors of same size.

Let's implement this

*/

int scalar_prod(double vector1[], double vector2[], int n){

//By the defination of scalar prod, it is sum of multiplications of elements at same positions.

//initialize the return variable

int scalar_prod = 0;

for (int i = 0; i < n; ++i)

{

//let's calculate the multiplication at the ith position of two vectors.

int scalar_prod_individual = vector1[i] * vector2[i];

//Add this to the scalar_prod variable

scalar_prod += scalar_prod_individual;

}

return scalar_prod;

}

/*4. We need to find the L2 norm of vector. By defination, it is the square root of scalar product of vector with itself.

Let's implement this

*/

int norm2(double vector[], int n){

//First calculate the scalar product of the vector with itself, so basically pass the same vector in the scalar_prod function.

int scalar_prod = scalar_prod(vector, vector , n);

//Calculate the square root of the scalar_prod which is our answer. Using sqrt which is math function for calculating square root of a no.

int norm2 = sqrt(scalar_prod);

return norm2;

}