C++ 输出中产生的随机数(C+;+;)
我正在从文本文件中读取数据并运行下面的代码。然而,我得到了mu输出中的随机数以及正确的数字。我不知道这些随机数是从哪里来的。 我的文本文件包括以下内容: 弗雷德·布鲁20 哈里蓝35 托尼·怀特43 希尔达蓝12 保罗·怀特34 汤姆·怀特20岁 我的输出内容如下: 该程序读取文件bowling.txt中的行,以确定保龄球比赛的获胜者。获胜团队、成员和分数显示在监视器上。 获胜队伍:怀特 球员得分 托尼43 保罗34 汤姆20 0 4197592 0 -1501340552 62 6298312 0C++ 输出中产生的随机数(C+;+;),c++,arrays,C++,Arrays,我正在从文本文件中读取数据并运行下面的代码。然而,我得到了mu输出中的随机数以及正确的数字。我不知道这些随机数是从哪里来的。 我的文本文件包括以下内容: 弗雷德·布鲁20 哈里蓝35 托尼·怀特43 希尔达蓝12 保罗·怀特34 汤姆·怀特20岁 我的输出内容如下: 该程序读取文件bowling.txt中的行,以确定保龄球比赛的获胜者。获胜团队、成员和分数显示在监视器上。 获胜队伍:怀特 球员得分 托尼43 保罗34 汤姆20 0 4197592 0 -1501340552 62 6298312
#包括
#包括
#包括
#包括
使用名称空间std;
//函数sumArray()和printArray()的声明
int-sumArray(int[],int);
字符串打印数组(字符串,字符串[],int[],int);
//主程序声明
int main()
{
字符串蓝色_成员[10],白色_成员[10];
int蓝色_分数[10],白色_分数[10];
//1)连接到输入文件
ifstream fin(“bowling.txt”);
//在下面声明数组
弦乐队成员;
智力得分;
//2)将阵列累加器初始化为零
int蓝色=0;
int-white=0;
//3)显示描述性消息
cout>团队>>得分;
//5)测试ifstream.eof()条件
而(!fin.eof())
{
//6)测试团队颜色为蓝色
如果(团队==“蓝色”)
{
//7)然后存储蓝色成员并打分
蓝色分数[蓝色]=分数;
蓝色成员[蓝色]=成员;
//8)增加蓝色数组累加器
蓝色++;
}
//9)其他存储白色成员和分数
其他的
{
白色分数[白色]=分数;
白人成员[白人]=成员;
//10)增加白色阵列累加器
白色++;
}
//11)尝试从文件中输入下一行
fin>>成员>>团队>>得分;
}
//12)如果蓝队得分较大
if(SUMRARY(蓝色分数,蓝色)>SUMRARY(白色分数,白色))
{
//13然后显示蓝色团队作为团队的赢家
打印阵列(“蓝色”,蓝色成员,蓝色分数,10);
}
//14)否则显示白色团队作为团队的赢家
其他的
{
printArray(“白色”,白色成员,白色分数,10);
}
}
//实现下面的函数sumArray()
int-sumArray(int-array\u-name[],int-array\u-end)
{
//1.将累加器初始化为0
整数和=0;
//2.循环初始化数组索引
for(int i=0;i cout问题在于,当数组中没有10个有效条目时,您要求程序打印出数组的所有10个成员。这就是为什么最后会出现随机数
你需要改变(例如)
到
变量white
是white\u成员
和white\u分数
数组中的有效条目数
蓝色数组也有相同的更改。您应该回答这个问题,将输出作为文本而不是图像。但是,您应该查看传递给print\u array
的数字,了解要打印的数组的长度。谢谢!我会将输出作为文本。
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
// declaration of functions sumArray() and printArray()
int sumArray(int [], int);
string printArray(string, string [], int [], int);
// declaration of main program
int main()
{
string blue_members[10], white_members[10];
int blue_scores[10], white_scores[10];
// 1) connect to the input file
ifstream fin("bowling.txt");
// declare arrays below
string Team, Member;
int Score;
// 2) initialize array accumulators to zero
int blue = 0;
int white = 0;
// 3) display a descriptive message
cout << "This program reads the lines from the file bowling.txt to determine\n"
<< "the winner of a bowling match. The winning team, members and scores\n"
<< "are displayed on the monitor.\n\n";
// 4) attempt to input the first line of the file
fin >> Member >> Team >> Score;
// 5) test ifstream.eof() condition
while (!fin.eof())
{
// 6) test team color is blue
if (Team == "Blue")
{
// 7) then store blue member and score
blue_scores[blue] = Score;
blue_members[blue] = Member;
// 8) increase blue array accumulator
blue++;
}
// 9) else store white member and score
else
{
white_scores[white] = Score;
white_members[white] = Member;
// 10) increase white array accumulator
white++;
}
// 11) attempt to input next line from file
fin >> Member >> Team >> Score;
}
// 12) if blue team score is larger
if (sumArray(blue_scores, blue) > sumArray(white_scores, white))
{
// 13 then display blue team as winner with the team
printArray("Blue", blue_members, blue_scores, 10);
}
// 14) else display white team as winner with the team
else
{
printArray("White", white_members, white_scores, 10);
}
}
// implement function sumArray() below
int sumArray(int array_name[], int array_end)
{
// 1. initialize accumulator to 0
int sum = 0;
// 2. loop over initialized array indices
for (int i = 0; i < array_end; i++)
// 3. increase accumulator by indexed array element
sum += array_name[i];
// 4. return accumulator
return sum;
}
// implement function printArray() below
void printArray(string team_name, string array_name [], int array_score [], int array_end)
{
// 1. display the team name as the winner
cout << setw(1) << "Winning Team: " << team_name<< endl;
cout << setw(5) << "Player" << setw(7) << "Score" << endl;
// 2. loop over initialized array indices
for (int i = 0; i < array_end; i++)
{
// 3. display member and score for that array index
cout << setw(3) << array_name[i]
<< setw(6) << setfill(' ') << array_score[i]
<< endl;
}
}
printArray("White", white_members, white_scores, 10);
printArray("White", white_members, white_scores, white);