Is this flowchart right?

僤鯓⒐⒋嵵緔 提交于 2019-11-27 04:54:32

问题


I've developed a C program that can calculate the value of a sin function using Taylor series expansion. I've also drawn a flowchart for the program. The source code is given below:

#include<stdio.h>
#include<math.h>

int fact(int n)
{
 if(n==0)
  {
    return 1;
  }
 else
    return n*fact(n-1);
}

int main()
{
 int l,i,t=1;
 float deg,rad,val=0;

 printf("Enter degree of sin: ");
 scanf("%f",&deg);

 printf("Enter limit of Taylor series: ");  
 scanf("%d",&l);                            
 rad = (deg*3.142857)/180;                  

 for(i=1;i<=l;i+=2)
  {
   val = val + (t*pow(rad,i)/fact(i));     
   t = t*(-1);                         
  }

 printf("\nValue calculated by program, using Taylor Series:\n");
 printf("Sin(%f) = %f\n",deg,val);
 printf("\nValue calculated using library function:\n");
 printf("Sin(%f) = %f\n",deg,sin(rad));

 getch();
 return 0;
}

And here's the flowchart for the program:


So is this flowchart right for the program? Are there any mistakes? I am a fresher in programming and don't have a good knowledge in drawing flowcharts.


回答1:


The risk with using a factorial function is that it very quickly goes out of int range. There is no need to have either a power function or a factorial function, because each term of the Taylor series can be derived from the previous term, by using a multiplication and a division.

The multiplier is self-evident, simply the square of the angle.

The divisor is i * (i - 1), the next two terms of the factorial.

You will see I have removed your sign change factor t, because to change the sign of the previous term from neg to pos, or pos to neg, you just multiply by -1. But I have even removed that by reversing the sign of (i - 1) with my use of (1 - i).

The first term of the series is simply rad so I start with that.

#include <stdio.h>
#include <math.h>

int main()
{
    int n, i;                                       // don't use `l` for a variable name 
    float deg, rad, radsq, val, term;

    printf("Enter degree of sin: ");
    if(scanf("%f", &deg) != 1) {
        return 1;                                   // or other error handling
    }

    printf("Enter limit of Taylor series: ");  
    if(scanf("%d", &n) != 1) {                           
        return 1;                                   // or other error handling
    }
    rad = deg * 3.14159265f / 180;                  // proper value for pi
    radsq = rad * rad;

    term = rad;                                     // first term is rad
    val = term;                                     // so is series sum
    for(i = 3; i <= n; i += 2)                      // we've done the first term
    {
        term *= radsq / (i * (1 - i));              // see explanation
        val += term;                                // sum the series
    }

    printf("\nValue calculated by program, using Taylor Series:\n");
    printf("Sin(%f) = %f\n", deg, val);
    printf("\nValue calculated using library function:\n");
    printf("Sin(%f) = %f\n", deg, sin(rad));

    return 0;
}



回答2:


No the flow charts in both case are wrong because 1) the for loop is in the body of main function and not in the body of fact function. 2) for the fact recursive function the correct flow chat would be here: http://improvec.blogspot.in/2010/12/flow-chart-for-recursive-function-of.html

now,i understand that u know the basic flow chart of for loop connect try again for the main function and connect the two flow charts.



来源:https://stackoverflow.com/questions/38263886/is-this-flowchart-right

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!