471,056 Members | 1,534 Online

normalization of vectors

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.
*/
Expand|Select|Wrap|Line Numbers
1. /* Program skeleton */
2. #include <stdio.h>
3. #include <math.h>
4. void prob1(void);
5. void prob2(void);
6.
7. double func(double x);
8. double norm2(double *v, double *w, int k);
9.
10. main()
11. {
12. int menu;
13. printf("There are two functions: prob1() and prob2().\n");
14. printf("Enter the function number to execute (1 or 2):");
15. scanf("%d", &menu);
16. /* form a switch to execute one function */
17. switch(menu)
18. {
19.  case 1:
20.     prob1();
21.     break;
22.  case 2:
23.     prob2();
24.     break;
25.  default:
26. printf("prob%d() does not exist.\n", menu);
27. }
28. exit(0);
29. }
30.
31. /*  Problem 1 */
32. void prob1(void)
33. {
34. double x;
35. printf("Here is Problem 1:\n");
36. printf("\nEnter a double f-p constant: ");
37. scanf("%lf", &x);
38. printf("\nf(%g) = %g\n", x, func(x));
39. return;
40. }
41.
42. double func(double x)
43. {
44. double y;
45. if(x>=5.)
46.         y=exp(x)-2.*log10(x)+log(x);
47. else if(x<=-2.)
48.         y=pow(x,2.)+3.*x-sinh(x);
49. else if(-2.<x, x<5.)
50.         y=1.5+x*sin(x);
51. }
52.
53.
54. /*  Problem 2 */
55. void prob2(void)
56. {
57. int k, row, n, sum, sum1;
58. double v[9], w[9];
59.
60. printf("Here is  Problem 2:\n");
61. printf("\nInsert the number of elements (<9): ");
62. scanf("%d", &k);
63. printf("\nEnter %d components of v[]\n", k);
64. for(row=0;row<k;row++){
65.         scanf("%lf", &v[row]);
66.         }
67. printf("\nEnter %d components of w[]\n", k);
68. for(row=0;row<k;row++){
69.         scanf("%lf", &w[row]);
70.         }
71. printf("\nv[] as entered:\n");
72. for(row=0;row<k;row++){
73.         printf("%.3f    ", v[row]);
74.         }
75. printf("\n\nw[] as entered:\n");
76. for(row=0;row<k;row++){
77.         printf("%.3f    ", w[row]);
78.         }
79. sum1=norm2(v,w,k);
80. printf("\n\nnormalized v[]:\n");
81. for(row=0;row<k;row++){
82. printf("%.3f    ", norm2(v,w,k));}
83. printf("\n\nnormalized w[]:\n");
84. for(row=0;row<k;row++){
85. printf("%.3f    ", norm2(v,w,k));}
86. printf("\n\nlength_w = %f\n", sum1);
87.
88. return;
89. }
90.
91. double norm2(double *v, double *w, int k)
92. {
93. double sum, sum1;
94. int row;
95.
96. for(row=0, sum=0.0; row<k; row++)
97.         sum += v[row]*v[row];
98. sum=sqrt(sum);
99. for(row=0; row<k; row++)
100.         v[row]=v[row]/sum;
101.
102. for(row=0, sum1=0.0; row<k; row++)
103.         sum1 += w[row]*w[row];
104. sum1=sqrt(sum1);
105. for(row=0; row<k; row++)
106.         w[row]=w[row]/sum1;
107. return sum1;
108. }
109.
110.
/*

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
Nov 21 '06 #1
2 8191
I Tried Putting Parantheses Around The Double Terms And Took The Sum=0 In Both For Terms Out. But That Didn't Help.

Pretty Interesting Topic. Let Me Know What Is Wrong With That I Couldn't Find A Problem.

Sorry I Couldn't Help.

Good Luck Hope You Get A Answer.
Nov 22 '06 #2
I Tried Putting Parantheses Around The Double Terms And Took The Sum=0 In Both For Terms Out. But That Didn't Help.

Pretty Interesting Topic. Let Me Know What Is Wrong With That I Couldn't Find A Problem.

Sorry I Couldn't Help.

Good Luck Hope You Get A Answer.
Found the problem and here is the corrected code =]

Expand|Select|Wrap|Line Numbers
1. #include <stdio.h>
2. #include <math.h>
3. void prob1(void);
4. void prob2(void);
5.
6. double func(double x);
7. double unit_vector(double *v, double *w, int num_n);
8.
9. main()
10. {
11. int menu;
12. printf("There are two functions: prob1() and prob2().\n");
13. printf("Enter the function number to execute (1 or 2):");
14. scanf("%d", &menu);
15. /* form a switch to execute one function */
16. switch(menu)
17. {
18.  case 1:
19.     prob1();
20.     break;
21.  case 2:
22.     prob2();
23.     break;
24.  default:
25. printf("prob%d() does not exist.\n", menu);
26. }
27. exit(0);
28. }
29.
30. /*  Problem 1 */
31. void prob1(void)
32. {
33. double x;
34. printf("Here is Problem 1:\n");
35. printf("\nEnter a double f-p constant: ");
36. scanf("%lf", &x);
37. printf("\nf(%g) = %g\n", x, func(x));
38. return;
39. }
40.
41. double func(double x)
42. {
43. double y;
44. if(x>=5.)
45.         y=exp(x)-2.*log10(x)+log(x);
46. else if(x<=-2.)
47.         y=pow(x,2.)+3.*x-sinh(x);
48. else if(-2.<x, x<5.)
49.         y=1.5+x*sin(x);
50. }
51.
52.
53. /*  Problem 2 */
54. void prob2(void)
55. {
56.         double a[8], b[8], r;
57.         printf("Here is Problem 2:\n");
58.         int n, num_n;
59.         printf("Enter the number of elements (<9):");
60.         scanf("%d",&n);
61.         printf("\nEnter %d components of v[]\n", n);
62.                 num_n=n+1;
63.                 for(n=1; n<num_n; n++)
64.                         scanf("%lf", &a[n]);
65.                         printf("\n");
66.
67.         printf("Enter %d components of w[]\n", n-1);
68.                 for(n=1; n<num_n; n++)
69.                         scanf("%lf", &b[n]);
70.                         printf("\n");
71.
72.                         printf("v[] as entered:");
73.                         n=0.0;
74.                 for(n=1; n<num_n; n++){
75.                         if((n-1)%5==0) printf("\n");
76.                         printf("%.3f  ", a[n]);
77.                         }
78.                 printf("\n\n");
79.
80.                         printf("w[] as entered:");
81.                         n=0.0;
82.                 for(n=1; n<num_n; n++){
83.                         if((n-1)%5==0) printf("\n");
84.                         printf("%.3f  ", b[n]);
85.                         }
86.                 printf("\n\n");
87.
88.                 unit_vector(a, b, num_n);
89.                 printf("normalized v[]:");
90.                 for(n=1;n<num_n; n++){
91.                         if((n-1)%5==0) printf("\n");
92.                         printf("%.3f  ", a[n]);
93.                         }
94.
95.                 r=unit_vector(b, a, num_n);
96.                 printf("\n\nnormalized w[]:");
97.                 for(n=1;n<num_n; n++){
98.                         if((n-1)%5==0) printf("\n");
99.                         printf("%.3f  ", b[n]);
100.                         }
101.                 printf("\n\n");
102.                 printf("length_w = %.3f\n\n", r);
103.         return;
104. }
105. double unit_vector(double *w, double *v, int num_n)
106. {
107.         int k;
108.         double sum;
109.         sum= 0.0;
110.         for(k=1; k<num_n; k++)
111.         sum +=w[k]*w[k];
112.                         }
113.
114.                 r=unit_vector(b, a, num_n);
115.                 printf("\n\nnormalized w[]:");
116.                 for(n=1;n<num_n; n++){
117.                         if((n-1)%5==0) printf("\n");
118.                         printf("%.3f  ", b[n]);
119.                         }
120.                 printf("\n\n");
121.                 printf("length_w = %.3f\n\n", r);
122.         return;
123. }
124. double unit_vector(double *w, double *v, int num_n)
125. {
126.         int k;
127.         double sum;
128.         sum= 0.0;
129.         for(k=1; k<num_n; k++)
130.         sum +=w[k]*w[k];
131.         sum=sqrt(sum);
132.         for(k=0; k<num_n; k++) w[k]/=sum;
133.         return sum;
134. }
135.
136.
Nov 24 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

 5 posts views Thread by Pratyush | last post: by 14 posts views Thread by Rakesh Kumar | last post: by 5 posts views Thread by Computer Whizz | last post: by 3 posts views Thread by Amit | last post: by 4 posts views Thread by Dr. J.K. Becker | last post: by 7 posts views Thread by John Welch | last post: by 5 posts views Thread by madhu | last post: by 20 posts views Thread by hippomedon | last post: by 1 post views Thread by Rob | last post: by reply views Thread by MartianBanks | last post: by reply views Thread by anoble1 | last post: by reply views Thread by clicknium | last post: by reply views Thread by autodeveloper | last post: by 6 posts views Thread by Videot7 | last post: by reply views Thread by Stoney L | last post: by reply views Thread by Twizzlers | last post: by reply views Thread by annayun | last post: by 1 post views Thread by OSWOM | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.