Use the program skeleton below (starting with #include <stdio.h>)
as the starting point for quiz4. Add the necessary code to the
functions prob1() and prob2(), and add the other 2 functions, as
described in the text below.
You do not need to change anything in main().
In void prob1(void), take a double floating-point number x from
the keyboard and compute the function f(x), which is defined by:
f(x) = exp(x)-2*log10(x)+log( x) if x >=5
f(x) = 1.5+x*sin(x) if -2 < x < 5
f(x) = x*2+3*x-sinh(x) if x <= -2.
This function should be computed in: double func(double x).
In prob1(void), print x and f(x) on the screen.
In void prob2(void), scan the number of elements k that
is less than 9, and the elements of two k-component vectors
v[] and w[]. Print these vectors on the screen.
Then, pass the vectors v[] and w[] and k to a double function
double norm2(double *v, double *w, int k) which will normalize
these two vectors and return the length of the original vector w[].
In prob2(void), print the components of the unormalized (unit)
vectors on the screen, as well as the length of the original vector
w[]. If k>5, let 5 components of the vector appear in one line.
*/
/*
Your output should look like:
iacs5.ucsd.edu% gcc quiz4.c -lm
iacs5.ucsd.edu% a.out
There are two functions: prob1() and prob2().
Enter the function number to execute (1 or 2): 1
Here is Problem 1:
Enter a double f-p constant: -2.5
f(-2.5) = 4.8002
iacs5.ucsd.edu% a.out
There are two functions: prob1() and prob2().
Enter the function number to execute (1 or 2): 2
Here is Problem 2:
Enter the number of elements (<9): 7
Enter 7 components of v[]
-1 2.0 3.1 -5.2 11.2 -0.25 6.
Enter 7 components of w[]
0.25 -5.1 3.5 1.25 -7.1 0.0 -3.5
v[] as entered:
-1.000 +2.000 +3.100 -5.200 +11.200
-0.250 +6.000
w[] as entered:
+0.250 -5.100 +3.500 +1.250 -7.100
+0.000 -3.500
normalized v[]:
-0.070 +0.140 +0.217 -0.365 +0.786
-0.018 +0.421
normalized w[]:
+0.025 -0.504 +0.346 +0.123 -0.701
+0.000 -0.346
length_w = 10.126
I need help with part 2. any suggestions? my vectors won't normalize and the length won't calculate
as the starting point for quiz4. Add the necessary code to the
functions prob1() and prob2(), and add the other 2 functions, as
described in the text below.
You do not need to change anything in main().
In void prob1(void), take a double floating-point number x from
the keyboard and compute the function f(x), which is defined by:
f(x) = exp(x)-2*log10(x)+log( x) if x >=5
f(x) = 1.5+x*sin(x) if -2 < x < 5
f(x) = x*2+3*x-sinh(x) if x <= -2.
This function should be computed in: double func(double x).
In prob1(void), print x and f(x) on the screen.
In void prob2(void), scan the number of elements k that
is less than 9, and the elements of two k-component vectors
v[] and w[]. Print these vectors on the screen.
Then, pass the vectors v[] and w[] and k to a double function
double norm2(double *v, double *w, int k) which will normalize
these two vectors and return the length of the original vector w[].
In prob2(void), print the components of the unormalized (unit)
vectors on the screen, as well as the length of the original vector
w[]. If k>5, let 5 components of the vector appear in one line.
*/
Code:
/* Program skeleton */ #include <stdio.h> #include <math.h> void prob1(void); void prob2(void); double func(double x); double norm2(double *v, double *w, int k); main() { int menu; printf("There are two functions: prob1() and prob2().\n"); printf("Enter the function number to execute (1 or 2):"); scanf("%d", &menu); /* form a switch to execute one function */ switch(menu) { case 1: prob1(); break; case 2: prob2(); break; default: printf("prob%d() does not exist.\n", menu); } exit(0); } /* Problem 1 */ void prob1(void) { double x; printf("Here is Problem 1:\n"); printf("\nEnter a double f-p constant: "); scanf("%lf", &x); printf("\nf(%g) = %g\n", x, func(x)); return; } double func(double x) { double y; if(x>=5.) y=exp(x)-2.*log10(x)+log(x); else if(x<=-2.) y=pow(x,2.)+3.*x-sinh(x); else if(-2.<x, x<5.) y=1.5+x*sin(x); } /* Problem 2 */ void prob2(void) { int k, row, n, sum, sum1; double v[9], w[9]; printf("Here is Problem 2:\n"); printf("\nInsert the number of elements (<9): "); scanf("%d", &k); printf("\nEnter %d components of v[]\n", k); for(row=0;row<k;row++){ scanf("%lf", &v[row]); } printf("\nEnter %d components of w[]\n", k); for(row=0;row<k;row++){ scanf("%lf", &w[row]); } printf("\nv[] as entered:\n"); for(row=0;row<k;row++){ printf("%.3f ", v[row]); } printf("\n\nw[] as entered:\n"); for(row=0;row<k;row++){ printf("%.3f ", w[row]); } sum1=norm2(v,w,k); printf("\n\nnormalized v[]:\n"); for(row=0;row<k;row++){ printf("%.3f ", norm2(v,w,k));} printf("\n\nnormalized w[]:\n"); for(row=0;row<k;row++){ printf("%.3f ", norm2(v,w,k));} printf("\n\nlength_w = %f\n", sum1); return; } double norm2(double *v, double *w, int k) { double sum, sum1; int row; for(row=0, sum=0.0; row<k; row++) sum += v[row]*v[row]; sum=sqrt(sum); for(row=0; row<k; row++) v[row]=v[row]/sum; for(row=0, sum1=0.0; row<k; row++) sum1 += w[row]*w[row]; sum1=sqrt(sum1); for(row=0; row<k; row++) w[row]=w[row]/sum1; return sum1; }
Your output should look like:
iacs5.ucsd.edu% gcc quiz4.c -lm
iacs5.ucsd.edu% a.out
There are two functions: prob1() and prob2().
Enter the function number to execute (1 or 2): 1
Here is Problem 1:
Enter a double f-p constant: -2.5
f(-2.5) = 4.8002
iacs5.ucsd.edu% a.out
There are two functions: prob1() and prob2().
Enter the function number to execute (1 or 2): 2
Here is Problem 2:
Enter the number of elements (<9): 7
Enter 7 components of v[]
-1 2.0 3.1 -5.2 11.2 -0.25 6.
Enter 7 components of w[]
0.25 -5.1 3.5 1.25 -7.1 0.0 -3.5
v[] as entered:
-1.000 +2.000 +3.100 -5.200 +11.200
-0.250 +6.000
w[] as entered:
+0.250 -5.100 +3.500 +1.250 -7.100
+0.000 -3.500
normalized v[]:
-0.070 +0.140 +0.217 -0.365 +0.786
-0.018 +0.421
normalized w[]:
+0.025 -0.504 +0.346 +0.123 -0.701
+0.000 -0.346
length_w = 10.126
I need help with part 2. any suggestions? my vectors won't normalize and the length won't calculate
Comment