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...
Question Rewrite the following C program using switch-case statement. and give me an output please use...
Question Rewrite the following C program using switch-case statement. and give me an output please use printf and scanf #include<stdio.h> int main(void) {      int semester;           printf("What is your Studying Level (1-8)?> ");      scanf("%d" , &semester);      if(semester == 1 || semester == 2)           printf("Your are a freshman!\n ");      else if(semester == 3 || semester == 4)           printf("Your are sophomore!\n ");      else if(semester == 5 || semester == 6)           printf("Your are...
Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2...
Miscellaneous C knowledge. (Put all answers in the same q5.txt file, they’re all short.) (a) [2 marks] On an old 16-bit computer system and its C compiler,sizeof(double)=8andsizeof(int)=2. Given the two type definitions below, what weresizeof(s)andsizeof(lingling)on that system? Assume that ins, there is no gap between the twofields. typedef struct s { double r[5];int a[5]; } s; typedef union lingling { double r[5];int a[5]; } lingling; (b) [2 marks] Given the following declarations, two questions: What is the type ofq? Whatis...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels...
IN C PROGRAMMING A Tv_show structure keeps track of a tv show’s name and the channels (integer values) that broadcast the show. For this problem you can ONLY use the following string library functions: strcpy, strlen, strcmp. You MAY not use memcpy, memset, memmove. You can assume memory allocations are successful (you do not need to check values returned by malloc nor calloc). typedef struct tv_show { char *name; int num_channels, *channels; } Tv_show; a. Implement the init_tv_show function that...
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:...
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*...
Please ask the user to input a low range and a high range and then print...
Please ask the user to input a low range and a high range and then print the range between them. Add printRang method to BST.java that, given a low key value, and high key value, print all records in a sorted order whose values fall between the two given keys from the inventory.txt file. (Both low key and high key do not have to be a key on the list). ****Please seperate the information in text file by product id,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT