Question

Please answer the following C question: Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable...

Please answer the following C question:

Read the files vec5C.h, vec5C.c, and main5C.h. Build an executable using gcc -Wall main5C.c vec5C.c

Do this: Add function definitions to vec5C.c so that functions are available for dot product, sum, and cross product. Do not change the function prototypes in vec5C.h. Then add code to main to call these functions and display the dot product of u and v, the sum of u and v, and the cross product of u and v.

File vec5C.h

struct vec {
double x;
double y;
double z;
};

typedef struct vec vec_t;

void print_vec(vec_t v);
// PROMISES:
// Members of v are printed in a format as in this example:
// (-1.25 0.5 19.0)


// REMARK: The interfaces below do not use a consistent style for
// communication of inputs and results of functions. That would be
// annoying in production code, but in this exercise it helps
// students learn when to use the . operator and when to use the ->
// operator.

vec_t scalar_product(double k, vec_t v);
// PROMISES:
// Return value is scalar product of k and v.

double dot_product(vec_t left, vec_t right);
// PROMISES:
// Return value is dot product of k and v.

vec_t sum(const vec_t* left, const vec_t *right);
// REQUIRES:
// left and right point to vector objects.
// PROMISES:
// Return value is sum of *left and *right.

void cross_product(const vec_t* left, const vec_t *right, vec_t *product);
// REQUIRES:
// left, right and product point to vector objects.
// PROMISES:
// *product contains cross product of *left and *right.

File vec5C.c

/ Function definitions for operations on vectors in 3-space.

#include <stdio.h>
#include "vec5C.h"

void print_vec(vec_t v)
{
// REMARK: %g usually does a more helpful job or printing a double
// than %f does.
printf("(%g %g %g)", v.x, v.y, v.z);
}

vec_t scalar_product(double k, vec_t v)
{
vec_t result;
result.x = k * v.x;
result.y = k * v.y;
result.z = k * v.z;
return result;
}

File main5C.h

// main function to demonstrate operations on vectors in 3-space.

#include <stdio.h>
#include "vec5C.h"
gcc -wall main5c.c vec5c.c

int main(void)
{
vec_t u = {1.5, 0.5, 0.125}, v = {-2.0, 3.5, -3.0};
  
printf("Value of u: ");
print_vec(u);
printf("\nValue of v: ");
print_vec(v);
printf("\n\n");

// Demonstrate scalar product.
double k = 0.5;
vec_t sp;
sp = scalar_product(k, v);
printf("Scalar product of %g and v is: ", k);
print_vec(sp);
printf("\n\n");

return 0;
}

Include copies of the final .c and .h files and program output.

Homework Answers

Answer #1

// vec5C.h
#ifndef VEC5C_H
#define VEC5C_H

struct vec {
double x;
double y;
double z;
};

typedef struct vec vec_t;

void print_vec(vec_t v);
// PROMISES:
// Members of v are printed in a format as in this example:
// (-1.25 0.5 19.0)


// REMARK: The interfaces below do not use a consistent style for
// communication of inputs and results of functions. That would be
// annoying in production code, but in this exercise it helps
// students learn when to use the . operator and when to use the ->
// operator.

vec_t scalar_product(double k, vec_t v);
// PROMISES:
// Return value is scalar product of k and v.

double dot_product(vec_t left, vec_t right);
// PROMISES:
// Return value is dot product of k and v.

vec_t sum(const vec_t* left, const vec_t *right);
// REQUIRES:
// left and right point to vector objects.
// PROMISES:
// Return value is sum of *left and *right.

void cross_product(const vec_t* left, const vec_t *right, vec_t *product);
// REQUIRES:
// left, right and product point to vector objects.
// PROMISES:
// *product contains cross product of *left and *right.

#endif

//end of vec5C.h

// vec5C.c
// Function definitions for operations on vectors in 3-space.

#include <stdio.h>
#include "vec5C.h"

