【问题标题】:Is this flowchart right?这个流程图对吗?
【发布时间】:2016-07-08 10:01:44
【问题描述】:

我开发了一个 C 程序,可以使用泰勒级数展开计算 sin 函数的值。我还为该程序绘制了流程图。源码如下:

#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;
}

这是程序的流程图:

那么这个流程图适合这个程序吗?有没有错误?我是编程新手,对绘制流程图没有很好的了解。

【问题讨论】:

  • 3.142857 是一个奇怪的 PI,如果n 大于 7,您的 fact 函数可能会溢出 int
  • 您既不需要幂函数也不需要阶乘函数。每一项都可以从前一项推导出来,只需简单的乘法、除法和符号更改。
  • 我明白了。对于在我的系统上大于 13 的数字的阶乘值形成的大量数字,事实函数可能会溢出。因此,我总是确保泰勒级数的极限不超过 13。那么 PI 呢,我应该使用 3.141592 来代替吗?
  • 你的 pi 是22/7
  • 如果您的程序正在运行,您可能需要通过Code Review 寻求批评。请务必先阅读A guide to Code Review for Stack Overflow users,因为那里有些事情的做法不同!

标签: c flowchart


【解决方案1】:

使用阶乘函数的风险在于它很快就会超出int 范围。不需要幂函数或阶乘函数,因为泰勒级数的每一项都可以通过乘法和除法从前一项推导出来。

乘数不言而喻,就是角度的平方。

除数是i * (i - 1),即阶乘的后两项。

您会看到我已经删除了您的符号更改因子t,因为要将前一项的符号从 neg 更改为 pos,或 pos 到 neg,您只需乘以 -1。但是我什至通过使用 (1 - i) 来反转 (i - 1) 的符号来消除它。

该系列的第一个词只是rad,所以我从它开始。

#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】:

    不,这两种情况下的流程图都是错误的,因为 1) for 循环在 main 函数体中,而不是在 fact 函数体中。 2)对于事实递归函数,正确的流聊天将在这里:http://improvec.blogspot.in/2010/12/flow-chart-for-recursive-function-of.html

    现在,我知道你知道了for循环连接的基本流程图,再试一次main函数并连接两个流程图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-21
      • 2021-09-26
      • 2010-12-26
      • 1970-01-01
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多