如何在c ++中计算pi?没有编程背景 [英] How do I compute pi in c++? No programming background

查看:85
本文介绍了如何在c ++中计算pi?没有编程背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用u = 2和v = 1 / sqrt(2)计算实际pi的10 ^ -5并使用u / v替换u并使用sqrt((1 + v)/ 2替换v来计算c ++中的pi )每次迭代。我没有编程背景!这就是我得到的:





  #include   <   iostream  >  
#include < math.h >

使用 命名空间标准;

int main()
{
float U,v;

u = 2 ;
v = 1 / sqrt( 2 );







请帮助!!!



明天到期

解决方案

你的显示的代码不完整。

但这不是重点...

你的作业是为了帮助你学习。

如果我们给你一个解决方案,你不会思考解决问题。



我会给你一个提示:

类型 double 将提供比更精确的浮动



编辑:

既然你暗示这不适用于c ++编程课程,但是你需要有一些编程背景,这里是程序的迭代部分。

< pre lang =c ++> #include < < span class =code-leadattribute> iostream >
#include < math.h >

使用 命名空间标准;

int main()
{
float 你,v,prevu;

u = 2 ;
v = 1 / sqrt( 2 );
prevu = 0 ;
while (fabs(prevu - u)> 10e-6f)
{
prevu = u;
u = u / v;
v = sqrt(( 1 + v)/ 2);
}
// 这里你将包含pi的近似值
// 以下是输出结果的位置
}



我的c ++ I / O非常生锈,抱歉。


这是我最终想出来的。绝对不是最好的方法,但绝对不是我能用我非常有限的知识做的。



  #include   <    < span class =code-attribute> iostream  >  
#include < math.h > //这样可以使用sqrt函数

使用 命名空间标准;

int main()
{
float 你,v,prevu;

u = 2 ;
v = 1 / sqrt( 2 );
prevu = 0 ;

while (fabs(prevu-u)> 10e-6f) // fabs:浮点绝对值函数。 f:表示它是一种浮动类型
{
prevu = u;
u = u / v;
v = sqrt((1 + v)/ 2);
}
cout<< pi~<<你<< ENDL;
}


显然我忘了提及当我输入我的问题时,程序所在的代码是否无关紧要(新的网站记得)但我更喜欢c或c ++而不是其他原因我看过的少量东西,并且理解它是用c ++和c ++而c是我下学期要学习的内容以及有些人试图解释的内容对我来说。





 #include< math.h>  //  使用平方根函数 
#include< stdio.h> // 使用print

int main()
{
float u = 2 0 ;
float v = 1 / sqrt(( float 2 0 );

printf( Pi approximation\\\
\ n
); // 打印输出

for int i = 0 ; i < 12 ; i ++)
{
u = u / v;
v = sqrt(( 1 + v)/ 2 );
printf( %f \ n,u); // 将每个循环打印到屏幕上
}

的getchar();
return 0 ;
}


How do I compute pi in c++ withing 10^-5 of actual pi using u=2 and v= 1/sqrt(2) and replacing u with u/v and v with sqrt((1+v)/2) each iteration. I have no programming background! This is all I got:


#include <iostream>
#include <math.h>

using namespace std;

int main()
{
  float u,v;

  u=2;
  v=1/sqrt(2);




PLEASE HELP!!!

It is due tomorrow

解决方案

Your code shown is incomplete.
But that is beside the point...
Your homework is given to help you to learn.
If we give you a solution you will not have the "think through" solving the problem.

I'll give you a HINT:
type double will give more precision than float

EDIT:
Since you imply this is not for a c++ programming course, but that you were expected to have some programming background, here is the iteration part of the program.

#include <iostream>
#include <math.h>

using namespace std;
 
int main()
{
  float u,v, prevu;
 
  u=2;
  v=1/sqrt(2);
  prevu = 0;
  while (fabs(prevu - u) > 10e-6f)
  {
    prevu = u;
    u = u/v;
    v = sqrt((1 + v)/2);
  }
  // Here u will contain the approximation to pi
  // Here is where the result should be output
}


My c++ I/O is very rusty, sorry.


Here is what I finally came up with. Definitely not the best way by no means but best I can do with my very limited knowledge.

#include < iostream>
#include < math.h> //so that sqrt function may be used
 
using namespace std;
 
int main()
{
float u,v, prevu;
 
u=2;
v=1/sqrt(2);
prevu = 0;
 
while (fabs(prevu-u) > 10e-6f) //fabs: floating point absolute value function. f: means it's a type of float
{
prevu = u;
u=u/v;
v=sqrt((1+v)/2);
}
cout << " pi ~ " << u << endl;
}


Apparently I forgot to mention if didn't matter which code the program was in at the beginning when I entered my question (new to site remember) but I prefer c or c++ over the others cause of the small amount of stuff I've looked at and understood it was in c++ and c++ and c is what I will be taking next semester and what some people have tried to explain to me.


#include <math.h>               // to use square root function
#include <stdio.h>              // to use print

int main()
{
    float u = 2.0;
    float v = 1/ sqrt((float)2.0);

    printf("Pi approximation\n\n");  //prints output

    for (int i = 0; i < 12; i++)
    {
        u = u/v;
        v = sqrt((1 + v) / 2);
        printf("%f\n", u);      //prints u for each loop onto screen
    }

    getchar();
    return 0;
}


这篇关于如何在c ++中计算pi?没有编程背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