Structures and Pointers – Geometry – Problem #3
IIT Kharagpur CS10001 / CS11001 Programming and Data Structures Lab Question Archive
Problem Statement
A user is asked to input two n-dimensional vectors but the dimension is not known initially. We use a structure to define a vector type variable, which contains two elements, an integer number denoting the dimension of the vector the structure is currently storing and a float pointer that points to a dynamically allocated array that stores the vector components corresponding to each of the n dimensions. Take input a number n and correspondingly take input two n-dimensional vectors, storing the dimension and the components of each vector into two corresponding vector type struct variables. Note that before taking input the vector components, remember to initial- ize the array within the struct variable which will hold the componants. Write the C-functions to perform the following. • struct Vector VectorSum(struct Vector *v1, struct Vector *v2): Performs the sum of the two vectors of the same dimension and returns another structure of type vector that holds the sum. For two n-dimensional vectors with components [x1, x2, x3,...,xn] and [y1, y2, y3,...,yn], their sum is another n-dimensional vector [x1+y1, x2+y2, x3+y3,....,xn+yn]. • int OrthogonalTest(struct Vector *v1, struct Vector *v2): Returns 1 if two vectors v1 and v2 are orthogonal, 0 otherwise. Two n-dimensional vectors with components [x1, x2, x3,...,xn] and [y1, y2, y3,...,yn] are said to be orthogonal if their inner product (dot product) is zero, x1.y1+x2.y2+x3.y3+....+xn.yn = 0. After taking input the two vectors, first print both the vectors on console in the format ‘(n, [.......])’, where n represents the dimension and put the dimension components within the square brackets. Hence call the above mentioned functions from main() and print the results returned.