显示时间的正面和反面被扔C++
display amount of time heads and tails was tossed C++
我不得不为学校编写一个C++程序,我让它工作,但我无法弄清楚如何让它保持值以显示正面被抛出和反面被抛出的次数。它一直说0。这些是方向:编写一个模拟抛硬币的C++程序。 对于每次抛硬币,程序应打印正面或反面。 程序应该抛硬币 100 次。 计算硬币每面出现的次数,并在100次掷出结束时打印结果。
该程序至少应具有以下功能:
void toss(( - 从main((调用,将随机抛硬币并设置一个等于硬币表面的变量 void count(( - 从投掷到正面或反面的增量计数器调用 void displayCount(( - 从main((调用,将显示头部计数器的值和 tails.NO GLoBAL变量的计数器值!!
以下是代码:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
//prototypes
void toss(int headCount, int tailCount);
void count(int headCount, int tailCount);
void displayCount(int headCount, int tailCount);
//tosses a coin 100 itmes and tells you how many were heads and how many were tails.
int main()
{
int headCount = 0;
int tailCount = 0;
int count = 0;
toss(headCount, tailCount); //toss coin
displayCount(headCount, tailCount); //displays how many heads and tails
}
//***************function definitions***************
void toss(int headCount, int tailCount)
{
srand(time(NULL));//makes the coin toss random
for (int i = 0; i<100; i++) //while it is less then 100 tosses
{
if (rand() % 2 == 0) //assigns 0 to heads
{
cout << "heads";
}
else
{
cout << "tails"; //assigns 1 to tails
}
}
count(headCount, tailCount);
}
void count(int headCount, int tailCount)
{
if (rand() % 2 == 0)
{
headCount++; //counts the amount of heads
}
else
{
tailCount++; //counts the amount of tails
}
}
void displayCount(int headCount, int tailCount) //displays count of head and tails
{
cout << "Number of heads: " << headCount << "n";
cout << "Number of tails: " << tailCount << "n";
}
void toss(int headCount, int tailCount)
是按值传递的,而不是引用的,所以toss
中发生的事情会保留在toss
中。给
void toss(int &headCount, int &tailCount)
尝试一下,然后将相同的思维应用于count
.
更深入地观察,count
应该在toss
的for
循环中调用,否则它只会运行一次。这意味着需要重新考虑for
循环的当前内部。我会完全放弃count
函数并使用类似的东西:
void toss(int &headCount, int &tailCount)
{
srand(time(NULL));//makes the coin toss random.
// Well... Sorta. More on that in a second.
for (int i = 0; i<100; i++) //while it is less then 100 tosses
{
if (rand() % 2 == 0) //assigns 0 to heads
{
cout << "headsn";
headCount++; //counts the amount of heads
}
else
{
cout << "tailsn"; //assigns 1 to tails
tailCount++; //counts the amount of tails
}
}
}
还有一个旁注:
srand
最好在程序靠近main
顶部的地方调用一次。每次调用srand
时,它都会重置随机数生成器。如果您再次呼叫srand(time(NULL));
太快,时间将没有时间更改为下一秒,您将生成相同的号码。在少数情况下,您确实需要为每个程序多次调用srand
,rand
和srand
可能不是适合这项工作的工具。查看 C++11 或第三方随机数工具中添加的<random>
库。
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 字符串-C++后显示的随机字符
- 继承期间显示未知行为的子类
- 仅使用绝对值对数组进行排序,并在C++中显示实际值
- 程序崩溃并显示"std::out_of_range"错误
- 如何在C++中用std::cout正确显示带十六进制的字符串文本
- 为什么在C的循环中使用printf的Rust代码不显示输出,而在C++的循环中显示std::cout
- 从数据库实时显示QT c++中的数据
- 当使用比格式支持的精度更高的精度来显示数字时,会写出什么数据
- 显示错误输出的简单数组排序程序
- Qt自定义QPush按钮未显示在布局上
- C++射线示踪剂ppm表示没有足够的数据来显示图像
- 显示基于用户输入的整数的字符
- 使用QTreeView,如何通过调用函数只突出显示特定的行/列
- 应用程序崩溃并显示"symbol _ZdlPvm, version Qt_5 not defined in file libQt5Core.so.5 with link time reference"
- 示例外壳应用程序显示的 V8 "segmentation fault (core dumped)"错误
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 密码登录程序将永远循环并显示不正确的结果
- std::strftime 在 Windows 中使用 GMTIME 将时区 UTC 显示为 UTC+1