C++ homework help

Im having trouble trying to figure out why im getting 3 errors:



Error 1 error LNK2019: unresolved external symbol "double __cdecl power(double,int)" (?power@@YANNH@Z) referenced in function "void __cdecl taylor1(double)" (?taylor1@@YAXN@Z)

Error 2 error LNK2019: unresolved external symbol "double __cdecl fact(double)" (?fact@@YANN@Z) referenced in function "void __cdecl taylor1(double)" (?taylor1@@YAXN@Z)

Error 3 error LNK1120: 2 unresolved externals

Ive been at it for a couple hours and havent been able to figure it out.

The program is to be used to calculate sine using taylor series in case 1&2, and to just call sine for case 3.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
 #include <iostream>
#include <iomanip>

using namespace std;

void menu();
void dispatch(int k);
void taylor1(double rad1);
void taylor2(double rad2);
void cosine(double rad3);

double power(double x, int exp);
double fact(double y);
double rad1, rad2, rad3;


int main()
{
	int code;

	menu();
	cin >> code;

	while (code != -1)
	{
		dispatch(code);
		menu();
		cin >> code;
	}
}

 void menu()
{
	cout << "Type 1 for Taylor Series" << endl;
	cout << endl;

	cout << "Type 2 for Taylor Series #2" << endl;
	cout << endl;

	cout << "Type 3 for the cosine function" << endl;
	cout << endl;

}

 void dispatch(int code)
 {
	 switch (code)

	 {
	 case 1: 
		 cout << " What is the angle in radians?  ";
		 cin >> rad1;
		 taylor1(rad1);
		 break;

	 case 2: 
		 cout << " What is the angle in radians?  ";
		 cin >> rad2;
		 taylor2(rad2);
		 break;

	 case 3: 
		 cout << " What is the angle in radians?  ";
		 cin >> rad3;
		 cosine(rad3);
		 break;

	 default: cout << " There was a bad code used, please input another code" << endl;
	 }

 }


 void taylor1(double x)
 {
	 double total = 1;

	 {for (int i = 1; i <= 5; ++i)
		 if (i % 2 == 1)
			 total -= (power( x, i * 2) / fact(i * 2));
		 else total += (power( x, i * 2) / fact(i * 2));
	 }
	 cout << total;
 }

 void taylor2(double x)
 {
	  double total = 1;
	  int i=1;

	 while (x >= .0001)
	 {
		 if (i % 2 == 1)
			 total -= (power(x, i * 2) / fact(i * 2));
		 else total += (power(x, i * 2) / fact(i * 2));
		 ++i;
	 }
	 cout << total;
 }


 void cosine(double x)
 {
	 
	 double value = 0;

	 value = cos(x);

	 cout << " The cosine of " << x << " radians is " << value << endl;
 }
You're calling a function that hasn't yet been defined.
Ive cleaned up my code but ive bumped into another problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <iostream>
#include <iomanip>
#include <cstdlib>

using namespace std;

void menu();
void dispatch(int k);
void taylor1(double rad1);
void taylor2(double rad2);
void cosine(double rad3);
double pow(double x, int exp);
double factorial();

double rad1, rad2, rad3;


