C++最大的随机数生成器
C++ Largest Random Number Generator
我正在尝试为 3 支球队生成 3 个随机分数,并确定哪个是最大的。我的方法是使用 Predfined
函数,一个程序员定义的函数,并声明和定义该函数。我对这个很陌生,我买的这本书并没有真正帮助我。
以下是代码端的目标概述:
-
调用预定义函数以生成随机数序列
-
声明并定义返回值的函数
-
调用程序员定义的函数。
最终目标(取自书):
-
编写一个名为
max
的函数,该函数采用三个类型int
的参数并返回参数的最大值。程序必须同时具有此函数的声明和定义。函数声明必须放在main
函数的上方。 -
编写执行以下操作的函数
main()
:a. 生成一个介于 10 到 40 之间的随机整数作为三支球队中每支球队的分数Hoosier,Boilermakers和Fighting Irish,并打印出这些分数。您的程序必须能够在不同时间运行时生成不同的分数序列。
b.b. 调用任务 1 中
c. 将最大分数与Hoosier的分数max
定义的函数,以查找所有团队中的最大分数并打印出找到的最大分数。进行比较,如果Hoosier团队的分数等于所有团队的最高分数,则打印出"Go Hoosier!!"。
这是代码
/*
Author: Dan Wingeart
Assignment: Lab 9
*/
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int max(int Hscore, int Pscore, int Fscore);
int main()
{
int Fscore, Pscore, Hscore, highestScore;
Fscore = 10 + rand() % 40;
Pscore = 10 + rand() % 40;
Hscore = 10 + rand() % 40;
cout << "Prediction performance of sport teams:" << endl;
cout << "Team Hoosier's score is " << Hscore << endl;
cout << "Team Boilermakers' score is " << Pscore << endl;
cout << "Team Fighting Irish's score is " << Fscore << endl;
highestScore = max(Hscore, Pscore, Fscore)
if (max>Pscore&&max>Fscore){
cout << "The largest score is " << max << endl;
cout << "GO HOOSIER!!!" << endl;}
else
cout << "The largest score is " << max << endl;
return 0;
}
int max(int Hscore, int Pscore, int Fscore)
{
if (Hscore>Pscore&&Hscore>Fscore){
cout << Hscore;}
else if (Pscore>Hscore&&Pscore>Fscore){
cout << Pscore;}
else{
cout << Fscore;}
return 0;
}
由此产生的错误:
ClCompile:
1> Lab9.cpp
1>c:usersmackillerdocumentsvisual studio 2010projectslab9lab9lab9.cpp(34): error C2143: syntax error : missing ';' before 'if'
1>c:usersmackillerdocumentsvisual studio 2010projectslab9lab9lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:usersmackillerdocumentsvisual studio 2010projectslab9lab9lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:usersmackillerdocumentsvisual studio 2010projectslab9lab9lab9.cpp(35): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1>c:usersmackillerdocumentsvisual studio 2010projectslab9lab9lab9.cpp(38): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'overloaded-function' (or there is no acceptable conversion)
1> c:program files (x86)microsoft visual studio 10.0vcincludeostream(679): could be 'std::basic_ostream<_Elem,_Traits> &std::operator <<<char,std::char_traits<char>>(std::basic_ostream<_Elem,_Traits> &,const char *)'
1> with
1
//don't forget to like if the solution is working
//ask ban
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int max(int Hscore, int Pscore, int Fscore);
int main()
{
srand ( time(NULL) );//(1)
int Fscore, Pscore, Hscore, highestScore;
Fscore = 10 + rand() % 40;
Pscore = 10 + rand() % 40;
Hscore = 10 + rand() % 40;
cout << "Prediction performance of sport teams:" << endl;
cout << "Team Hoosrand ( time(NULL) );sier's score is " << Hscore << endl;
cout << "Team Boilermakers' score is " << Pscore << endl;
cout << "Team Fighting Irish's score is " << Fscore << endl;
highestScore = max(Hscore, Pscore, Fscore);
cout<<highestScore;
return 0;
}
int max(int a, int b, int c)
{
if(a>b && a>c)//we assume that a is the maximum. This means that a > b and a > c
{
return a;
}
else // if a is not maximum then we assume that b is maximum
{
if(b>c)
{
return b;
}
}
return c;// if a and b are not maximum then c is maximum
}
//(1)这个FCT将帮助您在每次运行程序时都有不同的随机数,更多INF在这里:http://www.cplusplus.com/reference/cstdlib/srand/
如前所述,max 函数需要返回一个并不总是 0 的 int。看起来就像您尝试在函数中使用 cout 在主文件中打印出来一样,但是它不是那样工作的。这会导致一些编译问题。
并且您在分配max结果的行尾缺少一个括号"最高分"。更多编译问题。
此外,您将 max 函数的结果分配给"最高分数",但然后永远不要使用它。实际上,您正在使用函数"max"本身,该函数可能会导致更多的编译器问题。
这是我的镜头。
#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;
int max(int Hscore, int Pscore, int Fscore);
int main()
{
int Fscore, Pscore, Hscore, highestScore;
srand (time(NULL)); // New numbers each time.
Fscore = 10 + rand() % 40;
Pscore = 10 + rand() % 40;
Hscore = 10 + rand() % 40;
cout << "Prediction performance of sport teams:" << endl;
cout << "Team Hoosier's score is " << Hscore << endl;
cout << "Team Boilermakers' score is " << Pscore << endl;
cout << "Team Fighting Irish's score is " << Fscore << endl;
highestScore = max(Hscore, Pscore, Fscore);
if ( (highestScore>Pscore ) && (highestScore>Fscore) ){
cout << "The largest score is " << highestScore << endl;
cout << "GO HOOSIER!!!" << endl;
} else {
cout << "The largest score is " << highestScore << endl;
}
return 0;
}
int max(int a, int b, int c) {
int m = ( a > b ) ? a : b;
return ( m > c ) ? m : c;
}
max
函数的实现中存在问题。首先,无论max
是什么,您始终return 0
。但还有另一个问题。
让我们想象Hscore = 40
、Pscore = 40
、Fscore = 20
并查看函数。
int max(int Hscore, int Pscore, int Fscore)
{
if (Hscore>Pscore&&Hscore>Fscore){ // False, Hscore > Pscore is false since Hscore is even but not greater than Pscore
cout << Hscore;}
else if (Pscore>Hscore&&Pscore>Fscore){ // False, Pscore > Hscore is false since Pscore is even but not greater than Hscore
cout << Pscore;}
else{ // else statement gets executed even tho Fscore was the lowest.
cout << Fscore;}
return 0;
}
您可能应该在 max 函数中使用 >=
而不是 >
。它应该看起来像这样:
int max(int Hscore, int Pscore, int Fscore)
{
if (Hscore>=Pscore&&Hscore>=Fscore){
cout << Hscore;
return Hscore;
}
else if (Pscore>=Hscore&&Pscore>=Fscore){
cout << Pscore;
return Pscore;
}
else{
cout << Fscore;
return Fscore;
}
}
根据代码示例,我的印象是你拥有的书有点旧(它在函数的开头声明了所有变量,这是 1999 年之前的 C 语言所必需的)。 我强烈建议你买一本更新的书。 就个人而言,我会推荐加速C++或编程:使用C++的原则和实践(由C++的创建者)。
代码中最大的问题是简单的事实,即:
if (max>Pscore&&max>Fscore){
混淆max
(函数)和highestScore
(变量)。 您希望它是:
if (highestScore>Pscore&&highestScore>Fscore){
虽然,就个人而言,我会把它写成
if (highestScore == Hscore) {
除此之外,您可能有兴趣发现标准库在 <algorithm>
标头中具有函数 max
(返回两个项目中较大的一个)和max_element
(返回序列中最大的项目)。 您可以将max
替换为呼叫max
:
int highestScore = std::max(std::max(Hscore, Pscore), Fscore);
或致电max_element
:
int scores[] = { Pscore, Fscore, Hscore };
int highestScore = *max_element(scores, scores + 3);
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 制作具有平均值的随机数生成器
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- 尝试在 c++ 中创建随机数生成器并收到错误
- 存储并重新加载随机数生成器状态
- 作为随机数生成器的争用条件
- 播种随机数生成器以使用分叉的好方法是什么?
- 随机数生成器的性能因平台而异
- 随机数生成器的种子和状态有什么区别?
- RcppShark 随机森林示例引发有关随机数生成器的异常
- C++多个实例的随机数生成器组种子中增强
- C++随机数生成器仅生成 0 - C++ 11
- GSL+OMP:C++中的线程安全随机数生成器
- 贪吃蛇游戏 - 食物瓷砖的随机数生成器
- 随机数生成器在 C++ 用于 skipList.
- RSA 加密和解密期间的随机数生成器要求
- 如何使用 cpp 设置随机数生成器的语法?
- 产生相同结果的随机数生成器
- C++随机数生成器通常在 Visual Studio 中运行程序时在 2 次后停止生成数字
- 如何理解C++11随机数生成器