Problem with Modularity Program...

I've been working on a homework problem for my engineering computer sciences class although am having trouble finding the error in my program and was hoping someone here might be able to spot it given the appropriate information. This is my first project using modules so far and i'm decently sure that I have the factorial set up properly although when I run the program it asks for the value in degrees then does nothing.

So far I assume the problem is in my loop although I am not sure how else I could state it give then problem I am to solve (I listed it in the block comment). Any help here will be greatly appreciated.

Here is the code:
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
/*
Programmer: John Lee
Date: October 2, 2008
Objective: To find how many terms are needed to approximate the value returned by the intrinsic sin() function with an error less than 1 e-6, when x=30 degrees.
Formula Given: sinx=-(x^3)/3! +(x^5)/5! -(x^7)/7! +(x^9)/9!...
*/

#include <iostream>
#include <cmath>
using namespace std;

int factorial(int a)                        //create the function factorial for use in formual
{
    double factorial=1;
    int i;
    for(i=1; i<=a; i++)
    {
             factorial = (factorial * i);
    }
    return (factorial);
}

int main()
{
    float valueDegree, valueRadian, sinValue, sum;
    int count;
    sum = 0;
   
    cout << "Enter the value of an angle in degrees.\n\t";	//input from user for degrees
    cin >> valueDegree;
  
    valueRadian = (valueDegree/0.0174532925); //convert the given degrees to radians so it can be used in the formula
    sinValue = sin(valueRadian);
   
    do
    {
          for (count=1; count>0; count++)	//formulas set up for even/odd terms due to +/-
          {
              if ((count %2)>0)
              sum = (sum + (-pow(count,(2.0*count +1.0)) / factorial(2*count + 1)));
             
              else
              sum = (sum + (pow(count,(2.0*count +1.0)) / factorial(2*count + 1)));            
          }
    }
    while ((sinValue-sum) >= pow(10,-6.0));	//stop the loop when the difference between the two is less than 1 e-6
   
    cout << "The number of terms needed to aproximate the value returned by sin(x) is " << count << ".\n";	//display the # of terms it took to get within desired accuracy
    return 0;
}


I am using Dev-C++ for the first time while I download the client for Visual Studio although I somehow doubt the difference compiler wise is causing the problem.
It's because your for loop is an infinite loop. You should just put count = 1 outside of the do/while loop, and do the incrementing yourself, because as it is now, your while loop will run until count goes above INT_MAX and loops around.
That makes sense, although i'm not sure how I can setup the loop to do accumulated sum until the difference between the two is less that 1 e-6. I mean I still need conditions in the statement
for (count=1; count>0; count++)
although I am not sure how high it will be required to go. I could set a random number bigger than the number of terms for sin(90) since that is the largest value sin(x) can equal although I would much prefer a proper loop. Any suggestions?
Sorry for that last post, I wasn't thinking properly lol. I have fixed the code:
1
2
3
4
5
6
7
8
9
10
11
12
sum = count;
    count = 1;
    do
    {
          if ((count %2)==0)
          sum = (sum + (pow(count,((2.0*count) +1.0)) / factorial((2*count) + 1))); //odd terms
          
          else
          sum = (sum + (-pow(count,((2.0*count) +1.0)) / factorial((2*count) + 1)));  //even terms
          count++;
    }
    while ((sinValue-sum) >= pow(10,-6.0));


Thanks for pointing out my simple error :)
Topic archived. No new replies allowed.