English 中文(简体)
如何将数学等值(假设)转换为一种代码? [闭门]
原标题:How to convert a math equation (summation) to a code? [closed]
  • 时间:2011-01-04 17:16:15
  •  标签:
  • c

利用两项职能:

  • Factorial Function
  • Power Function

Develop a C program for the following equation:

“alt

我这样做了,但没有产出。

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

double factorial(double x);
double Power_function(double y);

int main(){
  double answer=0;
  double n;
  double y;

  printf("Enter Y :The last limit of the summation:>");
  scanf("%ld",&y);
  for (n=1;n<=y;++n){
    answer=answer +factorial(n)*Power_function(n)*y;
  }

  printf("The Answer is %0.2f
",answer);
  return 0;
}

double factorial(double x)
{
  double ans;
  if (x==0){
    ans = 1;
  }
  else
  {
    ans = x*factorial(x-1);
  }
  return ans;
}

double Power_function(double y){
  double ans;
  ans=pow(2,y);
  return ans;
}
最佳回答

我没有做任何事情,但这里是一个基本大纲。

你们必须完成这项工作。

int answer = 0;
int n;
for(n=1; n <= y; ++n)
{
    answer = answer + (n! * 2^n)*y;
}
问题回答

我怀疑,这一学习成果之一是什么,这里的帮助太大了。

我认为,我可以安全地说,你需要打破每一部分的公式,将其分成小部分。

The three biggest parts are summation, N Factorial (N!) and 2 to the power N

传票实际上从1岁起至19岁,因此,C syntax要写假(有两种共同类型,而为)

其余两项职能是,如果你允许使用预设职能,那么lent光只剩下一条 go子,可以 sort笑你,如果不是你自己的话。

系数:n*(n-1)*(n-2)...(n-(n-2))*(n-(n-1))

∗∗∗∗∗

http://www.google.co.uk/search?q=recursion”rel=“nofollow” 或带有倾斜性的功能

2 ×2 ×

页: 1

页: 1

Once again a loop looks like a good place to start with that.

之后,它只是利用你在坡道内的两个功能,让用户有办法投入Y,并用一些数字进行测试。

1,2,3美元将是一个良好的开端,因为它们重新 n,很容易就计算器开展工作。

我 hope希望,1人应当对我所看到的迄今所展示的每一个例子作出令人感兴趣的事情,在我们开始把大船推向32人之前就这样做了。

Edit: Right, I ve taken a look at your code, and it looks like your problem isn t in the implementation of your algorithm, its not reading the variable in correctly, outputing y just after you set it yields 0, so something isn t working quite right there. Im not a c coder but i had a quick hack at it with some liberal googling and your non functional code and made it read an argument in then parse it to a double.

#include<stdio.h>
#include<math.h>
double factorial(double x);
double Power_function(double y);

main(int argc, char *argv[]){
  double answer=0;
  double n;
  double y;
  y = atol(argv[1]);
  //printf("%lf
",y);
  for (n=1;n<=y;++n){
    answer=answer +factorial(n)*Power_function(n)*y;
  }

  printf("The Answer is %0.2f
",answer);
  return 0;
}

double factorial(double x)
{
  double ans;
  if (x==0){
    ans = 1;
  }
  else
  {
    ans = x*factorial(x-1);
  }
  return ans;
}

double Power_function(double y){
  double ans;
  ans=pow(2,y);
  return ans;
}

因此,基本上来说,你的法典有不同的投入方法。

汇编

gcc so.c -lm

执行

./a.out 1 yields The Answer is 2.00

./a.out 2 yields The Answer is 20.00

./a.out 3 yields The Answer is 174.00

纸浆和纸面数字支持这一方法,因此,迄今为止,你的算法是健全的!

要求看到来自这里的一些负面数字和大量的数字!

页: 1

total variable is 0  
for 1 until y:  
multiply factorial(n) and pow(2, n) and add this to total
end of loop
print out total

首先要做的是,写一下你人工做些什么来完成这一类工作。 例如,请看一至五点,看你会得到什么。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签