void print_vec(vec_t v)
{
// REMARK: %g usually does a more helpful job or printing a double
// than %f does.
printf("(%g %g %g)", v.x, v.y, v.z);
}

vec_t scalar_product(double k, vec_t v)
{
vec_t result;
result.x = k * v.x;
result.y = k * v.y;
result.z = k * v.z;
return result;
}

// function to return dot product of left and right vectors
double dot_product(vec_t left, vec_t right)
{
return (left.x*right.x + left.y + right.y + left.z*right.z);
}

// function to calculate sum of vectors left and right and return the resultant vector
vec_t sum(const vec_t* left, const vec_t *right)
{
vec_t result;
result.x = left->x + right->x;
result.y = left->y + right->y;
result.z = left->z + right->z;

return result;
}

// function to calculate cross product of left and right and store it in product
void cross_product(const vec_t* left, const vec_t *right, vec_t *product)
{
product->x = (left->y*right->z)-(left->z*right->y);
product->y = (left->z*right->x)-(left->x*right->z);
product->z = (left->x*right->y)-(left->y*right->x);
}

// end of vec5C.c

// main5C.c : main function to demonstrate operations on vectors in 3-space.

#include <stdio.h>
#include "vec5C.h"


int main(void)
{
vec_t u = {1.5, 0.5, 0.125}, v = {-2.0, 3.5, -3.0};

printf("Value of u: ");
print_vec(u);
printf("\nValue of v: ");
print_vec(v);
printf("\n\n");

// Demonstrate scalar product.
double k = 0.5;
vec_t sp;
sp = scalar_product(k, v);
printf("Scalar product of %g and v is: ", k);
print_vec(sp);
printf("\n\n");

printf("Dot product of u and v is: %f",dot_product(u,v));
printf("\n\n");

vec_t result = sum(&u,&v);
printf("Sum of u and v is: ");
print_vec(result);
printf("\n\n");

cross_product(&u, &v, &result);
printf("Cross product of u and v is: ");
print_vec(result);
printf("\n\n");

return 0;
}

// end of main5C.c

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an...
Please answer the following C question: Read the following files called array-utils5A.c and array-utils5A.h. Build an executable with gcc -Wall -DUNIT_TESTS=1 array-utils5A.c The definitions for is_reverse_sorted and all_different are both defective. Rewrite the definitions so that they are correct. The definition for is_alternating is missing. Write a correct definition for that function, and add unit tests for it, using the unit tests for is_reverse_sorted and all_different as models. Please explain the logic errors present in in the definition of is_reverse_sorted...
Using the C programming language implement Heapsort in the manner described in class. Here is some...
Using the C programming language implement Heapsort in the manner described in class. Here is some example code to use as a guideline. Remember, you need only implement the sort algorithm, both the comparison and main functions have been provided. /* * * after splitting this file into the five source files: * * srt.h, main.c, srtbubb.c, srtinsr.c, srtmerg.c * * compile using the command: * * gcc -std=c99 -DRAND -DPRNT -DTYPE=(float | double) -D(BUBB | HEAP | INSR |...
Please answer the following C question: There is a documented prototype for a function called get_a_line...
Please answer the following C question: There is a documented prototype for a function called get_a_line in the code below. Write a definition for get_a_line—the only function called from that definition should be fgetc. #include <stdio.h> #include <string.h> #define BUFFER_ARRAY_SIZE 10 int get_a_line(char *s, int size, FILE *stream); // Does what fgets does, using repeated calls to fgetc, but // provides a more useful return value than fgets does. // // REQUIRES // size > 1. // s points to...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...
This is C programming assignment. The objective of this homework is to give you practice using...
This is C programming assignment. The objective of this homework is to give you practice using make files to compose an executable file from a set of source files and adding additional functions to an existing set of code. This assignment will give you an appreciation for the ease with which well designed software can be extended. For this assignment, you will use both the static and dynamic assignment versions of the matrix software. Using each version, do the following:...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...