Program provides an illustration of a function with structure as one of its parameter. The function calculates and returns the magnitude of a vector which is an instance of structure defined to hold the three components of a vector as its data members. The structure is declared as below.
struct vector { double x; double y; double z;};
Note that x, y, and z are three components of the vector. The function to return magnitude of vector is defined as given below.
double Magnitude ( struct vector V1 ) II Function head
{double Mag ; II Function body
Mag= sqrt(V1.x * V1.x + V1.y*V1.y + V1.z*V1.z);
return Mag; }
Program demonstrates implementation of the codes described above.
#include<stdio.h>
#include<math.h>
struct vector {double x ; // Declaration of structure
double y ;
double z ;};
double Magnitude ( struct vector V) //definition of function
{
double Mag ;
Mag = sqrt(V.x * V.x + V.y*V.y + V.z*V.z);
return Mag;
}
void main()
{
struct vector V1 = {4,5,7};
struct vector V2 = {2.5,6.5,8.5};
double V1mag, V2mag;
clrscr();
V1mag =Magnitude (V1);
V2mag = Magnitude(V2);
printf("Magnitude of V1 = %f\n", V1mag);
printf("Magnitude of V2 = %f\n", V2mag);
}
Dinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.
Related Articles
Basic Courses
Advance Courses