ELEC 1520, Homework structs, 100 points submission instructions: Your homework w
ID: 3819270 • Letter: E
Question
ELEC 1520, Homework structs, 100 points submission instructions: Your homework will be submitted electronically to Can The file format must be a zip file. Your file should be named elec 1520 hw struct your name.zip. Substitute your first and last name for "your name" in the file name. Example: Students name is Sparky Watts. The file name is elec 1520 hw struct sparky watts.zip. Break your programming solution up into three separate files: main.c, vector.c, and vector.h Place these in your compressed file. The program must include the following information in comments: (1) problem number, (2) course information, (3) assignment, (4) date, and (5) student name. 1. Write a program that creates a vector struct and a set of functions that perform vector calculations. mag calculate and return the magnitude of the vector add calculate and return vector c: vector c vector a vector b sub calculate and return vector c: vector c vector a vector b mult multiply a vector by a scalar dist calculate and return the distance between two points dot-calculate and return the dot product of two vectors normalize normalize the vector to a length of 1 heading-calculate the angle of rotation for this vector. (rotation about origin) Ista and reta rn the snnlaExplanation / Answer
/*****************************vector.h file ************************************************************/
typedef struct
{
double x;
double y;
}vector;
void print_vector(int i, vector v[8]);
vector add_vector(vector v[8],int i,int j );
vector sub_vector(vector v[8],int i,int j );
vector scalar_mult(vector v , double val);
double dot_vector(vector v[8], int i, int j);
double dist_vector(vector v[8], int i,int j);
void vector_heading(vector v[8]);
/**********************************vector.c file**********************************************/
#include "header.h"
/* fuction to print vector*/
void print_vector(int i, vector v[8])
{
printf("v[%d] data : x = %0.2f y = %0.2f ", i, v[i].x, v[i].y);
}
/*function to add vector */
vector add_vector(vector v[8],int i,int j )
{
v[4].x = v[i].x + v[j].x;
v[4].y = v[i].y + v[j].y;
printf("v[4] = v[%d] + v[%d], v[4] data : x %0.2f y %0.2f ", i,j,v[4].x, v[4].y);
return v[4];
}
/*function to subtract vector*/
vector sub_vector(vector v[8],int i,int j )
{
v[5].x = v[i].x + v[j].x;
v[5].y = v[i].y + v[j].y;
printf("v[5] = v[%d] - v[%d], v[5] data : x %0.2f y %0.2f ", i,j,v[5].x, v[5].y);
return v[5];
}
/* function for scalar multipication*/
vector scalar_mult(vector v , double val)
{
vector v1;
v1.x = v.x * val;
v1.y = v.y * val;
return v1;
}
/*function to caculate distance between 2 points*/
double dist_vector(vector v[8], int i,int j)
{
double dist;
if(i = j)
{
return 0;
}
dist = pow((v[i].x - v[j].x),2) + pow((v[i].y - v[j].y),2);
dist = sqrt( dist);
return dist;
}
/* dot product */
double dot_vector(vector v[8], int i, int j)
{
double k;
k = ((v[i].x * v[j].x) + (v[i].y * v[j].y));
return k;
}
/* to calculate angle of rotation*/
void vector_heading(vector v[8])
{
int i;
double angle;
angle = atan(v[0].y/v[0].x);
printf("v[0] data: x = %0.2f y = %0.2f , %0.2f degrees", v[0].x, v[0].y, angle);
angle = atan((v[1].y/v[1].x)*(-1));
printf("v[1] data: x = %0.2f y = %0.2f , -%0.2f degrees", v[1].x, v[1].y, angle);
angle = atan((v[2].y/v[2].x)*(-1));
angle = 180 - angle;
printf("v[2] data: x = %0.2f y = %0.2f , %0.2f degrees", v[0].x, v[0].y, angle);
angle = atan(v[3].y/v[3].x);
angle = angle - 180;
printf("v[0] data: x = %0.2f y = %0.2f , 0.2f degrees", v[0].x, v[0].y, angle);
}
/*********************** main.c file ********************************************************/
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "vector.h"
#include "vector.c"
int main()
{
vector v[8];
int i,j,k;
v[0].x = 3;
v[0].y = 4;
v[1].x = 3;
v[1].y = -4;
v[2].x = -3;
v[2].y = 4;
v[3].x = -3;
v[3].y = -4;
printf("*** Test vector - one per quadrant *** ");
for(i = 0;i< 4;i++)
{
print_vector(i , v);
}
printf("*** Test vector addition and subtracion ");
v[4]= add_vector(v, 0,1);
v[5]= sub_vector(v, 1, 3);
printf("*** Test scalar multiplication ");
v[6] = scalar_mult(v[0], 1.50);
printf("v[6] = v[0] is scaled by 1.50 v[6] data : x %0.2f y %0.2f ", i,j,v[6].x, v[6].y);
v[7] = scalar_mult(v[1], 0.85);
printf("v[7] = v[1] is scaled by 0.85 v[7] data : x %0.2f y %0.2f ", i,j,v[7].x, v[7].y);
return 0;
prinf("**** distance between 2 points ***");
for(i = 0;i<4;i++)
{
for(j = 0;j<4;j++)
{
k = dist_vector(v,i,j);
printf("distance from v[%d] to v[%d] is %0.2f ", i,j,k);
}
printf(" ");
}
printf("***** vector heading ****");
vector_head(v);
printf("*** dot product *****");
k= dot_vector(v,0,1);
printf("dot product of v[0] and v[1] is %0.2f", k);
k =dot_vector(v,1,2);
printf("dot product of v[1] and v[2] is %0.2f", k);
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.