C The difference of two vectors (x1, x2, ..., xN) and (y1, y2, ..., yN) is anoth
ID: 3911892 • Letter: C
Question
C
The difference of two vectors (x1, x2, ..., xN) and (y1, y2, ..., yN) is another vector (z1, z2, ..., zN) defined as
(x1-y1, x2-y2, ..., xN-yN)
where subtraction is performed in a component-wise fashion. The sum of two vectors is calculated by performing component-wise addition. Write an app, vsubsum, that takes two vectors as input on stdin and outputs two vectors: their difference and sum. The format of the input should be
N
x1 y1
x2 y2
...
xN yN
where the integer in the first line specifies the dimension (i.e., number of elements) of the vectors and the N pairs of numbers that follow specify the components of the two vectors. Make the type of the vector components double, the calculations should be done as double, and output the results with accuracy 3 digits below the decimal point for each vector component.
From the viewpoint of app programming, coding is straightforward. However, as an exercise in C programming, we will put forward constraints that your code must meet.
Declare four 1-D arrays A[MAXSZ], B[MAXSZ], C[MAXSZ], and D[MAXSZ] of type double where MAXSZ is a constant defined using the C preprocessor command, #define MAXSZ 75, inside vsubsum.h which is included in main.c. The two 1-D arrays A[] and B[] are local to main() and contain the two vectors read in from stdin. The arrays C[] and D[] which contain their difference and sum are declared as global data structures. Also, define a local variable vecdim of type int inside main() that will hold the size of the vectors read from stdin.
Reading the values of the two vectors and their size should be delegated to a function
int read_vecs(double *, double *, int *);
called from main() where the first and second argument passes pointers to the input vectors and the third argument passes the pointer to vecdim. If the input provided by a user is ill-formed (i.e., does not follow the input specification format), read_vecs() returns -1. main() should check for this condition and print out a suitable error message to stderr before terminating. Use scanf() to read from stdin in read_vecs().
Calculation of the difference and sum of the two input vectors is done by two functions
void calc_diff(double *, double *, int);
where the first and second argument are pointers to the input vectors, and the third argument passes vecdim by value. Calculation of the sum is performed by
void calc_sum(double *, double *, int);
in an analogous manner.
Output the difference vector followed by the sum vector to stdout by calling function
void print_result(int);
where the integer argument is vecdim passed by value. The components of a each should be separated by tabs. After the difference vector is ouput, a newline should be output before the sum vector is printed.
Put each of the three functions in separate files main.c, read_vecs.c, calc_diff.c, and calc_sum.c. Create a Makefile that compiles each C source file individually to generate object files which are then linked to generate an executable file named vsubsum. Make sure to set up the dependencies correctly. Code, compile, debug, and test that your app works correctly. Testing should include the case where user input is ill-formed. Make sure to comment your code. Deposit your source code (*.c and *.h) files and Makefile in vsubsum/.
Explanation / Answer
The project files are given below.
#pragma once
double C[MAXSZ];
double D[MAXSZ];
int read_vecs(double *, double *, int *);
void calc_diff(double *, double *, int );
void calc_sum(double *, double *, int );
void print_result(int );
#include"read_vecs.c"
#include"calc_diff.c"
#include"calc_sum.c"
#include"print_result.c"
#pragma once
#include<stdio.h>
#include"Declarations.h"
int read_vecs(double *A, double *B, int *vecdim)
{
int i;
printf("Enter the vectors' size: ");
int retVal = scanf("%d", vecdim);
if(retVal == 0)
return -1;
if(*vecdim > 75)
{
printf("Size must be less than or equal to 75. ");
return -1;
}
printf(" Enter the values of vector A and B seperated by space. ");
for(i = 0; i < *vecdim; i++)
{
retVal = scanf("%lf %lf", &A[i], &B[i]);
if(retVal != 2)
return -1;
}
}
#pragma once
#include"Declarations.h"
#include<stdio.h>
void print_result(int vecdim)
{
int i;
printf("Difference of the vectors is. ");
for(i = 0; i < vecdim; i++)
printf("%.3f ", C[i]);
printf(" ");
printf("Sum of the vectors is. ");
for(i = 0; i < vecdim; i++)
printf("%.3f ", D[i]);
}
#include<stdio.h>
#define MAXSZ 75
#include "Declarations.h"
int main()
{
double A[MAXSZ];
double B[MAXSZ];
int vecdim = 0;
int retVal = read_vecs(A, B, &vecdim);
if(retVal == -1)
{
printf("ERROR! Bad value supplied! ");
return -1;
}
calc_diff(A, B, vecdim);
calc_sum(A, B, vecdim);
print_result(vecdim);
}
Instruction: Create all the above files with the exact names as given in the table headers. Finally, run the main program.
Declarations.h#pragma once
double C[MAXSZ];
double D[MAXSZ];
int read_vecs(double *, double *, int *);
void calc_diff(double *, double *, int );
void calc_sum(double *, double *, int );
void print_result(int );
#include"read_vecs.c"
#include"calc_diff.c"
#include"calc_sum.c"
#include"print_result.c"
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.