Nested loops think process assistance

Feb 10, 2019 at 8:04pm
hello there, I was recently asked to code the sine of a given value using the Taylor series.

FYI before getting mad at me:
Yes, it was an assignment given to me.
I don't expect anyone to give me just a code.
I did this code from scratch in my head!

I'm having a hard time getting the twist of nested loops, writing this code took me hours just to figure out the right way to implement it.

When comparing with some classmates, I realized my code was actually cleaner than most of them.

SO my question is: can anyone give me some tips on how should I visualize those kinds of problem? when do we know it is necessary to have nested loops? could I have written a code giving the same result using only 1 loop?


thank you very much, kind sirs.


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
  #include<iostream>
#include<cmath>

using namespace std;
int main()
{
	double x=1;
	double y = 1;
	double sum = 0;
	double fact = 1;
	double sign = -1;
	
	







	cout << "please enter a value of x to calculate its sine";
		cin >> x;

	for (int i = 1; i < 100; i += 2)
	{
		
		fact = 1;
		
		

		for (int j = 1; j <= i ; j++)
		{
			
			fact *= j;
			   


		}



		sum += sign*(-1)*(pow(x, i)) / fact;
		sign *= -1;
		


		
	}

	cout << "\n\n\n" <<"SINE("<<x<<") = "<< sum << endl;
	return 0;
	
}


	
}


Last edited on Feb 10, 2019 at 8:18pm
Feb 10, 2019 at 8:14pm
I didn't get what you're trying to do? What is the sine?, do you mean the mathematical function sin()?

1
2
3
4
5
6
7
8
9
for (int i = 1; i < 100; i += 2)
{	
	fact = 1;

	for (int j = 1; j <= i ; j++) fact *= j;

	sum += sign*(-1)*(pow(x, i)) / fact;
	sign *= -1;	
}


What are you trying to do here?
Last edited on Feb 10, 2019 at 8:19pm
Feb 10, 2019 at 8:17pm
^ Im sorry,

yes it is the mathematical function sin().

I enter a value "x" and my program should compute the sine of this value, in radiant.

Last edited on Feb 10, 2019 at 8:18pm
Feb 10, 2019 at 8:21pm
You have a sin() function in cmath, you know that, right? Or were you asked to manually code a sin function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<cmath>

using namespace std;
int main()
{
	double x;
	
	cout << "please enter a value of x to calculate its sin : ";
	cin >> x;

	cout << "\n\n\n" <<"sin("<<x<<") = "<< sin(x) << endl;
	
	return 0;	
}
Last edited on Feb 10, 2019 at 8:23pm
Feb 10, 2019 at 8:28pm
yeah I know, I used <cmath> to use the pow function

Or were you asked to manually code a sin function?


exactly
Last edited on Feb 10, 2019 at 8:28pm
Feb 10, 2019 at 8:38pm
What formula of the sine function are you using? Sine can calculate many things, angles in triangles, in trigonometry, limits etc..
Feb 10, 2019 at 8:43pm
The Taylor expansion of the function sin(x) around the point x = 0 is given by:

sin(x) = x − x^3/ 3! + x^5/- 5! − x^7/ 7! + ⋯

Write a C++ program that takes a real value of x and computes the sine of that number (up to a
certain precision).


in my code for instance, my precision is set in my first for loop (i<100)
Last edited on Feb 10, 2019 at 8:44pm
Feb 10, 2019 at 9:06pm
I don't know what the '!' are for in your formula but I don't think you would have needed a nested loop.

Considering you chose a limit of 100:

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
//forward declaration
double sin(double, int);
int factorial(int);

//definition, limit is here equal to 100
double sin(double num, int limit)
{
    double result = num;
    bool sign = false;    //(true for positive and false for negative)

    for (int count = 3; count < limit; count += 2)
    {
        if (sign)
            result += (num + (pow(num, count) / factorial(count)));
        else
            result -= (num + (pow(num, count) / factorial(count)));

        sign = !sign;
    }

    return result;
}

int factorial(int num)
{
    int total = num;

    for (int count = num - 1, count > 0; count--)
    {
        total *= count;
    }

    return total;
}
Last edited on Feb 10, 2019 at 9:17pm
Feb 10, 2019 at 9:11pm
"!" represent the factorial of the number
Last edited on Feb 10, 2019 at 9:11pm
Topic archived. No new replies allowed.