Introduction 1. The deadline of submission is February 15th. Acceptable file for
ID: 3792459 • Letter: I
Question
Introduction 1. The deadline of submission is February 15th. Acceptable file format for submission is C source file, not a text file, archive file, word document or executable file. 2. Check your program carefully before submission. Test data differs from the sample input will be used in the grading. Make sure your program work properly for all possible cases. 3. You just need to fill the content of hw2.c, do not change the form of the function. It is fine that the main function remains blank. Functions Finish the following functions in hw2.c. 1. void vector input (float v lint n): input the elements of vector v and store in the array. (15 points) 2. void vector output float v nt n): output the elements of vector v on the screen (15 points) 3. void vector a (float v10, float v2 float v30,int n): calculate the sum of vector v1 and v2 and store the result in v3. The sum is a vector, not a single value (20 points) 4. void vector multiply (float vl.int n float x) multiply each element in v by T (15 points) 5. float inner-product(float v10, float v2 ,int n): return the inner product of vector v1 and v2. (20 points) 6. float length (float v int n): return the length of vector v (15 points) Sample Input and output 1. vector add: (1.2, 3.5, 5.7)+(3.2, 1, 6-3) (4.4, 4.5, 12) 2. vector multiply: (2.2, 4.1, 6.50x3 (6.6, 12.3, 19.5) 3. inner-product: (1,2,3) (4,5,6) 1x4+2x5+3x6 4-+10+18 32 4. length: length of vector (3,4,5) is V9-16+ 25 30 7.071068Explanation / Answer
c code:
#include <stdio.h>
#include <math.h>
int n = 5;
float v1[5];
float v2[5];
float v3[5];
void vector_input(float v[], int n)
{
int i = 0;
while(i<n)
{
printf("Please input next number ");
scanf("%f", &v[i]);
i++;
}
}
void vector_output(float v[], int n)
{
int i = 0;
while(i<n)
{
printf("%f ", v[i]);
i++;
}
printf(" ");
}
void vector_add(float v1[],float v2[],float v3[], int n)
{
int i = 0;
while(i<n)
{
v3[i] = v1[i] + v2[i];
i++;
}
}
void vector_multiply(float v[], int n, float x)
{
int i = 0;
while(i<n)
{
v[i] = x*(v[i]);
i++;
}
}
void inner_product(float v1[], float v2[], int n)
{
float s = 0;
int i = 0;
while(i<n)
{
s = s + (v1[i])*(v2[i]);
i++;
}
printf("Inner product = %f ", s);
}
float length(float v[], int n)
{
float s = 0;
int i = 0;
while(i<n)
{
s = s + (v[i])*(v[i]);
i++;
}
printf("%f ", sqrt(s));
return sqrt(s);
}
int main()
{
vector_input(v1,n);
vector_output(v1,n);
printf("%s ", "Add");
vector_add(v1,v1,v3,n);
vector_output(v3,n);
printf("%s ", "multiply");
vector_multiply(v3,n,2);
vector_output(v3,n);
inner_product(v1,v3,n);
length(v1,n);
return 0;
}
Sample Output:
Please input next number
1
Please input next number
2
Please input next number
3
Please input next number
4
Please input next number
5
1.000000 2.000000 3.000000 4.000000 5.000000
Add
2.000000 4.000000 6.000000 8.000000 10.000000
multiply
4.000000 8.000000 12.000000 16.000000 20.000000
Inner product = 220.000000
7.416198
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.