gpt4 book ai didi

c++ - 从返回 int c++ 的函数中获取二维数组

转载 作者:行者123 更新时间:2023-11-28 03:39:04 25 4
gpt4 key购买 nike

我有一项模拟骰子游戏的作业。作为程序的一部分,用户输入要掷骰子的数量和掷骰子的次数。如果用户掷 4 个骰子,程序应将 4 个值相加,将结果存储在数组中,然后按照用户定义的次数重做程序。主要代码和函数原型(prototype)由导师定义,不可修改。我们必须编写函数。

在main的第3步中,有两个for循环。内部 for 循环调用相关函数。二维数组 rollSums[][] 被分配给函数的结果。该数组将在另一个函数中使用。我不知道如何从函数中正确填充二维数组。代码和我对该功能的尝试如下:

#include <iostream>
#include <iomanip>
#include <cstdlib> // needed for functions srand() and rand()
#include <ctime> // needed for function time()
#include <cmath> // needed for sqrt()

using namespace std;

const int MAXNUMTOROLL=10;
const int MAXROLLS=100;


int rollDice(int diceVals[], int numToRoll);

int main()
{
int sum;
int rollSums[MAXNUMTOROLL][MAXROLLS];
int diceVals[MAXROLLS];
double mean[MAXNUMTOROLL], std[MAXNUMTOROLL];
int numToRoll, numRolls;

srand(time(NULL));

// STEP 1: Ask user to input the maximum number of dice to use:

cout << "Please enter the maximum number of dice to use:" << endl;
do
{
cin >> numToRoll;
} while (numToRoll < 0 || numToRoll > MAXNUMTOROLL);

cout << "Please enter the number of rolls:" << endl;

// STEP 2: Ask user to input the number of rolls to carry out:
do
{
cin >> numRolls;
} while (numRolls < 0 || numRolls > MAXROLLS);

// STEP 3: For k=1 to numToRoll, simulated numRolls rolls of the dice
// and store the sum of the numbers rolled in the array rollSums[][]

for (int k=1;k<=numToRoll;k++)
{
for (int i=0;i<numRolls;i++)
{
rollSums[k-1][i] = rollDice(diceVals, k);
}
}

return 0;
}

int rollDice(int diceVals[], int numToRoll) //function simulating throwing of dice
{
int sum=0;
int i=0;

for(i=0;i<numToRoll;i++)
{
diceVals[i]=1+rand()%6;
sum=sum+diceVals[i];
}
return sum;
}

最佳答案

adohertyd,请参阅我在代码示例中的注释:

#include <iostream>
#include <iomanip>
#include <cstdlib> // needed for functions srand() and rand()
#include <ctime> // needed for function time()
#include <cmath> // needed for sqrt()

using namespace std;

const int MAXNUMTOROLL=10;
const int MAXROLLS=100;
const bool show_debug = true;

int rollDice(int diceVals[], int numToRoll);

int main()
{
int roll_Sums[MAXNUMTOROLL];
int diceVals[MAXROLLS];
//double mean[MAXNUMTOROLL], std[MAXNUMTOROLL];
int numDice, numThrows;

//Initialize random number generator with the current time
srand(time(NULL));

// STEP 1: Ask user to input the maximum number of dice to use:
cout << "Please enter the maximum number of dice to use:" << endl;

// STEP 2: Validate number of dice input
do
{
cin >> numDice;
} while (numDice < 0 || numDice > MAXNUMTOROLL);

//STEP 3: Ask user to input the number of times to throw each dice
cout << "Please enter the number of rolls:" << endl;

// STEP 4: Validate number of throws input
do
{
cin >> numThrows;
} while (numThrows < 0 || numThrows > MAXROLLS);

cout << "\n\nThrowing Dice Now...\n\n";

// STEP 5: Roll the dice
//The loop deals with each dice
for (int diceCount = 0; diceCount < numDice; diceCount++)
{
//The function call deals with all the throws per dice
//Note: roll_Sums array didn't need to be two dimensional,
// also, rollDice gets passed diceVals[] by value and the number of throws to execute
roll_Sums[diceCount] = rollDice(diceVals, numThrows);

//Debug output
if(show_debug)
{
//Since roll_Sums is zero based, add one to the visible index so the user doesn't get confused :P
cout << "Roll Sum for dice #" << diceCount + 1 << ": " << roll_Sums[diceCount] << endl << endl;
}
}

return 0;
}

//rollDice() returns the sum of all the dice rolls it performed
int rollDice(int diceVals[], int numToRoll)
{
int sum=0;

for(int i=0;i<numToRoll;i++)
{
//Get your random dice rolls
diceVals[i]=1+rand()%6;

//Debug output
if(show_debug)
{
cout << "Dice Roll # " << i+1 << ": " << diceVals[i] << endl;
}

//Accumulate your value, e.g. "sum"
sum += diceVals[i];
}

return sum;
}

关于c++ - 从返回 int c++ 的函数中获取二维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9912157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com