Question: Please help me to correct the following code, the current running result is not in line with expectations. #include #include #include using namespace std; const
Please help me to correct the following code, the current running result is not in line with expectations.
#include
#include
#include
using namespace std;
const int MAXPATHLENGTH ;
TODO TASK : Implement the tearcards function.
The array card is the original array, cardtorn is the array after tearing,
orgsize is the number of elements the array card has.
For example, if card is after the function finishes, the cardtorn array
should be
void tearcardsconst int card int cardtorn int orgsize
for int i ; i orgsize; i
cardtorni cardi;
cardtorni orgsize cardi;
TODO TASK : Implement the rotatecards function.
The function should be able to finish the job of rotating the first 'size' elements
of the array 'card' for 'times' times.
For example, rotatecardsmycard, means rotate the first elements of array
mycard for times
void rotatecardsint card int size, int times
int temp new intsize;
for int i ; i size; i
tempi times size cardi;
for int i ; i size; i
cardi tempi;
delete temp;
TODO TASK : Implement the throwcards function.
The array card is the original array, cardfinal is the array after throwing,
size is the number of cards after throwing, age is the age of the player.
void throwcardsconst int card int cardfinal int size, int age
if age
Even age, discard the first card
for int i ; i size ; i
cardfinali cardi ;
else
Odd age, discard the second card
cardfinal card;
for int i ; i size; i
cardfinali cardi ;
OPTIONAL TODO: You can rewrite this rotateremove function to enhance your understanding of Darray.
However, there's no bonus for it Enjoy.
void rotateremoveint card int size
int leftcardnum size;
whileleftcardnum
move the top card to the bottom
rotatecardscard leftcardnum, ;
remove the now top card
leftcardnum ;
forint i ; i leftcardnum; i
cardi cardi ;
WARNING: DO NOT EDIT THIS FUNCTION
void outputcardstring step, const int card int size
cout step : ;
forint i ; i size; i
cout cardi;
cout endl;
int main
int card;
int year, age;
cout "Welcome to Lab Let's do some real magic!" endl;
cout "Please enter the file you put your secrets in: ;
char filepathMAXPATHLENGTH;
cin filepath;
cout filepath endl;
ifstream finfilepath;
Step: initialize by reading the player's input file
TODO TASK : Read secret file from the player.
TODO TASK : You need to check if the file can be opened, if not, output the error message.
if fin.isopen
cout "Error opening file." endl;
return ; Terminate the program
TODO TASK : Read and initialize the array representing card, player's year of study and age
You should be aware that the cards, year, and age may appear in random order.
In the array, the th element represents the number on the top card,
st element is the number on the second top card, etc.
char typeofinformation;
whilefin typeofinformation
if typeofinformation C
Read the card number
int cardnumber;
fin cardnumber;
Initialize the card array
card cardnumber;
else if typeofinformation Y
Read the year of study
fin year;
else if typeofinformation A
Read the player's age
fin age;
fin.close;
WARNING: DO NOT EDIT THE CODE BELOW
Step: tear the cards and duplicate
int cardtorn;
tearcardscard cardtorn, ;
outputcardtorn cardtorn, ;
Step: rotate based on the player's year of study
rotatecardscardtorn, year;
outputcardrotate by year of study", cardtorn, ;
Step: hide the fourth card
int hiddencard cardtorn;
int cardaftercardtorn cardtorn cardtorn cardtorn cardtorn cardtorn cardtorn;
outputcardhide the fourth", cardafter, ;
Step: if the player's age is even, throw away the first card; else throw away the second card
int cardfinal;
throwcardscardafter, cardfinal, age;
outputcardthrown by age", cardfinal, ;
Step: rotate based on the magic spell
rotatecardscardfinal, ;
outputcardrotate by spell", cardfinal, ;
Step: move the top card to the bottom and then remove the now top card, until there's only one card left
rotateremovecardfinal, ;
Step: compare the hidden card and the last card left
ifhiddencard cardfinal
cout "Your hidden card hiddencard and the last card cardfinal do not match";
else
cout "Your hidden card hiddencard and the last card cardfinal match perfectly!";
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
