当前位置: 首页 > 知识库问答 >
问题:

输出中产生的随机数(C++)

狄彬彬
2023-03-14
#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;
    }
}

共有1个答案

谭昕
2023-03-14

问题是,当数组中没有10个有效条目时,您正在要求程序打印出数组的所有10个成员。这就是为什么你会在末尾得到随机数。

你需要改变(例如)

printArray("White", white_members, white_scores, 10);

printArray("White", white_members, white_scores, white);
 类似资料:
  • 随机数有着广泛的用途。比如测试、游戏、仿真以及安全等领域都需要用到随机数。标准库所提供的多种可供选择的随机数产生器也恰恰反应了随机数应用范 围的多样性。随机数产生器由引擎(engine)和分布(distribution)两部分组成。其中,engine用于产生一个随机数序列或者伪随机数 序列;distribution则将这些数值映射到位于固定范围的某一数学分布中。关于分布的例子有:unifrom_i

  • 问题内容: 我正在寻找一个随机数,并将其发布到特定user_id的数据库表中。问题是,相同的数字不能使用两次。有上百万种方法可以做到这一点,但是我希望对算法非常热衷的人能够以一种优雅的解决方案巧妙地解决问题,因为它满足以下条件: 1)最少查询数据库。2)通过内存中的数据结构进行的爬网次数最少。 本质上,这个想法是要执行以下操作 1)创建一个从0到9999999的随机数 2)检查数据库以查看该数字是

  • 下面要介绍一个在模拟事件和游戏的程序中常用的组件。本节和下节开发一个结构良好、包括多个函数的游戏程序。程序中要使用前面介绍的大多数控制结构。 在赌场上,人人都关心的一个问题就是机会元素(element of chance),也就是赢钱的运气。这个机会元素可以用标准库中的rand函数引入计算机应用程序中。 考虑下列语句: i=rand(); rand 函数产生O到RAND_MAX之间的整数(这是<s

  • 这个程序创建了一个名为datafile.txt的文件,并且应该使用文本I/O将100个随机创建的整数写入文件中。但是,我的输出是“java.util.Random@30c221”100次。我如何得到100个随机数?提前谢谢。

  • 本文向大家介绍C#产生随机双,包括了C#产生随机双的使用技巧和注意事项,需要的朋友参考一下 示例 生成介于0和1.0之间的随机数。(不包括1.0)            

  • 本文向大家介绍C#产生一个随机整数,包括了C#产生一个随机整数的使用技巧和注意事项,需要的朋友参考一下 示例 本示例生成0到2147483647之间的随机值。