你如何计算平均值? [英] How do you calculate the average?

查看:130
本文介绍了你如何计算平均值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法获得十次投球的正确平均值,有人可以帮助我吗?



我尝试过的事情:



I cant get the right average of the ten throw, can somebody help me?

What I have tried:

#include<iostream>
#include<cmath>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{

	char ch;
	int i;
	double average,dice1,dice2,sum;

	cout<<"	This program calculates the average of the ten throws"<<endl<<endl;

	srand(time(NULL));
	for (i = 0; i <= 9; i++)
	{
		dice1= int((double)rand()/RAND_MAX*6)+1;
		dice2= int((double)rand()/RAND_MAX*6)+1;
		sum=(dice1+dice2);
		cout<<" dice1:    "<<dice1<<" 		dice2:   "<<dice2<<"		the sum:    "<<sum<<endl;

	}
	average=(sum)/10;
	cout<< "the average of the ten throws :"<< average<<endl;


	cout<<"\n\n\nEnter 'e' to exit... ";
	cin>>ch;

	return 0;
}</cstdlib></ctime></cmath></iostream>

推荐答案

尝试这种方式

Try this way
sum= 0;
for (i = 0; i <= 9; i++)
{
    dice1= int((double)rand()/RAND_MAX*6)+1;
    dice2= int((double)rand()/RAND_MAX*6)+1;
    sum +=(dice1+dice2);
    cout<<" dice1:    "<<dice1<<"         dice2:   "<<dice2<<"        the sum:    "<<sum<<endl;

}



总和没有累积。



你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]


sum was not accumulating.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于你如何计算平均值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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