int mostLandings(vector<int> &v,int startInt,int endInt) { int i = 0; int numDic
ID: 643665 • Letter: I
Question
int mostLandings(vector<int> &v,int startInt,int endInt) {
int i = 0;
int numDice = 2;
int sidesOnDice = 6;
int Index = 0;
int score = 0;
int MostLandings = 0;
do {
score = score + rollNdice(numDice, sidesOnDice) + startInt;
if (score <= endInt ) {
v.at(i) = score - 1;
}
++i;
} while(v.at(i) <= endInt);
if (Index == v.at(0)) {
++MostLandings;
}
Index = v.at(0);
return Index;
Above is a part of my code to find the most landings on a board game.
Here is a description of what I am to find.
rollNDice
simulates rolling N dice, each of which has the specified number of sides
parameters: two integers by value:
first integer: the number of dice to roll, by value
second integer - the number of sides on the dice, by value
return type: integer, the summation of all N dice rolls
mostLandings
returns the cell on the board with the most "landings", within an inclusive interval
parameters: a vector<int>; two integers
first parameter: the game board vector, by const reference
second parameter: the start of the interval to be tested, by value
third parameter: the end of the interval to be tested, by value
return type: integer, the index of the first cell in the interval with the most landings
I have the first function completed. But now for the second one. I have no idea what to do. When I run the function I get the error,
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check
And have no idea how to fix that.
here is the code to my main function
cout << "How many sides of the board are there? ";
cin >> n;
cout << endl << "How many spots are there on each side? ";
cin >> k;
cout << endl << "How many simulations? ";
cin >> numSimulations;
endInt = n * k;
cout << endl << "The following spots on each side have the most landings:";
for (i = 0; i<n ; ++i) {
cout << "on side " << i+1;
cout << ", spot ";
cout << mostLandings(v, startInt, endInt);
cout << " has the most landings: ";
cout << Index;
}
From there, once I run the program I get this output:
The following spots on each side have the most landings:on side 1, spot Aborted
I am not sure what is wrong.
Explanation / Answer
The mostLandings method is re-written below:-
int mostLandings(const vector<int>&v,int startInt,int endInt){
int maximumNum=0;
int loc=0;
int i=0;
if (startInt==endInt)
{
loc=startInt;
return loc;
}
else{
for(i=startInt;i<=endInt;i++){
if (maximumNum<v.at(i)){
loc=i;
maximumNum=v.at(i);
}
}
}
return loc;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.