int main()
{
	int code;

	menu();
	cin >> code;

	while (code != -1)
	{
		dispatch(code);
		menu();
		cin >> code;
	}
}

 void menu()
{
	cout << "Type 1 for Taylor Series" << endl;
	cout << endl;

	cout << "Type 2 for Taylor Series #2" << endl;
	cout << endl;

	cout << "Type 3 for the cosine function" << endl;
	cout << endl;

}

 void dispatch(int code)
 {
	 switch (code)

	 {
	 case 1: 
		 cout << " What is the angle in radians?  ";
		 cin >> rad1;
		 taylor1(rad1);
		 break;

	 case 2: 
		 cout << " What is the angle in radians?  ";
		 cin >> rad2;
		 taylor2(rad2);
		 break;

	 case 3:
		 cout << " What is the angle in radians?  ";
		 cin >> rad3;
		 cosine(rad3);
		 break;

	 default: cout << " There was a bad code used, please input another code" << endl;
	 }

 }

 double pow(int b, int e)
 {
	 int x;

	 while (e != 0)
	 {
		 b *= x;
		 e--;
	 }
	 return b;
 }

 double factorial(double x)
 {
	 int factorial = 1;

	 for (int i = 0; i <= x; ++i)

		 if (i == 0)
			 factorial = 1;
		 else
			 factorial = factorial * i;
	 return factorial;

 }

 void taylor1(double x)
 {
	 double total = 1;

	 {for (int i = 0; i <= 5; i++)

		{
		  if (i % 2 == 1)
		
			 total += ( pow(x,i*2+2) / factorial(i*2+2) );

		  else
			 total -= (pow(x, i*2+2) / factorial(i*2+2) );
			 
		}
	 }
	 cout << total;
	 cout << endl;
 }


 void taylor2(double x)
 {
	 double total = 1;
	 int i = 1;
	 int count = 0;
	 double temp = 0;
	
	 {while (fabs(temp) >= .0001)
 
		 temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
	 {if (i % 2 == 1)
	 {
		 total += temp;
	 }
	 else {
		 total -= temp;
	 }
	 ++count;
	 i++;
	 }
	 }

	 cout << temp;
	 cout << total << endl;
	 cout << count << endl;
	 cout << endl;
	 
 }


 void cosine(double x)
 {
	 double value = 0;

	 value = cos(x);

	 cout << " The cosine of " << x << " radians is " << value << endl;
 }



When I run the taylor2 function, the value for temp seems to be stuck at zero when running and doesnt want to take any other value.
What will happen on line 128 given that temp is set to 0 immediately before the while condition?
I see, the temp being set to zero fores the program to skip over the while loop. Amazing how the smallest things can cause the biggest problems.


Ive come across another problem upon running the program. When I input a value over 2 for the function taylor2, the program doesnt return a value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

 double pow(int b, int e)
 {
	 int x;

	 while (e != 0)
	 {
		 b *= x;
		 e--;
	 }
	 return b;
 }

 double factorial(double x)
 {
	 int factorial = 1;

	 for (int i = 0; i <= x; ++i)

		 if (i == 0)
			 factorial = 1;
		 else
			 factorial = factorial * i;
	 return factorial;

 }

 void taylor1(double x)
 {
	 double total = 1;

	 {for (int i = 0; i <= 5; i++)

		{
		  if (i % 2 == 1)
		
			 total += ( pow(x,i*2+2) / factorial(i*2+2) );

		  else
			 total -= (pow(x, i*2+2) / factorial(i*2+2) );
			 
		}
	 }
	 cout << total;
	 cout << endl;
 }


 void taylor2(double x)
 {
	 double total = 1;
	 int i = 0;
	 int count = 1;
	 double temp = 0;

	 do
	 {

	 {
		 if (i % 2 == 1)
		 {
			 temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
			 total += temp;
		 }
		 else {
			 temp = (pow(x, i * 2 + 2) / factorial(i * 2 + 2));
			 total -= temp;
		 }
	 }
	 
	 count++;
	 i++;
	 

	 } while (fabs(temp) >= .0001);

	 cout << "The last recoreded temporary value was: "<<temp << endl;
	 cout << "The computed value for cosine is :  "<< total << endl;
	 cout << "It took " <<count << "values to calculate the value of the function to .0001 places"<< endl;
	 cout << endl; 
 }


 void cosine(double x)
 {
	 double value = 0;

	 value = cos(x);

	 cout << " The cosine of " << x << " radians is " << value << endl;
 }
Last edited on
Hi

On the pow function, you declare x with no initialisation, then multiply b by x without assigning a value to x at any point. Should that not be:

1
2
3
4
5
6
7
8
9
10
11
double pow(int b, int e)
 {
	 int x; // is this necessary?

	 while (e != 0)
	 {
		 b *= e; // changed from b *= x;
		 e--;
	 }
	 return b;
 }

I see, I added an extra unneeded variable to the function, i.e. over complicating things. I still havent been able to figure out why the function taylor2 fails to return a value when rad is over 2, I suspected it had something to due with long vs double vs float but ive come up empty handed there as well.
I believe it is because fabs(temp) will always be >= .0001, fabs(-4.5785) would be 4.578500, therefore it is > .0001, an infinite loop.

edit: That doesn't explain why it wouldn't loop for values below 2 however, maybe someone else has a better idea of that?
Last edited on
Topic archived. No new replies allowed.