Is this how you calculate sine?

I've come here for confirmation. The question is in two parts, I've done the first part (calculate exponent) and I've attempted the second part (calculate sine). I don't know what a sine is, as I haven't reached that part in my school maths, so I googled a quick formula.

Here is the question: For the second function you will compute the sine of an angle. Your function should accept the opposite and hypotenuse lengths and return the sine value.

Have I done the second part correctly (calculate sine)? This is how the formula should work to calculate sine right?

Thank you in advance.

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
 #include <iostream>
using namespace std;

int pow(int base, unsigned int exponent)
{
	if (exponent >= 0)
	{
		int result = 1;
		for (int i = 0; i < exponent; ++i)
		{
			result *= base;
		}
		return result;
	}
	else if (exponent == 0)
	{
		return 1;
	}

}

int calcSine(int opp, int hypo)
{
	int sin;
	sin = opp / hypo;
	return sin;
}

int main()
{
	int x, y;
	int a, b;

	cout << "Enter base value: " << endl;
	cin >> x;

	cout << "Enter exponent value: " << endl;
	cin >> y;

	cout << pow(x, y) << endl;

	system("CLS");

	cout << "Enter opposite length: " << endl;
	cin >> a;

	cout << "Enter hypotenuse: " << endl;
	cin >> b;

	cout << "Sine: " << calcSine(a, b) << endl;


	system("PAUSE");
	return 0;
}
closed account (48T7M4Gy)
The logic is right but double or float instead of int is required for all the variables involved, perhaps with the exception of opp, but I wouldn't bother.

(For a 3,4, 5 right angle triangle, the sine of the smaller acute angle is 3/5 = 0.6. If you use ints then sine = 0 because of integer division has no fractional part)
Last edited on
Thank you!
Hi,

Be careful with line 2. Both pow and sin exist in the std namespace, the minute you #include <cmath> , you will have a problem, well not a problem - just that you will call the library functions not yours. The best thing is to avoid line altogether, and put std:: before each std thing.

As I explained in one of you other Topics, you have an overlap with your logic on lines 6 and 15, although it doesn't matter here make a difference to the answer, avoid doing that, the code from line 15 to 18 is unreachable. Change line 6 to be >= 1

And the other warning. Warnings are there for a reason, ignore them at your peril.

 
In function 'int pow(int, unsigned int)': 
8:15: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] 
11:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
The real quastion is: how to calculate the sine when you don't know the sides of a triangle, but only the angle?

Here is one simple answer: use sin function from math.h
Topic archived. No new replies allowed.