3 つのチームに対して 3 つのランダム スコアを生成し、どれが最大かを判断しようとしています。私のアプローチはPredfined
、プログラマ定義関数である関数を使用し、関数を宣言して定義することです。私はこれに非常に慣れていないので、購入したこの本はあまり役に立ちません。
コード側の目的の概要は次のとおりです。
事前定義された関数を呼び出して一連の乱数を生成する
値を返す関数を宣言して定義する
プログラマ定義関数を呼び出します。
最終的な目標 (本から引用):
max
タイプの 3 つのパラメーターを取りint
、パラメーターの最大値を返すという名前の関数を作成し ます。プログラムには、この関数の宣言と定義の両方が必要です。関数宣言は、関数の上に配置する必要がありmain
ます。次のことを行う関数を書き
main()
ます。を。Hoosier、Boilermakers、Fighting Irish の 3 つのチームのそれぞれのスコアとして 10 から 40 までのランダムな整数を生成し、これらのスコアを出力します。プログラムは、異なる時間に実行されると、異なる一連のスコアを生成できなければなりません。
b. タスク 1 で定義した関数を呼び出して、
max
すべてのチームの最大スコアを見つけ、見つかった最大スコアを出力します。c. 最大のスコアを Hoosier のスコアと比較し、「Go Hoosier!!!」と出力します。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:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2143: syntax error : missing ';' before 'if'
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.cpp(34): error C2563: mismatch in formal parameter list
1>c:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.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.0\vc\include\ostream(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:\users\mackiller\documents\visual studio 2010\projects\lab9\lab9\lab9.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.0\vc\include\ostream(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